查询数据库

SQL 中级

Jasmin Ludolf

Data Science Content Developer, DataCamp

课程路线图

 

  • 查询数据库
  • 统计并查看指定记录
  • 理解查询执行与风格
  • 过滤
  • 聚合函数
  • 排序与分组

PostgreSQL 徽标

SQL 中级

我们的 films 数据库

架构图:展示 films 数据库的四张表及字段名和数据类型

SQL 中级

COUNT()

  • COUNT()
  • 统计某字段中有值的记录数
  • 使用别名以提高清晰度
SELECT COUNT(birthdate) AS count_birthdates
FROM people;
|count_birthdates|
|----------------|
|6152            |
SQL 中级

COUNT() 多个字段

SELECT COUNT(name) AS count_names, COUNT(birthdate) AS count_birthdates
FROM people;
|count_names|count_birthdates|
|-----------|----------------|
|6397       |6152            |
SQL 中级

在 COUNT() 中使用 *

  • COUNT(field_name) 统计字段中的值
  • COUNT(*) 统计表中的记录数
  • * 表示所有字段
SELECT COUNT(*) AS total_records
FROM people;
|total_records|
|-------------|
|8397         |
SQL 中级

DISTINCT

  • DISTINCT 去重,仅返回唯一值
SELECT language
FROM films;
|language |
|---------|
|Danish   |
|Danish   |
|Greek    |
|Greek    |
|Greek    |
  • 我们的 films 表中有哪些语言?

 

SELECT DISTINCT language
FROM films;
|language |
|---------|
|Danish   |
|Greek    |
SQL 中级

COUNT() 与 DISTINCT

  • COUNT()DISTINCT 结合,统计唯一值
SELECT COUNT(DISTINCT birthdate) AS count_distinct_birthdates
FROM people;
|count_distinct_birthdates|
|-------------------------|
|5398                     |
  • COUNT() 包含重复值
  • DISTINCT 排除重复值
SQL 中级

Ayo berlatih!

SQL 中级

Preparing Video For Download...