Structured Query Language
Basic Query
Syntax
SELECTclause- specify the columns that you want to retrieve from a database table
DISTINCTkeyword (optional)- return only unique values from a specific column (i.e., it would eliminate the outputs)
FROMclause- specify the table or tables from which you want to retrieve data
WHEREclause (optional)- filter data based on a specific condition
-
1
2
3SELECT DISTINCT a1, a2, ..., an -- target list
FROM R1, R2, ..., Rm -- relation list
WHERE P -- qualification Examples:
Find the names of sailors who have reserved boat number 103

-
1
2
3SELECT S.sname
FROM Sailors S, Reserves R
WHERE R.sid = S.sid AND R.bid = 103
-
Given following schema:

Find the sids of sailors who have reserved a red boat
1
2
3SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid AND B.color = 'red'Find the names of sailors who have reserved a red boat
1
2
3SELECT S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid AND B.bid = R.bid AND B.color = 'red'Find the colors of boats reserved by Lubber
1
2
3SELECT B.color
FROM Sailors S, Boats B, Reserves R
WHERE S.sname = 'Lubber' AND S.sid = R.sid AND B.bid = R.bid
Expressions & Strings
ASand=(name field)- rename columns or tables in the query result
1
2SELECT S.age, age1 = S.age + 5, S.age * 2 AS age2
FROM Sailors S
- rename columns or tables in the query result
LIKE- is used for string matching
_means any one character (must have one character)%means 0 or more arbitrary characters1
WHERE S.sname LIKE 'B_%B' -- BAB, BBBBBBB are accepted, BB is not accepted
Set Manipulation
Union
combine the results of two
SELECTstatements into a single result set (eliminates duplicates)E.g., Find the sids of sailors who have reserved a red boat or a green boat
1
2
3
4
5
6
7
8
9
10
11
12
13-- SELECT R.sid
-- FROM Boats B, Reserves R
-- WHERE B.bid = R.bid AND (B.color = 'red' OR B.color = 'green')
SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid AND B.color = 'red'
UNION
SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid AND B.color = 'green'
Intersection
- retrieve only the common records returned by two
SELECTstatements- E.g., Find the sids of sailors who have reserved a red boat and a green boat
1
2
3
4
5
6
7
8
9
10
11
12
13-- SELECT R.sid
-- FROM Boats B, Reserves R
-- WHERE B.bid = R.bid AND (B.color = 'red' AND B.color = 'green')
SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid AND B.color = 'red'
INTERSECT
SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid AND B.color = 'green'
- E.g., Find the sids of sailors who have reserved a red boat and a green boat
Except
- retrieve all records from one
SELECTstatement that are not returned by a secondSELECTstatement- E.g., Find the sids of sailors who have reserved a red boat but not a green boat
1
2
3
4
5
6
7
8
9SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid AND B.color = 'red'
EXCEPT
SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid AND B.color = 'green'
- E.g., Find the sids of sailors who have reserved a red boat but not a green boat
Nested Structure
Nested Queries
- a query that has another query embedded within it, the embedded query is called as a subquery
- the queries can have a deeply nested structures
- E.g., Find names of sailors who have reserved boat with bid 103
1
2
3
4
5SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
WHERE R.bid = 103)- The inner subquery is completely independent of the outer query
- E.g., Find names of sailors who have reserved boat with bid 103
Correlated Nested Queries
- a type of nested query that uses data from the outer query in the subquery
- E.g., Find names of sailors who have reserved boat with bid 103
1
2
3
4
5SELECT S.sname
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid = 103 AND S.sid = R.sid)- The inner subquery depends on the row that being examined in the outer query
- E.g., Find names of sailors who have reserved boat with bid 103
Comparsion Operators
Arithmetic Comparison Operators
=: equality!=or<>: not equal to<:less than>: greater than<=: less than or equal to>=: greater than or equal to
Set Comparsion Operators
INOperator- check whether a specified value matches any value in a list or a subquery
- allows us to specify multiple values in a
WHEREclause - negative operator:
NOT IN- E.g., Find the names of boats which are red, blue or green
1
2
3SELECT B.bname
FROM Boats B
WHERE B.color IN ('red', 'blue', 'green')
- E.g., Find the names of boats which are red, blue or green
EXISTS- check if a subquery returns any rows
- negative operator:
NOT EXISTS- E.g., Find names of sailors who have reserved boat with bid 103
1
2
3
4
5SELECT S.sname
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid = 103 AND S.sid = R.sid)
- E.g., Find names of sailors who have reserved boat with bid 103
op ANYorop SOME- check if the value being compared matches any of the values returned by the subquery
- E.g., Find sailors whose rating is greater than that of some sailor called Horatio:
1
2
3
4
5SELECT *
FROM Sailors S1
WHERE S1.rating > ANY (SELECT S2.rating
FROM Sailors S2
WHERE S2.name = 'Horatio')
- E.g., Find sailors whose rating is greater than that of some sailor called Horatio:
- check if the value being compared matches any of the values returned by the subquery
op ALL- check if the value being compared matches all of the values returned by the subquery
- E.g., Find sailors whose rating is better than every sailor called Horatio
1
2
3
4
5SELECT *
FROM Sailors S1
WHERE S1.rating > ALL (SELECT S2.rating
FROM Sailors S2
WHERE S2.name = 'Horatio') - E.g., Find the sailors with the highest rating
1
2
3
4SELECT *
FROM Sailors S1
WHERE S1.rating >= ALL (SELECT S2.rating
FROM Sailors S2)
- E.g., Find sailors whose rating is better than every sailor called Horatio
- check if the value being compared matches all of the values returned by the subquery
Division
Aggregate Operations
Five Operations
| Operations | Syntax | Descriptions |
|---|---|---|
| COUNT | COUNT([DISTINCT]A) |
The number of [unique] value in the A column |
| SUM | SUM([DISTINCT]A) |
The sum of all [unique] values in the A column |
| AVG | AVG([DISTINCT]A) |
The average of all [unique] values in the A column |
| MAX | MAX(A) |
The maximum values in the A column |
| MIN | MIN(A) |
The minimum values in the A column |
Syntax for
COUNT,SUMandAVG:- E.g., Find the average age of all sailors with rating of 10
1
2
3SELECT COUNT(S.age)
FROM Sailors S
WHERE S.rating = 10
- E.g., Find the average age of all sailors with rating of 10
Syntax for
MAXandMIN:E.g., Find the name and age of the oldest sailor
1
2
3
4SELECT S.name, S.age
FROM Sailors S
WHERE S.age = (SELECT MAX(S2.age)
FROM Sailors S2)Noted: the following syntax is invalid
1
2SELECT S.name, MAX(S.age) -- invalid
FROM Sailors S
Examples:
Count the number of sailors
1
2SELECT COUNT(*)
FROM SailorsFind the names of sailors who are older than the oldest sailor with a rating of 10
1
2
3
4
5SELECT S.name
FROM Sailors S
WHERE S.age > (SELECT MAX(S2.age)
FROM Sailors S2
WHERE S2.rating = 10)
GROUP BY and HAVING
GROUP BYis used to group rows based on the values in one or more columns with aggregate functionsHAVINGis used to filter the groups based on some criteriaNoted: only the columns that appear in
GROUP BYclause can appear in theHAVINGclause
E.g., Find the average age of sailors for each rating level that has at least two sailors
1
2
3
4SELECT S.sname, AVG(S.age) AS avgage
FROM Sailors S
GROUP BY S.rating
HAVING COUNT(*) >= 2
E.g., Find the average age of sailors who are at least 18 years old for each rating level that has at least two sailors
1
2
3
4
5
6
7SELECT S.sname, AVG(S.age) AS avgage
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING 2 <= (SELECT COUNT(*) -- !!!
FROM Sailors S2
WHERE S.rating = S2.rating)
E.g., Find the average age of sailors who are at least 18 years old for each rating level that has at least such two sailors
1
2
3
4
5
6
7SELECT S.sname, AVG(S.age) AS avgage
FROM Sailors S
WHERE S.age >= 18 -- ?
GROUP BY S.rating
HAVING 2 <= (SELECT COUNT(*)
FROM Sailors S2
WHERE S.rating = S2.rating AND S.age >= 18) -- !!!
- Noted: Aggregate operations cannot be nested!!!
- E.g., Find those ratings for which the average age is the minimum over all ratings
- The following SQL is invalid:
1
2
3
4
5SELECT S.rating
FROM Sailors S
WHERE S.age = (SELECT MIN(AVG(S2.age))
FROM Sailors S2
GROUP BY S2.rating) - Solution:
1
2
3
4
5
6SELECT Temp.rating, Temp.avgage
FROM (SELECT S.rating, AVG(S.age) AS avgage
FROM Sailors S
GROUP BY S.rating) AS Temp
WHERE Temp.avgage = (SELECT MIN(Temp.avgage)
FROM Temp)


