Coursera 笔记

Module 01 · Getting Started with SQL

SELECT statement examplesSELECT 语句示例

Coursera ↗

Estimated time: 5 min  预计时间:5 分钟

Objectives 目标

At the end of this reading, you will learn how to:
在本次阅读结束时,您将学会:

  • Use various SELECT queries to retrieve data from the database.
    使用各种 SELECT 查询从数据库中检索数据。

SELECT statement usage SELECT 语句的使用

SELECT is classified as a Database Query command used to retrieve information from a database table.
SELECT 被归类为数据库查询命令,用于从数据库表中检索信息。

There are various forms in which a SELECT statement is used.
SELECT 语句有多种使用形式。

  1. The general syntax of a SELECT statement retrieves the data under the listed columns from Table_1. The code is:
    SELECT 语句的一般语法从 Table_1 中检索列出的列下的数据。代码如下:
SELECT COLUMN1, COLUMN2, ... FROM TABLE_1 ;
  1. To retrieve all columns from a table, use " * " instead of specifying individual column names. The code below retrieves the entire table.
    要从表中检索所有列,请使用"*"代替指定单个列名。下面的代码检索整个表。
SELECT * FROM TABLE_1 ;
  1. Use the WHERE clause to filter the required data based on a predicate. The code below filters the response to only the entries that match the predicate.
    使用 WHERE 子句根据谓词过滤所需数据。下方的代码仅筛选出与谓词匹配的响应条目。
SELECT <COLUMNS> FROM TABLE_1 WHERE <predicate> ;

SELECT examples SELECT 示例

Let's look at these codes in action. Below is a database table called 'COUNTRY,' which contains the columns ID, Name, and CCode. Here, CCode is a 2 letter country code.
让我们看看这些代码的实际应用。下面是一个名为'COUNTRY'的数据库表,其中包含 ID 、 Name 和 CCode 等列。在这里, CCode 是一个两位字母的国家代码。

ID Name  姓名 CCode  C 语言
1 United States of America
美国
US
2 China  中国 CH
3 Japan  日本 JA
4 Germany  德国 GE
5 India  印度 IN
6 United Kingdom  英国 UK
7 France  法国 FR
8 Italy  意大利 IT
9 Canada  加拿大 CA
10 Brazil  巴西 BR

Example #1 示例 #1

When we apply the SELECT code SELECT * FROM COUNTRY ;, the query retrieves all rows and columns from the database table named COUNTRY.
当我们对 SELECT 代码 SELECT * FROM COUNTRY ; 应用时,查询会从名为 COUNTRY 的数据库表中检索所有行和列。

  • 'SELECT *' instructs the database to select all columns from the table.
    "SELECT *" 指示数据库从表中选取所有列。
  • 'FROM COUNTRY' specifies the table from which to retrieve the data. In this case, it's the "COUNTRY" table, so the entire table appears, as shown below.
    'FROM COUNTRY'指定要从中检索数据的表。在这种情况下,它是"COUNTRY"表,因此整个表都会显示,如下所示。

Response:  响应:

ID Name  姓名 CCode
1 United States of America
美利坚合众国
US
2 China  中国 CH
3 Japan  日本 JA
4 Germany  德国 GE
5 India  印度 IN
6 United Kingdom  英国 UK
7 France  法国 FR
8 Italy  意大利 IT
9 Canada  加拿大 CA
10 Brazil  巴西 BR

Example #2 示例 #2

The SQL query SELECT ID, Name FROM COUNTRY ; retrieves specific columns from a database table named 'COUNTRY'.
SQL 查询 SELECT ID, Name FROM COUNTRY ; 从名为 'COUNTRY' 的数据库表中检索特定列。

  • 'SELECT ID, Name' instructs the database to select two specific columns from the table: "ID" and "Name." It will return these two columns for each row that matches the query criteria.
    'SELECT ID, Name' 指示数据库从表中选择两个特定列:"ID" 和 "Name"。它将返回与查询条件匹配的每一行中的这两个列。
  • 'FROM COUNTRY' specifies the table from which to retrieve the data, which is the "COUNTRY" table. The table below shows that only the "ID" and "Name" columns were retrieved.
    'FROM COUNTRY' 指定要从中检索数据表,即 "COUNTRY" 表。下表显示仅检索了 "ID" 和 "Name" 列。

