Lecture Slide 3
Lecture Slide 3
⯈ Sorting Records
⯈ Joining Tables
⯈ Aggregate Functions
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
A condition in a where clause includes an attribute name, an operator, and attribute value.
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
There are two wildcards used in conjunction with the LIKE operator:
•% - The percent sign represents zero, one, or multiple characters
•_ - The underscore represents a single character
The percent sign and the underscore can also be used in combinations!
LIKE Syntax
The following SQL statement selects all customers with a CustomerName starting with “b":
SELECT * FROM Customers
WHERE CustomerName LIKE ’b%’;
The following SQL statement selects all customers with a CustomerName ending with “b":
The following SQL statement selects all customers with a CustomerName that have "or" in any
position:
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
The following SQL statement selects all customers with a CustomerName that have "r" in the
second position:
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
QUERYING (RETRIEVING) DATA FROM
RELATIONAL
The SQL IN Operator
DATABASES
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
QUERYING (RETRIEVING) DATA FROM
RELATIONAL
Examples of using IN Operator
DATABASES
The following SQL statement selects all customers that are located in "Germany", "France" and
"UK":
The following SQL statement selects all customers that are NOT located in "Germany",
"France"
or "UK":
The following SQL statement selects all customers that are from the same countries as the
suppliers:
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
Examples of using BETWEEN Operator
The following SQL statement selects all products with a price BETWEEN 10 and 20:
To display the products outside the range of the previous example, use NOT BETWEEN:
The following SQL statement selects all orders with an OrderDate BETWEEN '04-July-1996' and
'09-July-1996’:
The following SQL statement selects all fields from "Customers" where country is “UK" AND city
is “London":
The following SQL statement selects all fields from "Customers" where city is “London" OR
“Liverpool":
The following SQL statement selects all fields from "Customers" where country is NOT “UK":
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.
ORDER BY Syntax:
The following SQL statement selects all customers from the "Customers" table, sorted
by the "Country" and the "CustomerName" column:
SELECT * FROM Customers
ORDER BY Country, CustomerName;
The following SQL statement selects all customers from the "Customers" table, sorted
ascending by the "Country" and descending by the "CustomerName" column:
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
QUERYING (RETRIEVING) DATA FROM RELATIONAL
DATABASES
• Handling or testing NULL values
It is not possible to test for NULL values with comparison operators, such as =, <,
or
<>.
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
The following SQL statement uses the IS NULL operator to list all persons that have no
address:
The following SQL statement uses the IS NOT NULL operator to list all persons that do
have an address:
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
There four different types of the JOINs in SQL:
1. INNER JOIN: Returns records that have matching values in both tables
2. LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
3. RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
4. FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL INNER JOIN Keyword
The INNER JOIN keyword selects records that have matching values in both tables.
SELECT column_name(s)
FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name;
The following SQL statement selects all orders with customer information:
The LEFT JOIN keyword returns all records from the left table (table1), and the matched
records from the right table (table2). The result is NULL from the right side, if there is
no match.
The LEFT JOIN keyword returns all records from the left table (Customers), even if
there are no matches in the right table (Orders).
SELECT column_name(s)
FROM table1 LEFT JOIN table2 ON table1.column_name =
table2.column_name;
The following SQL statement will select all customers, and any orders they might
have:
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matched records from the left table (table1). The result is NULL from the left side, when
there is no match.
The RIGHT JOIN keyword returns all records from the right table, even if there are no
matches in the left table.
The following SQL statement will return all employees, and any orders they might have
have placed:
The FULL OUTER JOIN keyword return all records when there is a match in either left
(table1) or right (table2) table records.
SELECT column_name(s)
FROM table1 FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
The following SQL statement selects all customers, and all orders:
We often use aggregate functions with the GROUP BY and HAVING clauses of the
SELECT statement.
The following are the most commonly used SQL aggregate functions:
To get the number of the products in the products table, you use the COUNT function as follows:
1 SELECT
2 COUNT(*)
3 FROM
4 products;
To calculate the average units in stock of the products, you use the AVG function as follows:
1 SELECT
2 AVG(unitsinstock)
3 FROM
4 products;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL aggregate function examples
To calculate units in stock by product category, you use the AVG function with the GROUP BY clause as follows:
1 SELECT
2 categoryid, AVG(unitsinstock)
3 FROM
4 products
GROUP BY categoryid;
To calculate the sum of units in stock by product category, you use the SUM function with the GROUP BY clause as
the following query:
1 SELECT categoryid,
2 SUM(unitsinstock) FROM products
3 GROUP BY categoryid;
4
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL aggregate function examples
To get the minimum units in stock of products in the products table, you use the MIN function as follows:
1 SELECT
2 MIN(unitsinstock)
3 FROM
4 products;
To get the maximum units in stock of products in the products table, you use the MAX function as shown in the
following query:
1 SELECT
2 MAX(unitsinstock)
3 FROM products;
4