SQL Lab 4
SQL Lab 4
TASKS
The SELECT DISTINCT statement is used to return only distinct (different) values.
In a table, a column may contain many duplicate values; and sometimes you only want to
list the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different) values.
Demo Database
The following SQL statement selects only the distinct values from the "City" columns from
the "Customers" table:
Example
SELECT DISTINCT City FROM Customers;
The WHERE clause is used to extract only those records that fulfill a specified criterion.
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Demo Database
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
The following SQL statement selects all the customers from the country "Mexico", in the
"Customers" table:
Example
SELECT * FROM Customers
WHERE Country='Mexico';
SQL requires single quotes around text values (most database systems will also allow
double quotes).
Example
SELECT * FROM Customers
WHERE CustomerID=1;
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
The AND & OR operators are used to filter records based on more than one condition.
The OR operator displays a record if either the first condition OR the second condition is
true.
Demo Database
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
The following SQL statement selects all customers from the country "Germany" AND the city
"Berlin", in the "Customers" table:
Example
SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';
OR Operator Example
The following SQL statement selects all customers from the city "Berlin" OR "München", in
the "Customers" table:
Example
SELECT * FROM Customers
WHERE City='Berlin'
OR City='München';
Combining AND & OR
You can also combine AND and OR (use parenthesis to form complex expressions).
The following SQL statement selects all customers from the country "Germany" AND the city
must be equal to "Berlin" OR "München", in the "Customers" table:
Example
SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='München');
The ORDER BY keyword is used to sort the result-set by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in a descending order, you can use the DESC keyword.
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Demo Database
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
The following SQL statement selects all customers from the "Customers" table, sorted by
the "Country" column:
Example
SELECT * FROM Customers
ORDER BY Country;
The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:
Example
SELECT * FROM Customers
ORDER BY Country DESC;
The following SQL statement selects all customers from the "Customers" table, sorted by
the "Country" and the "CustomerName" column:
Example
SELECT * FROM Customers
ORDER BY Country,CustomerName;
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause can be very useful on large tables with thousands of records.
Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause.
MySQL Syntax
SELECT column_name(s)
FROM table_name
LIMIT number;
Example
SELECT *
FROM Persons
LIMIT 5;
Oracle Syntax
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
Example
SELECT *
FROM Persons
WHERE ROWNUM <=5;
Demo Database
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
The following SQL statement selects the two first records from the "Customers" table:
Example
SELECT TOP 2 * FROM Customers;
The following SQL statement selects the first 50% of the records from the "Customers"
table:
Example
SELECT TOP 50 PERCENT * FROM Customers;
EXPECTED DELIVERABLE