L4 SQL
L4 SQL
1
Introduction
• Structured Query Language (SQL) is the most widely used
commercial relational database language.
• It was originally developed at IBM in the SEQUEL-XRM
and System-R projects (1974-1977).
• Almost immediately, other vendors introduced DBMS
products based on SQL, and it is now a de facto standard.
• The SQL continues to evolve in response to the changing
need.
2
• The SQL language has several aspects to it:
– The Data Manipulation Language (DML)
• The subset of SQL allows users to pose queries and
to insert, delete, and modify rows.
– The Data Definition Language (DDL)
• The subset of SQL supports the creation, deletion,
and modification to definitions for tables and views.
– Triggers and Advanced Integrity Constraints
• The feature was introduced in SQL:1999. The
standard includes support for triggers, which are
action executed by the DBMS whenever changes to
the database meet conditions specified in the trigger
3
– Embedded and Dynamic SQL
• Embedded SQL features allow SQL code to be
called from a host language such as C or COBOL.
• Dynamic SQL features allow a query to be
constructed and executed at run-time.
– Client-Server Execution and Remote Database
Access
• Client application can connect to an SQL server.
• Access data from a database over a network.
– Transaction Management
• Control how transaction are executed.
– Security
• Provides mechanisms to control users’ access to
data.
4
Basic SQL Query
5
Basic SQL Query
6
Basic SQL Query
7
Basic SQL Query
Comparisons
• [Attr op const] or [Attr1 op Attr2],
where op is one of <, >, =, ≤, ≥, ≠
• combined using AND, OR and NOT.
8
Basic SQL Query
9
Basic SQL Query
• SELECT clause
– specifies columns to be retained in the result.
• FROM clause
– specifies a cross-product of tables.
• WHERE clause (optional)
– specifies selection conditions on the tables mentioned in the
FROM clause.
10
SELECT DISTINCT a1, a2, …, an
FROM R1, R2, …, Rm
WHERE P
a1, a2, …, an ( P ( R1 x R2 x … x Rm ) )
11
• Example: Find the names of all branches in
the loan relation.
SELECT branch-name
FROM Loan
Loan Result
branch_name loan_number amount branch_name
CUHK 222 2 CUHK
CMTR 333 1 CMTR
CUHK 777 2 CUHK
12
• To remove duplications
Loan Result
branch_name loan_number amount branch_name
CUHK 222 2 CUHK
CMTR 333 1 CMTR
CUHK 777 2
13
• Conceptual Evaluation Strategy
Compute the cross-product of relation-list.
Discard resulting tuples if they fail
qualifications.
Delete attributes that are not in target-list.
If DISTINCT is specified, eliminate duplicate
rows.
(This strategy is probably the least efficient way to
compute a query! An optimizer will find more efficient
strategies to compute the same answers.)
14
• Example (Q1, p.137): Find the names of sailors
who have reserved boat number 103.
15
sid sname rating age
sid bid day
22 dustin 7 45.0
22 101 10/10/19
31 lubber 8 55.5
58 103 11/12/19
58 rusty 10 35.0
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND R.bid=103
18
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
SELECT S.sname
FROM Sailors S, Reserves R, Boat B
WHERE S.sid = R.sid AND R.bid = B.bid AND
B.color = ‘red’
19
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
SELECT B.color
FROM Sailors S, Reserves R, Boat B
WHERE S.sid = R.sid AND R.bid = B.bid AND
S.sname = ‘Lubber’.
20
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
21
Expressions and Strings
SELECT S.age, age1=S.age-5, 2*S.age AS age2
FROM Sailors S
WHERE S.sname LIKE ‘B_%B’
22
Union, Intersect, and Except
• SQL provides three set-manipulation
constructs that extend the basic query form
presented earlier.
– Union ()
– Intersection ()
– Except ()
(many systems recognize the keyword MINUS for EXCEPT)
23
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
24
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
Example: Find sid’s of all sailors who’ve reserved red boat but
not green boat.
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
Indeed, since the Reserves
WHERE S.sid=R.sid AND R.bid=B.bid
relation contains sid information, AND B.color=‘red’
there is no need to look at the EXCEPT
Sailors relation. SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid
AND B.color=‘green’
SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color=‘red’
EXCEPT
SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color=‘green’
26
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
SELECT S.sid
FROM Sailor S
WHERE S.rating = 10
UNION
SELECT R.sid
FROM Reserves R
WHERE R.bid=104
27
SQL IN operator
• The IN operator allows you to specify
multiple values in a WHERE clause
Example: Find the names of boats which are red, blue,
or green.
SELECT B.bname
FROM Boats B
WHERE B.color IN (‘red’, ‘blue’, ’green’)
29
Example: Find names of sailors who’ve reserved boat #103:
SELECT S.sname Note that this query can
FROM Sailors S be implemented easily
WHERE S.sid IN (SELECT R.sid without using nested
FROM Reserves R structure.
WHERE R.bid=103)
31
Example: Find names of sailors who’ve reserved
boat #103:
SELECT S.sname
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid=103 AND S.sid=R.sid)
SELECT *
FROM Sailors S
WHERE S.rating > ANY (SELECT S2.rating
FROM Sailors S2
WHERE S2.sname=‘Horatio’)
33
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
SELECT *
FROM Sailors S
WHERE S.rating > ALL (SELECT S2.rating
FROM Sailors S2
WHERE S2.sname=‘Horatio’)
SELECT *
FROM Sailors S
WHERE S.rating >= ALL (SELECT S2.rating
FROM Sailors S2)
34
• Rewriting INTERSECT queries using IN
Example: Find sid’s of sailors who’ve reserved both a red and a
green boat:
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’
AND S.sid IN (SELECT S2.sid
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid=R2.sid AND R2.bid=B2.bid
AND B2.color=‘green’)
36
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
37
Aggregate Operators
• SQL allows the use of arithmetic expressions.
• SQL supports five aggregate operations, which
can be applied on any column of a relation.
COUNT([DISTINCT] A) The number of (unique) value in the A
column.
SUM ( [DISTINCT] A) The sum of all (unique) values in the A
column.
AVG ([DISTINCT] A) The average of all (unique) values in the A
column.
MAX (A) The maximum value in the A column.
38
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
39
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
41
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
42
Group by and Having
• So far, we’ve applied aggregate operators to all (qualifying)
tuples. Sometimes, we want to apply them to each of several
groups of tuples.
• Consider: Find the age of the youngest sailor for each rating
level.
– In general, we don’t know how many rating levels exist, and what the
rating values for these levels are!
– Suppose we know that rating values go from 1 to 10; we can write 10
queries that look like this (!):
43
• To write such queries, we need a major extension to the
basic SQL query form, namely the Group BY clause.
• The extension also includes an optional HAVING clause
that can be used to specify qualifications over groups.
44
The general format of GROUP BY and Having
46
Example: Find the age of the youngest sailor with age 18, for
each rating with at least 2 such sailors
sid sname rating age
SELECT S.rating, MIN (S.age) 22 dustin 7 45.0
FROM Sailors S 31 lubber 8 55.5
WHERE S.age >= 18 71 zorba 10 16.0
GROUP BY S.rating 64 horatio 7 35.0
HAVING COUNT (*) > 1
29 brutus 1 33.0
58 rusty 10 35.0
• Only S.rating and S.age are
mentioned in the SELECT, rating age rating
GROUP BY or HAVING clauses; 1 33.0 7 35.0
other attributes are 7 45.0
`unnecessary’. 7 35.0 Answer relation
• 2nd column of result is unnamed. 8 55.5
(Use AS to name it. See Q32 on 10 35.0
p. 155 of your textbook.)
47
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
For each red boat, find the number of reservations for this
boat
SELECT B.bid, COUNT (*) AS reservationcount Only columns that appear in the
GROUP BY clause can appear in
FROM Boats B, Reserves R
the HAVING clause, unless
WHERE R.bid=B.bid they appear as arguments to an
GROUP BY B.bid aggregate operator in the HAVING
HAVING B.color = ‘red’ clause.
48
Example: Find the average age of sailors for each rating level
that has at least two sailors.
sid sname rating age
SELECT S.rating, AVG(S.age) AS avgage 22 Dustin 7 45.0
FROM Sailor S
29 Brutus 1 33.0
GROUP BY S.rating
HAVING COUNT (*) > 1 31 Lubber 8 55.5
OR 32 Andy 8 25.5
SELECT S.rating, AVG(S.age) AS avgage 58 Rusty 10 35.0
FROM Sailor S 64 Horatio 7 35.0
GROUP BY S.rating
71 Zorba 10 16.0
HAVING 1 < (SELECT COUNT (*)
FROM Sailors S2 74 Horatio 9 35.0
WHERE S.rating = S2.rating) 85 Art 3 25.5
Rating avgage 95 Bob 3 63.5
We can use S.rating 96 Frodo 3 25.5
3 38.2 inside the nested
7 40.0 subquery in the HAVING
because it has a single Instance S3 of Sailor
8 40.5 value for the current group
10 25.5 of sailors (specified in the
GROUP BY clause). 49
Answer
Example: Find the average age of sailors who are at least 18
years old for each rating level that has at least two sailors.
sid sname rating age
SELECT S.rating, AVG(S.age) AS avgage
22 Dustin 7 45.0
FROM Sailor S
WHERE S.age >=18 29 Brutus 1 33.0
GROUP BY S.rating 31 Lubber 8 55.5
HAVING 1 < (SELECT COUNT (*)
32 Andy 8 25.5
FROM Sailors S2
WHERE S.rating = S2.rating) 58 Rusty 10 35.0
64 Horatio 7 35.0
71 Zorba 10 16.0
Rating avgage
Note that the answer is very 74 Horatio 9 35.0
similar to the previous one, with the 3 38.2
only difference being that for the 85 Art 3 25.5
7 40.0
group 10, we now ignore the 95 Bob 3 63.5
sailor with age 16 while computing 8 40.5
96 Frodo 3 25.5
the average. 10 35.0
Answer Instance S3 of Sailor
50
Example: Find the average age of sailors who are at least 18
years old for each rating level that has at least two such sailors.
sid sname rating age
SELECT S.rating, AVG(S.age) AS avgage
22 Dustin 7 45.0
FROM Sailor S
WHERE S.age >=18 29 Brutus 1 33.0
GROUP BY S.rating 31 Lubber 8 55.5
HAVING 1 < (SELECT COUNT (*)
32 Andy 8 25.5
FROM Sailors S2
WHERE S.rating = S2.rating AND 58 Rusty 10 35.0
S2.age >=18) 64 Horatio 7 35.0
71 Zorba 10 16.0
Rating avgage
It differs from the answer of the 74 Horatio 9 35.0
previous question in that there is no 3 38.2
tuple for rating 10, since there 85 Art 3 25.5
7 40.0
is only one tuple with rating 10 and 95 Bob 3 63.5
age >= 18. 8 40.5
96 Frodo 3 25.5
Answer
Instance S3 of Sailor
51
Some other ways to write the previous query.
52
sailors sid sname rating age
Boats bid bname color
Reserves sid bid day
SELECT S.rating
FROM Sailors S
WHERE S.age = (SELECT MIN (AVG (S2.age))
FROM Sailors S2
GROUP BY S2.rating)
53
Correct solution (in SQL/92):
54
NULL Value
• field value unknown
– A new employee has not been assigned a
supervisor yet.
55
Complications caused by NULL values
• Special operators provided to check if value is /
is not NULL
e.g. SELECT name, address
You cannot use comparison operators
FROM employee
(e.g. =, < , < >) to test for NULL values
WHERE address is NOT NULL
57
General Constraints
58
• Constraints can be named.
• When a record is inserted into Reserves or an existing
row is modified, the conditional expression in the
CHECK constraint is evaluated. If it evaluates to
false, the command is rejected.
60
Distinct Type
CREATE TYPE ratingtype AS INTEGER
61
Constraints Over Multiple Relations
CREATE TABLE Sailors
( sid INTEGER, Number of boats
sname CHAR(10), plus number of
rating INTEGER, sailors is < 100
age REAL,
PRIMARY KEY (sid),
CHECK
( (SELECT COUNT (S.sid) FROM Sailors S)
+ (SELECT COUNT (B.bid) FROM Boats B) < 100 )
62
• ASSERTION is the right solution; not associated
with either table.
63
Triggers
• Trigger: procedure that starts automatically if
specified changes occur to the DBMS
• Three parts:
– Event (activates the trigger)
– Condition (tests whether the triggers should run)
– Action (what happens if the trigger runs)
64
Triggers: Example (SQL:1999)
65
Appendix: Natural Join
• Note that some systems may support natural join.
This query
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND R.bid=103
can be rewritten as
SELECT S.sname
FROM Sailors S natural join Reserves R
WHERE R.bid=103
• Some systems also support inner join, left join,
right join and full join.
66
Appendix: Create view
• A view is a virtual table based on the result
of an SQL statement
CREATE VIEW Temp AS
SELECT S.rating, AVG (S.age) AS avgage
FROM Sailors S
GROUP BY S.rating
68