SQL Slides
SQL Slides
3
Example
Orders
Find if the customers "Hansen" or
OID OrderDate OrderPrice Customer
"Jensen“ have a total order of
1 2008/11/12 1000 Hansen more than 1500
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
FROM
R S
That’s not so bad, is it? 5
Other aspects of SQL
SELECT-FROM-WHERE QUERIES
‘SELECT’ clause
• Identifies which attribute(s) query returns
− Comma-separated list
=> Determines schema of query result
• (Optional) extended projection
− Compute arbitrary expressions
− Usually based on selected attributes, but not always
• (Optional) rename attributes
− “Prettify” column names for output
− Disambiguate (E1.name vs. E2.name)
• (Optional) specify groupings
− More on this later
• (Optional) duplicate elimination
− SELECT DISTINCT …
8
‘SELECT’ clause – examples
• SELECT E.name …
=> Explicit attribute
• SELECT name …
=> Implicit attribute (error if R.name and S.name exist)
• SELECT E.name AS ‘Employee name’ …
=> Prettified for output (like table renaming, ‘AS’ usually not required)
• SELECT sum(S.value) …
=> Grouping (compute sum)
• SELECT sum(S.value)*0.13 ‘HST’ …
=> Scalar expression based on aggregate
• SELECT * …
=> Select all attributes (no projection)
• SELECT E.* …
=> Select all attributes from E (no projection)
9
‘FROM’ clause
10
‘FROM’ clause – examples
• … FROM Employees
=> Explicit relation
• … FROM Employees AS E
=> Table alias (most systems don’t require “AS” keyword)
• … FROM Employees, Sales
=> Cartesian product
• … FROM Employees E JOIN Sales S
=> Cartesian product (no join condition given!)
• … FROM Employees E JOIN Sales S ON
E.EID=S.EID
=> Equi-join
11
‘FROM’ clause – examples (cont)
12
Gotcha: natural join in practice
15
‘WHERE’ clause
16
Scalar expressions in SQL
18
Pattern matching
20
21
22
‘GROUP BY’ clause – gotchas
• WHERE clause cannot reference aggregated values (sum,
count, etc.)
− Aggregates don’t “exist yet” when WHERE runs
=> Use HAVING clause instead (coming next)
• GROUP BY must list all non-aggregate attributes used in
SELECT clause
− Think projection
=> Some systems do this implicitly, others throw error
• Grouping often (but not always!) sorts on grouping key
− Depends on system and/or optimizer decisions
=> Use ORDER BY to be sure (coming next)
23
‘GROUP BY’ clause – examples
24
‘GROUP BY’ clause – examples (cont)
25
Eliminating duplicates in aggregation
26
‘HAVING’ clause
In tree form:
• Allows predicates on
aggregate values ORDER BY t
− Groups which do not match
the predicate are eliminated
=> HAVING is to groups what
SELECT p
WHERE is to tuples
• Order of execution HAVING s
− WHERE is before GROUP
G
BY
=> Aggregates not yet available GROUP BY
when WHERE clause runs
data flow
− GROUP BY is before
HAVING WHERE s
=> Scalar attributes still
available
FROM
R S 27
‘HAVING’ clause – examples
• SELECT EID, SUM(value)
FROM Sales GROUP BY EID
HAVING SUM(Sales.value) > 10000
− Highlight employees with “impressive” sales
28
‘ORDER BY’ clause
• … ORDER BY E.name
=> Defaults to ascending order
• … ORDER BY E.name ASC
=> Explicitly ascending order
• … ORDER BY E.name DESC
=> Explicitly descending order
• … ORDER BY CarCount DESC, CarName ASC
=> Matches our car example from previous lecture
• SELECT E.name … ORDER BY 1
=> Specify attribute’s position instead of its name
30
What’s next?
• Examples
31
32
WORKING EXAMPLES
Example Database
Employee(FirstName,Surname,Dept,Office,Salary,City)
Department(DeptName,Address,City) Home city
EMPLOYEE FirstName Surname Dept Office Salary City
Mary Brown Administration 10 45 London
Charles White Production 20 36 Toulouse
Gus Green Administration 20 40 Oxford
Jackson Neri Distribution 16 45 Dover
Charles Brown Planning 14 80 London
Laurence Chen Planning 7 73 Worthing
Pauline Bradshaw Administration 75 40 Brighton
Alice Jackson Production 20 46 Toulouse
Result: Remuneration
45
80
Example: * in the Target List
Employee(FirstName, Surname, Dept, Office, Salary, City)
Department(DeptName, Address, City)
Result:
FirstName Surname Dept Office Salary City
Mary Brown Administration 10 45 London
Charles Brown Planning 14 80 London
Example: Attribute Expressions
Employee(FirstName, Surname, Dept, Office, Salary, City)
Department(DeptName, Address, City)
Result: MonthlySalary
3.00
Example: Simple (Equi-)Join Query
Employee(FirstName, Surname, Dept, Office, Salary, City)
Department(DeptName, Address, City)
Result: FirstName
Mary
Example: String Matching Operator LIKE
Employee(FirstName, Surname, Dept, Office, Salary, City)
Department(DeptName, Address, City)
Result:
FirstName Surname Dept Office Salary City
Mary Brown Administration 10 45 London
Gus Green Administration 20 40 Oxford
Charles Brown Planning 14 80 London
Example: Aggregate Queries: Operator Count
Employee(FirstName, Surname, Dept, Office, Salary, City)
Department(DeptName, Address, City)
"Find the number of different values on attribute Salary for all tuples in
Employee":
SELECT count(DISTINCT Salary) FROM Employee
Result: SumSalary
125
Example: Operators Sum, Avg, Max and Min
Employee(FirstName, Surname, Dept, Office, Salary, City)
Department(DeptName, Address, City)
Result:
MaxSal MinSal
80 36
Example: Aggregate Operators with Join
Employee(FirstName, Surname, Dept, Office, Salary, City)
Department(DeptName, Address, City)
Result: MaxLondonSal
80
47
50
GROUP BY in practice (cont.)
Incorrect query:
SELECT Office
FROM Employee
GROUP BY Dept
Incorrect query:
SELECT DeptName, D.City, count(*)
FROM Employee E JOIN Department D ON (E.Dept = D.DeptName)
GROUP BY DeptName
Correct query:
SELECT DeptName, D.City, count(*)
FROM Employee E JOIN Department D ON (E.Dept = D.DeptName)
GROUP BY DeptName, D.City
51
Example: HAVING
Employee(FirstName, Surname, Dept, Office, Salary, City)
Department(DeptName, Address, City)
Result: Dept
Administration
Planning
HAVING in practice
53
54
EXERCISE
Exercise
Professor(Id, Name, DeptId)
Course(CrsCode, DeptId, CrsName, Description)
Teaching(ProfId, CrsCode, Semester)
Note: Values for Semester are YYYY (F | S | W), e.g., ‘2018F', ‘2019W‘
Questions:
• “Find the names of all professors who taught in Fall 2018”
• “Find the names of all courses taught in Fall 2018, together with the
names of professors who taught them”
• “Find the average number of courses taught by professors in Comp. Sc.
(CS)”
• “Find the number of courses taught by each professor in Comp. Sc. (CS)”
• “Find the number of courses taught by each professor in Comp. Sc. (CS)
in 2018”
55
Answers
Professor(Id, Name, DeptId)
Course(CrsCode, DeptId, CrsName, Description)
Teaching(ProfId, CrsCode, Semester)
SELECT P.Name
FROM Professor P, Teaching T
WHERE P.Id=T.ProfId AND T.Semester=‘2018F'
“Find the names of all courses taught in Fall 2018, together with the
names of professors who taught them”
“Find the number of courses taught by each professor in Comp. Sc. (CS)”
SELECT T.ProfId, count(*)
FROM Teaching T, Course C
WHERE T.CrsCode=C.CrsCode AND C.DeptId='CS'
GROUP BY ProfId
57
Answers (cont.)
Professor(Id, Name, DeptId)
Course(CrsCode, DeptId, CrsName, Description)
Teaching(ProfId, CrsCode, Semester)
"Find the number of courses taught by each professor in Comp. Sc. (CS)
in 2018"
58
59
OTHER CONCEPTS
NULL values in SQL
60
Effect of NULL in expressions
61
Effects of NULL on grouping
R x
• Short version: complicated
− Usually, “not present”
• COUNT 1
− COUNT(R.*) = 2 COUNT(R.x) = 1
− COUNT(S.*) = 1 COUNT(S.x) = 0
S x
− COUNT(T.*) = 0 COUNT(T.x) = 0
• Other aggregations (e.g. MIN/MAX)
− MIN(R.x) = 1 MAX(R.x) = 1
− MIN(S.x) = NULL MAX(S.x) = NULL
− MIN(T.x) = NULL MAX(T.x) = NULL
T x
62
SET Queries: Union, Intersection, Difference
63
Example: Union
64
Example: Intersection
equivalent to:
SELECT E1.FirstName AS Name
FROM Employee E1, Employee E2
WHERE E1.FirstName = E2.Surname
65
Example: Difference
66
Nested queries
p R s
vs.
p
R S
S T 67
Nested queries – uses
• Explicit join ordering
− FROM (A join B) is a (very simple) query to run first
• Input relation for a set operation
− Union, intersect, difference
• Input relation for a larger query
− Appears in FROM clause
− Usually joined with other tables (or other nested queries)
=> FROM A, (SELECT …) B WHERE …
=> Explicit join ordering is a degenerate case
68
Nested queries – more uses
• Conditional relation expression
− Dynamic list for [NOT] IN operator
=> WHERE (E.id,S.name)
IN (SELECT id,name FROM …)
− Special [NOT] EXISTS operator
=> WHERE NOT EXISTS (SELECT * FROM …)
• Scalar expression
− Must return single tuple (usually containing a single attribute)
=> 0.13*(SELECT sum(value)
FROM Sales WHERE taxable)
=> S.value > (SELECT average(S.value)
FROM Sales S)
69
List comparisons: ANY, ALL, [NOT] IN
SELECT FirstName,Surname
FROM Employee
WHERE Dept = ‘Plan’ AND FirstName = ANY (
SELECT FirstName
FROM Employee
WHERE Dept = ‘Prod’)
equivalent to:
SELECT E1.FirstName,E1.Surname
FROM Employee E1, Employee E2
WHERE E1.FirstName=E2.FirstName
AND E2.Dept=‘Prod’ AND E1.Dept=‘Plan’
72
Example: Negation with Nested Query
SELECT DeptName
FROM Department
WHERE DeptName <> ALL (
SELECT Dept FROM Employee WHERE Surname = ‘Brown’)
equivalent to:
SELECT DeptName FROM Department
EXCEPT
SELECT Dept FROM Employee WHERE Surname = ‘Brown’
73
Operators IN and NOT IN
• Operator IN is a shorthand for = ANY
SELECT FirstName, Surname
FROM Employee
WHERE Dept IN (
SELECT DeptName FROM Department WHERE City = ‘London’)
74
max, min as Nested Queries
“Find the department of the employee earning the highest
salary”
with max:
SELECT Dept FROM Employee
WHERE Salary IN (SELECT max(Salary) FROM Employee)
without max:
SELECT Dept FROM Employee
WHERE Salary >= ALL (SELECT Salary FROM Employee)
75
Operator: [NOT] EXISTS
“Find all persons who have the same first name and surname
with someone else (synonymous folks) but different tax codes”
SELECT * FROM Person P1
WHERE EXISTS (
SELECT * FROM Person P2
WHERE P2.FirstName = P1.FirstName
AND P2.Surname = P1.Surname
AND P2.TaxCode <> P1.TaxCode)
76
Operator: [NOT] EXISTS (cont.)
77
Tuple Constructors
78
Comments on Nested Queries
• Use of nesting
(-) may produce less declarative queries
(+) often results in improved readability
• Complex queries can become very difficult to understand
• The use of variables must respect scoping conventions:
− a variable can be used only within the query where it is
defined, OR
− within a query that is recursively nested within the query
where it is defined
79
What’s next?
80