View
219
Download
2
Embed Size (px)
Citation preview
--The SQL Query Language DML-- 1
LIKE
LIKE allows to select character strings which have some element in common by using wild cards:
Wild cards: “%” means “zero, one or more characters” “_” means “one single character”
--The SQL Query Language DML-- 2
SELECT: Queries Over Several Tables
Which films name ends in “100”?
SELECT TitleFROM Film
WHERE Title LIKE ‘%100’
Which films name contains the character a, b and c in the first, third and fifth positions?
SELECT TitleFROM Film
WHERE Title LIKE ‘a_b_c%’
--The SQL Query Language DML-- 3
Correlated Subqueries
Subquery must be re-evaluated for each tuple in outer SELECT
Table variable is used Find the customers who live at more than one address
(assume the key of Customer is (CustomerID, Address)).
SELECT Name
FROM Customer C
WHERE CustomerID IN
(SELECT D.CustomerID
FROM Customer D
WHERE C.Address <> D.Address)
--The SQL Query Language DML-- 4
Multiple Attribute Producing Subquery
The subquery produces a table with several columns EXISTS
true if subquery produced table has a tuple
NOT EXISTS true if subquery produced table is empty
--The SQL Query Language DML-- 5
EXISTS
List the customers who live in Dublin and have a film reserved.
SELECT NameFROM CustomerWHERE City = ’Dublin’
AND EXISTS ( SELECT * FROM Reserved WHERE Reserved.CustomerID =
Customer.CustomerID)
--The SQL Query Language DML-- 6
EXISTS, cont.
Often, EXISTS can be replaced with another correlation name.
List the customers who live in Dublin and have a film reserved.
SELECT NameFROM Customer, ReservedWHERE City = ’Dublin’
AND Customer.CustomerID = Reserved.CustomerID
--The SQL Query Language DML-- 7
NOT EXISTS
List the customers who live in Dublin but have no films reserved.
SELECT NameFROM CustomerWHERE City = ’Dublin’
AND NOT EXISTS(SELECT * FROM Reserved
WHERE Reserved.CustomerID = Customer.CustomerID)
--The SQL Query Language DML-- 8
Outline - The SELECT statement
Single table Projection Selection
Multiple tables Cartesian product and join Set operations Subqueries
Optional clauses Ordering results Computing aggregates on groups
Additional joins
--The SQL Query Language DML-- 9
ORDER BY
Can sort the result of a select, using ORDER BY. Who has reserved a film?
SELECT NameFROM Customer, ReservedWHERE Customer.CustomerID = Reserved.CustomerIDORDER BY Name
Can also sort in descending order, via DESC (ASC is the default).
SELECT NameFROM Customer, ReservedWHERE Customer.CustomerID = Reserved.CustomerIDORDER BY Name DESCOnly columns in the select list can be used for the ordering.
--The SQL Query Language DML-- 10
Select in the From Clause
The table in a from clause can itself be a select statement.
List the customers who have reserved an expensive film.
SELECT CustomerIDFROM Reserved,(SELECT *
FROM FilmWHERE RentalPrice > 4) AS Expensive
WHERE Reserved.FilmID = Expensive.FilmID
A correlation name is required in such cases.
--The SQL Query Language DML-- 11
Aggregates
Aggregates operate on the set of values of a column of a table, and return a single value. SUM: sum of values AVG: average value MAX: maximum value MIN: minimum value COUNT: number of values
How many reservations are there?
SELECT COUNT (*)FROM Reserved
--The SQL Query Language DML-- 12
Aggregates, cont.
What is the total rental price of all films?
SELECT SUM(RentalPrice)FROM Film
Eliminate duplicates before computing aggregate with DISTINCTIn how many cities do customers reside?
SELECT COUNT(DISTINCT City)FROM Customer
--The SQL Query Language DML-- 13
Aggregates, cont.
What is the average rental price of reserved films?
SELECT AVG(RentalPrice)FROM Reserved, FilmWHERE Reserved.FilmID = Film.FilmID
Find the film(s) with the highest rental price.
SELECT TitleFROM FilmWHERE RentalPrice IN (SELECT MAX(RentalPrice)
FROM Film)
--The SQL Query Language DML-- 14
GROUP BY
The table can be partitioned into several groups specifying
GROUP BY <list of columns>
Each group has the same attribute values for the indicated columns.
Name Age
Pam 4
Pat 1
Joe 3
Joe 2
Pat 1
Pam 4
Joe 3
Joe 2
Groups
GROUP BY Name
--The SQL Query Language DML-- 15
GROUP BY, cont.
Aggregate is applied to each group. What is the oldest age for each name?
SELECT Name, MAX(Age)FROM Person
GROUP BY Name
Name Age
Pam 4
Pat 1
Joe 3
Joe 2
Groups
Pam 4
Pat 1
Joe 3
Joe 2
MAX is 1
MAX is 4
MAX is 3
Pam 4
Pat 1
Joe 3
Name Max(Age)
--The SQL Query Language DML-- 16
GROUP BY, cont.
What is the average rental price of reserved films, by kind?
SELECT Kind, AVG(RentalPrice)FROM Reserved, FilmWHERE Reserved.FilmID = Film.FilmIDGROUP BY Kind
--The SQL Query Language DML-- 17
HAVING
Individual groups can be eliminated by using HAVING.
List the names and average age per name, as long as the average is more than 2.
Name Age
Pam 4
Pat 1
Joe 3
Joe 2
Groups
Pam 4
Pat 1
Joe 3
Joe 2
AVG is 1
AVG is 4
AVG is 2.5
Pam 4.0
Joe 2.5
Name AVG(Age)
SELECT Name, AVG(Age)FROM Person GROUP BY Name
HAVING AVG(Age) > 2
--The SQL Query Language DML-- 18
HAVING, cont.
Columns in HAVING clause must appear in the GROUP BY (or be contained within an aggregate).
List the customers whose average rental price for reserved films is greater than $3.
SELECT NameFROM Customer, Reserved, FilmWHERE Customer.CustomerID = Reserved.CustomerID
AND Reserved.FilmID = Film.FilmIDGROUP BY Name
HAVING AVG(RentalPrice) > 3
Note, RentalPrice appears within an aggregate
--The SQL Query Language DML-- 19
Summary
A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory.
The clauses are specified in the following order. SELECT column(s) FROM table list WHERE condition GROUP BY grouping column(s) HAVING group condition ORDER BY sort list
--The SQL Query Language DML-- 20
Summary, cont.
The query is evaluated in a different order. The tables in the from clause are combined using Cartesian
products. The where predicate is then applied. The resulting tuples are grouped according to the group by
clause. The having predicate is applied to each group, possibly
eliminating some groups. The aggregate(s) are applied to each remaining group. The select clause is performed last (followed by order clause).