Chapter 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Chapter 6

6.1 The SQL SELECT Statement


The SELECT statement is probably the most used SQL command. The SELECT statement is
used for retrieving rows from the database and enables the selection of one or many rows or
columns from one or many tables in the database. The data returned is stored in a result table,
called the result-set.
So, in the simplest form we can use the SELECT statement as follows:

If we want all columns, we use the symbol “*”

Below is a schema of the "Customers" table in the Northwind sample database:


Customers(CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country)

Example 1:
selects the "CustomerName" and "City" columns from the "Customers" table

Example 2:
selects all the columns from the "Customers" table

6.2 The SQL SELECT DISTINCT Statement


The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a
table, a column often contains many duplicate values; and sometimes you only want to list the
different (distinct) values.

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:

6.3 The SQL WHERE Clause

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 following operators can be used in the WHERE clause:


operator description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal
Between Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column
6.4 The SQL AND, OR and NOT Operators

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.

The NOT operator displays a record if the condition(s) is NOT 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"

You can also combine the AND, OR and NOT operators.

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"

6.5 SQL ORDER BY Keyword

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

You might also like