SQL zero-based introductory learning (2)
SQL WHERE child clause
The WHERE clause is used to extract those records that meet the specified conditions.
SQL WHERE syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Parameter Description:
column1, column2, …: The name of the field to be selected, which can be multiple fields. If no field names are specified, all fields will be selected.
table_name: The name of the table to be queried.
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
WHERE clause example
The following SQL statement selects all websites whose country is "CN" from the "Websites" table:
SELECT * FROM Websites WHERE country='CN';
Execution output:
Text Fields vs. Numeric Fields
SQL uses single quotes to surround text values (most database systems also accept double quotes).
In the previous example the 'CN' text field used single quotes.
For numeric fields, do not use quotes.
example
SELECT * FROM Websites WHERE id=1;
Execution output:
Operators in the WHERE clause
The following operators can be used in the WHERE clause:
SQL AND & OR operators
AND & OR operators are used to filter records based on more than one condition.
SQL AND & OR operators
The AND operator displays a record if both the first condition and the second condition are true.
The OR operator displays a record if only one of the first and second conditions is true.
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
AND operator instance
The following SQL statement selects all websites whose country is "CN" and whose alexa rank is greater than "50" from the "Websites" table:
example
SELECT * FROM Websites
WHERE country='CN'
AND alexa > 50;
Execution output:
OR operator instance
The following SQL statement selects all customers whose country is "USA" or "CN" from the "Websites" table:
example
SELECT * FROM Websites
WHERE country='USA'
OR country='CN';
Execution output:
Combining AND & OR
You can also combine AND and OR (using parentheses to form complex expressions).
The following SQL statement selects all websites with alexa rank greater than "15" and country "CN" or "USA" from the "Websites" table:
example
SELECT * FROM Websites
WHERE alexa > 15
AND (country='CN' OR country='USA');
Execution output:
SQL ORDER BY keyword
The ORDER BY keyword is used to sort the result set.
SQL ORDER BY keyword
The ORDER BY keyword is used to sort the result set by one or more columns.
The ORDER BY keyword sorts records in ascending order by default. If you need to sort records in descending order, you can use the DESC keyword.
SQL ORDER BY syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
column1, column2, …: The name of the field to be sorted, which can be multiple fields.
ASC: Indicates sorting in ascending order.
DESC: Indicates sorting in descending order.
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
ORDER BY instance
The following SQL statement selects all websites from the "Websites" table, sorted by the "alexa" column:
SELECT * FROM Websites
ORDER BY alexa;
Execution output:
ORDER BY DESC example
The following SQL statement selects all websites from the "Websites" table and sorts them in descending order by the "alexa" column:
SELECT * FROM Websites
ORDER BY alexa DESC;
Execution output:
ORDER BY multiple columns
The following SQL statement selects all websites from the "Websites" table, sorted by the "country" and "alexa" columns:
SELECT * FROM Websites
ORDER BY country,alexa;
Execution output: