SQL: The Query Language (Part II)
Expressions and Strings
SELECT [Link], age1=[Link]-5, 2*[Link] AS age2
FROM Sailors S
WHERE [Link] LIKE ‘B_%B’
Illustrates use of arithmetic expressions and string
pattern matching: Find triples (of ages of sailors and two
fields defined by expressions) for sailors whose names
begin and end with B and contain at least three characters.
AS and = are two ways to name fields in result.
LIKE is used for string matching. `_’ stands for any
one character and `%’ stands for 0 or more arbitrary
characters.
Find sid’s of sailors who’ve reserved a red or a green boat
SELECT [Link]
UNION: Can be used to FROM Sailors S, Boats B, Reserves R
WHERE [Link]=[Link] AND [Link]=[Link]
compute the union of any
AND ([Link]=‘red’ OR [Link]=‘green’)
two union-compatible sets
of tuples (which are
themselves the result of SELECT [Link]
FROM Sailors S, Boats B, Reserves R
SQL queries).
WHERE [Link]=[Link] AND
Also available: EXCEPT [Link]=[Link]
(What do we get if we AND [Link]=‘red’
replace UNION by EXCEPT?) UNION
SELECT [Link]
FROM Sailors S, Boats B, Reserves R
WHERE [Link]=[Link] AND
[Link]=[Link]
AND [Link]=‘green’
Find sid’s of sailors who’ve reserved a red and a green boat
SELECT [Link]
FROM Sailors S, Boats B1, Reserves R1,
INTERSECT: Can be used to Boats B2, Reserves R2
compute the intersection WHERE [Link]=[Link] AND [Link]=[Link]
AND [Link]=[Link] AND [Link]=[Link]
of any two union-
AND ([Link]=‘red’ AND [Link]=‘green’)
compatible sets of tuples.
Key field!
Included in the SQL/92
SELECT [Link]
standard, but some FROM Sailors S, Boats B, Reserves R
systems don’t support it. WHERE [Link]=[Link] AND
Contrast symmetry of the [Link]=[Link]
AND [Link]=‘red’
UNION and INTERSECT INTERSECT
queries with how much SELECT [Link]
the other versions differ. FROM Sailors S, Boats B, Reserves R
WHERE [Link]=[Link] AND
[Link]=[Link]
AND [Link]=‘green’
Nested Queries
Find names of sailors who’ve reserved boat #103:
SELECT [Link]
FROM Sailors S
WHERE [Link] IN (SELECT [Link]
FROM Reserves R
WHERE [Link]=103)
A very powerful feature of SQL: a WHERE clause can itself contain
an SQL query! (Actually, so can FROM and HAVING clauses.)
To find sailors who’ve not reserved #103, use NOT IN.
To understand semantics of nested queries, think of a nested loops
evaluation: For each Sailors tuple, check the qualification by computing
the subquery.
Nested Queries with Correlation
Find names of sailors who’ve reserved boat #103:
SELECT [Link]
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE [Link]=103 AND [Link]=[Link])
EXISTS is another set comparison operator, like IN.
If UNIQUE is used, and * is replaced by [Link], finds sailors with at
most one reservation for boat #103. (UNIQUE checks for duplicate
tuples; * denotes all attributes. Why do we have to replace * by
[Link]?)
Illustrates why, in general, subquery must be re-computed for
each Sailors tuple.
More on Set-Comparison Operators
We’ve already seen IN, EXISTS and UNIQUE. Can also
use NOT IN, NOT EXISTS and NOT UNIQUE.
Also available: op SOME, op ALL, where op is
,, ,,,
Find sailors whose rating is greater than that of some
sailor called Horatio:
SELECT *
FROM Sailors S
WHERE [Link] > SOME (SELECT [Link]
FROM Sailors S2
WHERE [Link]=‘Horatio’)
COUNT (*)
COUNT ( [DISTINCT] A)
Aggregate Operators SUM ( [DISTINCT] A)
AVG ( [DISTINCT] A)
MAX (A)
Significant extension of
MIN (A)
relational algebra.
single column
SELECT COUNT (*)
SELECT [Link]
FROM Sailors S
FROM Sailors S
SELECT AVG ([Link]) WHERE [Link]= (SELECT MAX([Link])
FROM Sailors S FROM Sailors S2)
WHERE [Link]=10
SELECT COUNT (DISTINCT [Link]) SELECT AVG ( DISTINCT [Link])
FROM Sailors S FROM Sailors S
WHERE [Link]=‘Bob’ WHERE [Link]=10
Find name and age of the oldest sailor(s)
SELECT [Link], MAX ([Link])
The first query is illegal! FROM Sailors S
(We’ll look into the reason
a bit later, when we SELECT [Link], [Link]
discuss GROUP BY.) FROM Sailors S
WHERE [Link] =
The third query is
(SELECT MAX ([Link])
equivalent to the second
FROM Sailors S2)
query, and is allowed in
the SQL/92 standard, but SELECT [Link], [Link]
is not supported in some FROM Sailors S
systems. WHERE (SELECT MAX ([Link])
FROM Sailors S2)
= [Link]
Summary
There are many ways to write a given query in SQL.
Target-list syntax can include arithmetic, naming
Set operations: UNION, EXCEPT, INTERSECT
Nested queries:
– Uncorrelated: subquery can be executed once
– Correlated: subquery executed once per “outer” tuple
Set comparisons: semantics can be tricky
– It’s just first-order predicate logic (, , , , )
Aggregates: Useful, and not part of relational
algebra. 1