SQL: Data Manipulation Language
CSC 343
Winter 2021
MICHAEL LIUT (MICHAEL.LIUT@UTORONTO.CA) ILIR DEMA (ILIR.DEMA@UTORONTO.CA)
DEPARTMENT OF MATHEMATICAL AND COMPUTATIONAL SCIENCES UNIVERSITY OF TORONTO MISSISSAUGA
Why SQL?
SQL is a very-high-level language.
◦ Structured Query Language
◦ Say “what to do” rather than “how to do it.”
◦ Avoid a lot of data-manipulation details needed in procedural languages like C++ or Java.
DBMSs determine the “best” method of executing a query. ◦ This is called “query optimization”.
2
Database Schemas in SQL
SQL is primarily a query language, used for retrieving data from a database. ◦ Data Manipulation Language (DML)
But SQL also includes a data-definition component for describing database schemas.
◦ Data Definition Language (DDL)
3
Select-From-Where Statements
SELECTàdesired attribute(s)
FROMàone or more tables (i.e. entity-sets) WHEREàcondition about tuples of the tables
4
Recall our Example
Our SQL queries will be based on the following database schema. ◦ Underlines indicates key attributes.
Beers (name, manf)
Bars (name, addr, license) Drinkers (name, addr, phone) Likes (drinker, beer)
Sells (bar, beer, price) Frequents (drinker, bar)
5
Example
Using Beers(name, manf), what beers are made by Anheuser-Busch? LECT name
SELECT name
FROM Beers
WHERE manf = ‘Anheuser-Busch’;
6
Result of Query
name
Bud
Bud Light
Michelob
…
The answer is a relation with a single attribute, name, and tuples with the name of each beer by Anheuser-Busch, such as Bud.
7
Meaning of Single-Relation Query
Begin with the relation in the FROM clause.
Apply the selection indicated by the WHERE clause.
Apply the extended projection indicated by the SELECT clause.
8
Operational Semantics – General
Think of a tuple variable visiting each tuple of the relation mentioned in FROM.
Check if the tuple assigned to the tuple variable satisfies the WHERE clause.
If so, compute the attributes or expressions of the SELECT clause using the components of this tuple.
9
Operational Semantics
name
If so, include t.name in the result
Tuple-variable t loops over all tuples
Bud
manf
Anheauser-Busch
Check if Anheuser- Busch
10
Example
What beers are made by Anheuser-Busch?
OR
NOTE: these two queries are identical.
SELECT name
FROM Beers
WHERE manf = ‘Anheuser- Busch’;
SELECT t.name
FROM Beers t
WHERE t.manf = ‘Anheuser- Busch’;
11
Asterisks ( * ) in Select Clauses
When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation”.
Example: Using Beers(name, manf): SELECT *
FROM Beers
WHERE manf = ‘Anheuser-Busch’;
12
Result of Query
name
manf
Bud
Anheuser-Busch
Bud Light
Anheuser-Busch
Michelob
Anheuser-Busch
…
…
The result now has each of the attributes of Beers.
13
Renaming Attributes
If you want the result to have different attribute names, use “AS
Example: Using Beers(name, manf): SELECT name AS beer, manf
FROM Beers
WHERE manf = ‘Anheuser-Busch’;
14
Result of Query
beer
manf
Bud
Anheuser-Busch
Bud Light
Anheuser-Busch
Michelob
Anheuser-Busch
…
…
The result now has each of the attributes of Beers.
15
Expressions in SELECT Clauses
Any valid expression can appear as an element of a SELECT clause.
EXAMPLE: Using Sells(bar, beer, price):
SELECT bar, beer, price*80 AS priceInJPY
FROM Sells;
16
Result of Query
bar
beer
priceInJPY
Joe’s
Bud
285
Sue’s
Miller
342
John’s
Michelob
532
…
…
…
The result now has each of the attributes of Beers.
17
Example: Constants as Expressions
Using Likes(drinker, beer):
SELECT drinker, ‘likes Bud’ AS whoLikesBud
FROM Likes
WHERE beer = ‘Bud’;
18
Result of Query
drinker
whoLikesBud
George
likes Bud
Frank
likes Bud
Jenny
likes Bud
…
…
19
Complex Conditions in WHERE clause
Boolean operators: AND, OR, NOT. Comparison operators: =, <>, <, >, <=, >=.
20
Example: Complex Condition
Using Sells(bar, beer, price), find the price that Joe’s Bar charges for Bud.
SELECT price
FROM Sells
WHERE bar = ‘Joe“s Bar’ AND beer = ‘Bud’;
21
Patterns
A condition can compare a string to a pattern by:
◦
◦ __ = “any character”.
22
Example: Like
Using Drinkers(name, addr, phone) find the drinkers with exchange 555:
SELECT name
FROM DRINKERS
WHERE phone LIKE ‘%555-_ _ _ _’;
23
NULL Values
Tuples in SQL relations can have NULL as a value for one or more components.
The meaning is dependent on the context. In general, there are two common cases:
1. Missing Values: e.g., we know Joe’s Bar has some address, but we don’t know what it is.
2. Inapplicable: e.g., the value of attribute spouse for an unmarried person.
24
Comparing NULL’s to Values
The logic of a conditions in SQL are 3 pronged:
1. TRUE
2. FALSE
3. UNKNOWN
◦ Comparing any value (including NULL) with NULL yields UNKNOWN.
A tuple is in a query answer iff the WHERE clause is TRUE (not FALSE or UNKNOWN).
25
RECALL
EXAMPLE: Using Sells(bar, beer, price):
SELECT bar, beer, price*80 AS priceInJPY FROM Sells;
26
Result of Query
bar
beer
priceInJPY
Joe’s
Bud
285
Sue’s
Miller
342
John’s
Michelob
532
…
…
…
27
String Functions in SELECT Clauses
EXAMPLE: Using Sells(bar, beer, price):
SELECT bar, INSERT(beer, 1, 0, ‘EH ’) AS ‘Canadian beer’, price FROM Sells;
28
Result of Query
bar
Canadian beer
price
Joe’s
EH Bud
4
Sue’s
EH Miller
5
John’s
EH Michelob
7
…
…
…
The result reflects the renamed beers to reflect their inner Canadian.
29
Three-Valued Logic
To understand how AND, OR, and NOT work in 3- valued logic For TRUE results:
◦ OR: at least one operand must be TRUE. ◦ AND: both operands must be TRUE.
◦ NOT: operand must be FALSE.
30
Three-Valued Logic
To understand how AND, OR, and NOT work in 3- valued logic For FALSE results:
◦ OR: both operands must be FALSE.
◦ AND: at least one operand must be FALSE. ◦ NOT: operand must be TRUE.
31
Three-Valued Logic
To understand how AND, OR, and NOT work in 3- valued logic
Otherwise
◦ The result is UNKNOWN.
32
Example
From the following Sells relation:
bar
beer
price
Joe’s Bar
Bud
NULL
…
…
…
SELECT bar
FROM Sells
WHERE price < 2.00 OR price >= 5.00;
UNKNOWN
UNKNOWN
UNKNOWN
33
Multi-Relation Queries
Usually a combination of data from multiple (i.e. more than one) relation. ◦ More often than not, these are interesting queries.
We can address several relations in one query by listing them all in the FROM clause.
Distinguish attributes of the same name by “
34
Example: Joining Two Relations
Using relations: Likes(drinker, beer) and Frequents(drinker, bar), find the beers liked by at least one person who frequents Joe’s Bar.
SELECT beer
FROM Likes, Frequents
WHERE bar = ‘Joe”s Bar’ AND Frequents.drinker = Likes.drinker;
35
Example: Joining Two Relations
Alternatively, previously shown on slide #11, we can use explicit (named) tuple variables.
SELECT beer
FROM Likes l, Frequents f
WHERE bar = ‘Joe“s Bar’ AND f.drinker = l.drinker;
36
Formal Semantics
Almost the same for single-relation queries:
◦ Start with the product of all the relations in the FROM clause.
◦ Apply the selection condition from the WHERE clause.
◦ Project onto the list of attributes and expressions in the SELECT clause.
37
Operational Semantics
Imagine one tuple-variable for each relation in the FROM clause.
◦ These tuple-variables visit each combination of tuples, one from each
relation.
If the tuple-variables are pointing to tuples that satisfy the WHERE clause, send these tuples to the SELECT clause.
38
Operational Semantics
check for Joe
Tuple- variable t1
Tuple- variable t2
drinker
beer
Sally
Bud
drinker
bar
Sally
Joe’s
to output
check these are equal
39
Explicit Tuple-Variables
Sometimes a query needs to use two copies of the same relation.
Copies are distinguished by following the relation name by the name of a tuple-variable, in the FROM clause.
It’s always an option to rename relations this way, even when not essential.
40
Example: Self-Join
From Beers(name, manf), find all pairs of beers by the same manufacturer. ◦ Do not produce pairs like (Bud, Bud).
◦ Do not produce the same pairs twice (Bud, Miller) and (Miller, Bud).
SELECT b1.name, b2.name
FROM Beers b1, Beers b2
WHERE b1.manf = b2.manf AND b1.name < b2.name;
41
Sub-queries
A parenthesized SELECT-FROM-WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses.
Example: in place of a relation in the FROM clause, we can use a subquery and then query its result.
◦ To do this we must use a tuple-variable to name tuples of the result.
42
Example: Sub-query in FROM
Find the beers liked by at least one person who frequents Joe’s Bar.
SELECT beer FROM Likes,
WHERE Likes.drinkers = JD.drinker;
Drinkers who frequent Joe’s Bar
(SELECT drinker
FROM Frequents
WHERE bar = ‘Joe``s Bar’) JD
43
Sub-queries Often Obscure Queries
Find the beers liked by at least one person who frequents Joe’s Bar.
SELECT beer
FROM Likes l, Frequents f
WHERE l.drinker = f.drinker AND bar = ‘Joe``s Bar’;
Simple Join Query
44
Sub-Queries That Return One Tuple
If a sub-query is guaranteed to produce one tuple, then the sub-query can be used as a value.
◦ Usually, the tuple has one component. ◦ Remember SQL’s 3-valued logic.
45
Example: Single-Tuple Sub-query
Using Sells(bar, beer, price), find the bars that serve Miller for the same price Joe charges for Bud.
Two queries would work:
◦ Find the price Joe charges for Bud.
◦ Find the bars that serve Miller at that price.
46
Query + Sub-Query Solution
Sells(bar, beer, price)
SELECT bar
FROM Sells
WHERE beer = ‘Miller’ AND price =
• Find the price Joe charges for Bud.
• Find the bars that serve Miller at that price.
The price at which Joe sells Bud
(SELECT price
FROM Sells
WHERE bar = ‘Joe``s Bar’ AND
beer = ‘Bud’);
47
Query + Sub-Query Solution
SELECT bar • Find the price Joe charges for Bud.
FROM Sells • Find the bars that serve Miller at that price.
WHERE beer = ‘Miller’ AND price = Sells(bar, beer, price)
Questions:
1. What if the price of Bud is NULL? 2. What if the sub-query returns
multiple values?
(SELECT price
FROM Sells
WHERE bar = ‘Joe``s Bar’ AND
beer = ‘Bud’);
The price at which Joe sells Bud
48
Temporary Tables
Yes, they do exists! MySQL allows them!
Note: a declared temporary table must be declared!
◦ CREATE TEMPORARY TABLE
◦ WHERE/FROM clauses, WITH statements, and survive for the duration a single session.
◦ Single session must be created in a user temporary space.
A few useful links for further investigation:
◦ https://dev.mysql.com/doc/refman/5.7/en/create-temporary-table.html
49
Recap: Conditions in WHERE Clause
Boolean operators: AND, OR, NOT. Comparisons: =, <>, <, >, <=, >=. LIKE operator.
SQL includes a BETWEEN comparison operator too. ◦ Let’s see an example!
50
Example: Between
Find the names of all Instructors with salary BETWEEN $90,000 and $100,000.
◦ i.e. >= $90,000 and <= $100,000.
SELECT name
FROM Instructor
WHERE salary BETWEEN 90000 AND 100000;
51
The Operator: ANY
X = ANY(
◦ = could be any comparison operator.
Example: x >= ANY(
produced by the sub-query.
◦ NOTE: tuples must have one component only.
52
The Operator: ALL
x <> ALL (
<> can be any comparison operator.
Example: x >= ALL (
53
Example: ALL
From Sells(bar, beer, price), find the beer(s) sold at the highest price. SELECT beer
FROM Sells WHERE price >=
The price from the outer Sells must not be less than any price.
ALL(
SELECT price
FROM Sells );
54
The Operator: IN
◦ Opposite:
55
“IN” is Concise
SELECT *
FROM Cartoons WHERE LastName
SELECT *
VS. FROM Cartoons
IN (‘Jetsons’, ‘Smurfs’, ‘Flinstones’);
WHERE LastName = ‘Jetsons’ OR LastName = ‘Smurfs’
OR LastName = ‘Flinstones’;
56
Example: IN
Using Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes.
SELECT * FROM Beers WHERE name
The set of beers that Fred likes.
IN (SELECT beer
FROM Likes
WHERE drinker = ‘Fred’);
57
Another Example:
Operator “IN” and “NOT IN”
Using IN and NOT IN operators with a Multiple-Row Sub-query:
◦ http://www.w3resource.com/sql/subqueries/multiplee-row-column-subqueries.php
58
IN vs. Join
SELECT R.a
FROM R, S WHERE R.b = S.b;
VS.
SELECT R.a
FROM R
WHERE b IN (SELECT b FROM S);
Note: IN and JOIN are different queries that can yield different results. Unless S.b is unique!
59
IN vs. Join
Equivalent to:
SELECT R.a
FROM R, S
JOIN (SELECT DISTINCT b
FROM S )
ON R.b = S.b;
Performance Note: if the joining column is not UNIQUE then IN is faster than JOIN on DISTINCT.
60
IN is a Predicate About R’s Tuples
SELECT R.a
FROM R
WHERE b IN (SELECT b FROM S);
RS
Two 2’s.
(1,2) satisfies the condition; 1 is output once.
One loop, over the tuples of R.
a
b
1
2
3
4
b
c
2
5
2
6
61
This Query Pairs Tuples from R, S
SELECT R.a
FROM R, S WHERE R.b = S.b;
RS
(1,2) with (2,5) and (1,2) with (2,6) both satisfy the condition; 1 is output twice.
Double loop, over the tuples of R and S.
a
b
1
2
3
4
b
c
2
5
2
6
62
Recall: Original Query
SELECT bar
FROM Sells
WHERE beer = ‘Miller’ AND
price = (SELECT price FROM Sells
Option 1: use IN
Option 2: use = ANY()
WHERE beer = ‘Bud’);
63
RECAP
The IN() operator is equivalent to = ANY().
For ANY(), you can use other comparison operators such as: ◦ >, <, etc... but this is NOT applicable for the IN() operator.
◦ The ability to use these operators can give you more control in your query depending on your desired output/result.
64
RECAP
NOTE: the <> ANY operator differs from NOT IN:
1. 2. 3.
<> ANY means != (a.k.a. does not equal) any… ◦ e.g. <> ANY means != a or != b or != c …
NOT IN means != (a.k.a. does not equal) any… ◦ e.g. != a and != b and != c …
<> ALL means the same as NOT IN.
65
Example: = ANY()
Sells
SELECT bar
FROM Sells
WHERE beer = ‘Miller’ AND price =
ANY(SELECT price FROM Sells
WHERE beer = ‘Bud’);
bar
beer
price
Jane
Miller
3.00
Joe
Miller
4.00
Joe
Bud
3.00
Jack
Bud
4.00
Tom
Miller
4.50
bar
Jane
Joe
Result
66
The Operator: Exists
Exists (
Example: From Beers(name, manf), find the beers that are unique (i.e. only) beer made by their respective manufacturer.
67
Example: Exists
SELECT name
FROM Beers b1 WHERE NOT EXISTS (
Set of beers with the same manf as b1, but not the same beer.
Notice the scope rule:
Manf refers to the closest nested FROM with a relation having that attribute.
NOTE: Some DBMSs consider this to be ambiguous.
SELECT *
FROM Beers
WHERE manf = b1.manf AND name <> b1.name
);
Notice the SQL “not equals” operator.
68
Union, Intersection, and Difference
Union, intersection, and difference of relations are expressed by the following forms, each involving sub-queries:
◦ (
◦ (
69
Union, Intersection, and Difference
70
Example: Intersection
Using Likes(drinker, beer) , Sells(bar, beer, price), and Frequents(drinker, bar), find the drinkers and beers such that:
1. The drinker likes the beer, and
2. The drinker frequents at least one bar that sells the beer.
71
Example: Intersection – Solution
INTERSECT
The sub-query is really a stored table!
SELECT * FROM Likes
The drinker frequents a bar that sells the beer.
(SELECT drinker, beer
FROM Sells, Frequents
WHERE Frequents.bar = Sells.bar );
72
Ordering the Display of Tuples
List in alphabetical order the names of all instructors SELECT attribute
FROM Table
ORDER BY attribute
We may specify desc for descending order or asc for ascending order, for each attribute.
Note: Ascending order is the default.
73
Example: Order By
List the names of all instructors in descending alphabetical.
SELECT name
FROM Instructor vs. FROM Instructor
SELECT name
ORDER BY name;
ORDER BY name desc;
74
Bag Semantics
A bag (or multi-set) is like a set, but an element may appear more than once.
Example: {1,2,1,3} is a bag.
Example: {1,2,3} is also a bag that happens to be a set.
75
Bag (multi-Set Semantics)
SQL primarily uses bag semantics.
The SELECT-FROM-WHERE statement uses bag semantics.
◦ Originally for efficiency reasons.
The default for union, intersection, and difference is set semantics. ◦ That is, duplicates are eliminated as the operation is applied.
76
Motivation: Efficiency
When doing projection, it is easier to avoid eliminating duplicates. ◦ Work on one tuple-at-a-time.
For intersection or difference, it is most efficient to sort the relations first. ◦ At that point you may as well eliminate the duplicates anyway.
77
Control Duplicate Elimination
Force the result to be a set by SELECT DISTINCT…
Force the results to be a bag (i.e. don’t eliminate duplicates) by the ALL operator
i.e. … UNION ALL …
78
Example: DISTINCT
From Sells(bar, beer, price), find all the different prices charges for beers: SELECT DISTINCT price
FROM Sells;
Notice that without DISTINCT, each price would be listed as many times as there were bar/beer pairs at that price.
79
Example: ALL
Using the relations: Likes(drinker, beer) and Frequents(drinker, bar):
◦ List drinkers who frequent more bars than they like beers, and do so as
many times as the difference of those counts.
(SELECT drinker FROM Frequents) EXCEPT ALL
(SELECT drinker FROM Likes);
80
Let’s Add Some HUMOUR!
A SQL query walks into a bar, and approaches two tables. The SQL query proceeds to ask “can I join you?”
81
Database Modifications
A modification command does not return a result (as a query does), but changes the database in some way.
Three types of modifications are:
1. Insert à inserting a tuple or tuples.
2. Delete à deleting a tuple or tuples.
3. Update à updating the value(s) of an existing tuple or tuples.
82
Insertion
To insert a single tuple:
INSERT INTO
Example: add to Likes(drinker, beer) the fact that Sally likes Bud. INSERT INTO Likes
VALUES (‘Sally’, ‘Bud’);
83
Specifying Attributes in INSERT
We may add to the relation name a list of attributes.
Two reasons to do so:
1. We forget the standard order of attributes for the relation.
2. We don’t have values for all attributes, and we want the system to fill in missing components with NULL of a default value.
84
Example: Specifying Insert Attributes
Another way to add the statement Sally likes Bud to Likes(drinker, beer).
INSERT INTO Likes (beer, drinker) VALUES (‘Bud’, ‘Sally’);
85
Adding Default Values
In a CREATE TABLE statement, we can follow an attribute by DEFAULT and a value.
When an inserted tuple has no value for that attribute, the default value will be used.
86
Example: Default Values
CREATE TABLE Drinkers (
name CHAR(30) PRIMARY KEY,
addr CHAR(50) DEFAULT ‘123 Main St.’, phone CHAR(16)
);
87
Example: Default Values Continued
INSERT INTO Drinkers (name) VALUES (‘Sally’);
Result:
name
address
phone
Sally
123 Main St.
NULL
88
Insert Many Tuples
We may insert the entire result of a query into a relation, using the form:
INSERT INTO
89
Example: Insert a SubQuery
Using Frequents(drinker, beer), enter into the new relation Buddies(name) all of Sally’s “potential buddies”.
i.e. The drinkers who frequent at least one bar that Sally also frequents.
INSERT INTO Buddies (SELECT);
90
Result
The other drinker
INSERT INTO BUDDIES
(SELECT d2.drinker
FROM Frequents d1, Frequents d2
WHERE d1.drinker = ‘Sally’ AND d2.drinker <> ‘Sally’ AND
d1.bar = d2.bar );
Pairs of drinker tuples where the first is for “Sally”, the second is for someone else, and the bars are the same.
91
Deletion
To delete tuples satisfying a condition from some relation:
DELETE FROM
92
Example: Deletion
Delete from Likes(drinker, beer) the tuple stating that Sally likes Bud.
DELETE FROM Likes
WHERE drinker = ‘Sally’ AND beer = ‘Bud’;
93
Example: Delete all Tuples
Alternatively, you can make the relation Likes empty. DELETE FROM Likes;
Note: No WHERE clause is required.
94
Example: Delete Some Tuples
Delete from Beers(name, manf) all beers for which there is another beer by the same manufacturer.
DELETE FROM Beers b WHERE
EXISTS(
SELECT name
FROM Beers
WHERE manf = b.manf AND name <> b.name );
Beers with the same manufacturer and a different name from the name of the beer represented by tuple b.
95
Semantics of Deletion
Suppose Anheuser-Busch makes only Bud and Bud Lite. Suppose we come to the tuple b for Bud first.
The sub-query is non-empty, because of the Bud Lite tuple, so we delete Bud.
Now, when b is the tuple for Bud Lite, do we delete that tuple too?
We do delete Bud Lite as well!
Let’s see how!
96
Semantics of Deletion
Answer: Yes, we do delete Bud Lite as well.
The deletion proceeds in two stages:
1. Mark all tuples for which the WHERE condition is satisfied.
2. Delete the marked tuples.
97
Updates
To change certain attributes in certain tuples of a relation: UPDATE
SET WHERE
98
Example: Updates
Change drinker Fred’s phone number to 555-1234.
UPDATE Drinkers
SET phone = ‘555-1234’ WHERE name = ‘Fred’;
99
Example: Update Several Tuples
Make $4 the maximum price for beer.
UPDATE Sells
SET price = 4.00 WHERE price > 4.00;
100
Questions?
Q
THANKS FOR LISTENING
I’LL BE ANSWERING QUESTIONS NOW
&
A
101
Citations, Images and Resources
Database Management Systems (3rd Ed.), Ramakrishnan & Gehrke
Some content is based off the slides of Dr. Fei Chiang – http://www.cas.mcmaster.ca/~fchiang/
http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/06112016031910AM/sql.png
http://www.acoa-apeca.gc.ca/eng/investment/InvestmentHome/PublishingImages/Canada_flag.jpg
http://4.bp.blogspot.com/- lYiQHrCXpvE/Ve3o5LdGsII/AAAAAAAAd88/Tl16JTc17Ho/s1600/difference%2Bbetween%2Bunion%2Bintersect%2Band%2Bexcept%2Bin%2Bsql%2Bserver.png
http://www.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/intro/src/tpc/db2z_creationoftemporarytables.html http://www.cs.newpaltz.edu/~pletcha/DB/db2_TempTables.html http://www.clipartkid.com/images/85/thumbs-up-happy-smiley-emoticon-clipart-royalty-free-public-domain-pyaRBJ-clipart.png https://web.stanford.edu/~bobonich/glances%20ahead/III.logic.language.html
https://en.wikipedia.org/wiki/Relational_algebra https://en.wikipedia.org/wiki/Conjunctive_query http://ceur-ws.org/Vol-1087/keynote2.pdf http://ceur-ws.org/Vol-1087/keynote2slides.pdf
102