Lec06 SQL Subqueries
Lec06 SQL Subqueries
1
Lecture Goals
• Today we will learn how to write (even) more
powerful SQL queries
2
Subqueries
• A subquery is a SQL query nested inside a larger query
– such inner-outer queries are called nested queries
• A subquery may occur in:
– A SELECT clause
– A FROM clause
– A WHERE clause
• Rule of thumb: avoid nested queries when possible;
keep in mind that sometimes it’s impossible
– (though use in FROM is often not as bad)
3
Subqueries…
• Can return a single constant and this constant can be
compared with another value in a WHERE clause
• Can return relations that can be used in various ways
in WHERE clauses
• Can appear in FROM clauses, followed by a tuple
variable that represents the tuples in the result of the
subquery
• Can appear as computed values in a SELECT clause
4
1. Subqueries in SELECT
1. Subqueries in SELECT
Whenever possible, don’t use nested queries:
DBMS also
does this…
1. Subqueries in SELECT
Compute the number of products made by each company
8
Product (pname, price, cid)
Company(cid, cname, city)
1. Subqueries in SELECT
But are these really equivalent?
SELECT DISTINCT C.cname, (SELECT count(*)
FROM Product P
WHERE P.cid=C.cid)
FROM Company C
2. Subqueries in FROM
SELECT X.pname
FROM (SELECT * FROM Product AS Y WHERE price > 20) as X
WHERE X.price < 500
SELECT pname
FROM Product
WHERE price > 20 AND price < 500
10
2. Subqueries in FROM
• We will see that sometimes we really need a
subquery
– will see most compelling examples next lecture
– in that case, we can put it in the FROM clause
11
Product (pname, price, cid)
Company(cid, cname, city)
3. Subqueries in WHERE
Find all companies that make some products with price < 100
Existential quantifiers
Using EXISTS:
SELECT DISTINCT C.cname
FROM Company C
WHERE EXISTS (SELECT *
FROM Product P
WHERE C.cid = P.cid and P.price < 100)
12
Product (pname, price, cid)
Company(cid, cname, city)
3. Subqueries in WHERE
Find all companies that make some products with price < 100
Existential quantifiers
Using IN
SELECT DISTINCT C.cname
FROM Company C
WHERE C.cid IN (SELECT P.cid
FROM Product P
WHERE P.price < 100)
13
Product (pname, price, cid)
Company(cid, cname, city)
3. Subqueries in WHERE
Find all companies that make some products with price < 100
Existential quantifiers
Using ANY:
SELECT DISTINCT C.cname
FROM Company C Not supported
WHERE 100 > ANY (SELECT price
FROM Product P
in sqlite
WHERE P.cid = C.cid)
14
Product (pname, price, cid)
Company(cid, cname, city)
3. Subqueries in WHERE
Find all companies that make some products with price < 100
Existential quantifiers
3. Subqueries in WHERE
Find all companies where all their products have price < 100
same as:
Find all companies that make only products with price < 100
Universal quantifiers
3. Subqueries in WHERE
Find all companies where all their products have price < 100
1. Find the other companies with some product having price ≥ 100
SELECT DISTINCT C.cname
FROM Company C
WHERE C.cid IN (SELECT P.cid
FROM Product P
WHERE P.price >= 100)
2. Find all companies where all their products have price < 100
SELECT DISTINCT C.cname
FROM Company C
WHERE C.cid NOT IN (SELECT P.cid
FROM Product P
WHERE P.price >= 100)
CSE 414 - Fall 2017 18
Product (pname, price, cid)
Company(cid, cname, city)
3. Subqueries in WHERE
Find all companies where all their products have price < 100
Universal quantifiers
Using EXISTS:
SELECT DISTINCT C.cname
FROM Company C
WHERE NOT EXISTS (SELECT *
FROM Product P
WHERE P.cid = C.cid and P.price >= 100)
18
Product (pname, price, cid)
Company(cid, cname, city)
3. Subqueries in WHERE
Find all companies where all their products have price < 100
Universal quantifiers
Using ALL:
SELECT DISTINCT C.cname
FROM Company C Not supported
WHERE 100 >= ALL (SELECT price
FROM Product P
in sqlite
WHERE P.cid = C.cid)
19
Question for Database Fans
and their Friends
• Can we unnest the universal quantifier query ?
– No
20
Product (pname, price, cid)
Company(cid, cname, city)
Monotone Queries
• Definition: A query Q is monotone if:
– Whenever we add tuples to one or more input tables, the
answer to the query will not lose any of the tuples
Product Company
pname price cid cid cname city A B
Q
Gizmo 19.99 c001 c002 Sunworks Bonn Gizmo Lyon
Gadget 999.99 c004 c001 DB Inc. Lyon Camera Lodtz
Camera 149.99 c003 c003 Builder Lodtz
Product Company
pname price cid A B
cid cname city Q
Gizmo 19.99 c001 c002 Sunworks Bonn Gizmo Lyon
22
Product (pname, price, cid)
Company(cid, cname, city)
Monotone Queries
• The query:
Find all companies where all their products have price < 100
is not monotone
pname price cid cid cname city cname
Gizmo 19.99 c001 c001 Sunworks Bonn Sunworks
24