Response:  回复:

ID Name  名称
1 United States of America
美利坚合众国
2 China  中国
3 Japan  日本
4 Germany  德国
5 India  印度
6 United Kingdom  英国
7 France  法国
8 Italy  意大利
9 Canada  加拿大
10 Brazil  巴西

Example #3 示例 #3

The SQL query SELECT * FROM COUNTRY WHERE ID <= 5 ; retrieves all columns from the "COUNTRY" table where the value in the "ID" column is less than or equal to 5.
SQL 查询 SELECT * FROM COUNTRY WHERE ID <= 5 ; 从 "COUNTRY" 表中检索所有列,其中 "ID" 列中的值小于或等于 5。

  • SELECT * instructs the database to select all columns from the specified table.<br>SELECT * 指示数据库从指定表中选择所有列。
  • FROM COUNTRY specifies the table from which to retrieve the data, which is the 'COUNTRY' table.
    FROM COUNTRY 指定要从中检索数据的数据表,即 'COUNTRY' 表。
  • WHERE ID <= 5 ; is a condition that filters the rows from the table. It will only return rows where the value in the "ID" column is less than or equal to 5. In the table below, you can see that only rows 1-5 were retrieved.
    WHERE ID <= 5 ; 是一个过滤表格行的条件。它只会返回 "ID" 列值小于或等于 5 的行。在下表中,你可以看到只检索到了 1-5 行。

Response:  响应:

ID Name  姓名 CCode
1 United States of America
美国
US
2 China  中国 CH
3 Japan  日本 JA
4 Germany  德国 GE
5 India  印度 IN

Example #4 示例 #4

The SQL querySELECT * FROM COUNTRY WHERE CCode = 'CA' ; retrieves all columns from the "COUNTRY" table where the value in the "CCode" column is equal to 'CA'.
SQL 查询 SELECT * FROM COUNTRY WHERE CCode = 'CA' ; 检索"COUNTRY"表中的所有列,其中"CCode"列中的值等于'CA'。

  • SELECT * instructs the database to select all columns from the specified table.<br>SELECT *指示数据库从指定表中选择所有列。
  • FROM COUNTRY specifies the bale from which to retrieve the data, which is the 'COUNTRY' table.
    FROM COUNTRY 指定了要从哪个捆包中检索数据,这是'COUNTRY'表。
  • WHERE CCode = 'CA'; is a condition that filters the rows from the table. It will only return rows where the value in the "CCode" column is equal to 'CA.' In the table below, you will find that only the CA column was retrieved.
    WHERE CCode = 'CA';是一个过滤行的条件。它将只返回"CCode"列中的值等于'CA'的行。在下面的表中,你会发现只有 CA 列被检索到了。

Response:  响应:

ID Name  名称 CCode  C 语言
9 Canada  加拿大 CA

In the lab that follows later in the module, you will apply these concepts and practice more SELECT queries hands-on.
在模块后面跟随的实验室中,您将应用这些概念并动手练习更多 SELECT 查询。

In this reading, you learned that:
在本阅读中,你学习了:

  • SELECT is a Database Query command that retrieves information from a database table.
    SELECT 是一个数据库查询命令,用于从数据库表中检索信息。
  • The SELECT statement has various forms depending on what action you require.
    SELECT 语句根据所需操作的不同有多种形式。
  • The general syntax will retrieve the data under the listed columns from a named table.
    一般语法将从命名的表中检索列出的列中的数据。
  • Use "" to retrieve all columns from a table without specifying individual column names.
    使用"
    "来检索表中的所有列,而无需指定各个列名。
  • Use the WHERE clause to filter the data based on a predicate.
    使用 WHERE 子句根据谓词过滤数据。