Chapter 6
Chapter 6
Chapter 6
Example 1:
selects the "CustomerName" and "City" columns from the "Customers" table
Example 2:
selects all the columns from the "Customers" table
Syntax:
The following SQL statement selects all (including the duplicates) values from the "Country"
column in the "Customers" table.
The following SQL statement selects only the DISTINCT values from the "Country" column
in the "Customers" table:
The WHERE clause is used to filter records. It is used to extract only those records that fulfill
a specified condition.
Syntax:
Example:
selects all the customers from the country "Mexico", in the "Customers" table
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
• The AND operator displays a record if all the conditions separated by AND are TRUE.
• The OR operator displays a record if any of the conditions separated by OR is TRUE.
AND Syntax
OR Syntax
NOT Syntax
AND Example
selects all fields from "Customers" where country is "Germany" AND city is "Berlin"
OR Example
selects all fields from "Customers" where city is "Berlin" OR "München"
Example
selects all fields from "Customers" where country is "Germany" OR "Spain"
NOT Example
selects all fields from "Customers" where country is NOT "Germany"
Example:
selects all fields from "Customers" where country is "Germany" AND city must be "Berlin"
OR "München"
Example:
selects all fields from "Customers" where country is NOT "Germany" and NOT "USA"
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in descending order, use the DESC keyword.
Syntax
Example
selects all customers from the "Customers" table, sorted by the "Country" column
Example :
selects all customers from the "Customers" table, sorted DESCENDING by the "Country"
column
Example:
selects all customers from the "Customers" table, sorted by the "Country" and the
"CustomerName" column. This means that it orders by Country, but if some rows have the
same Country, it orders them by CustomerName.
Example:
selects all customers from the "Customers" table, sorted ascending by the "Country" and
descending by the "CustomerName" column