Basic SQL
Basic SQL
Basic SQL
Introduction
SQL—Structured Query Language
Pronounced “S-Q-L” or “sequel”
The query language of all commercial database
systems (including Oracle, MS Access, MySQL, etc.)
2
Basic SQL Query
3
SQL example 1
SELECT *
FROM CUST
4
SQL example 2
SELECT customer-id
FROM CUST
5
SQL example 2 (cont.)
SELECT customer-id
FROM CUST
6
SQL example 2 (cont.)
7
SQL example 2 (cont.)
8
SQL example 3 (removing duplicates)
SELECT customer-city
FROM CUST
9
SQL example 4
10
SQL example 5
Answer:
SELECT * FROM CUST
WHERE customer-city = ‘Pittsfield’
11
SQL example 6
Write the SQL query to display the names of the customers living in
‘Pittsfield’. We do not want any other attributes displayed.
Answer:
SELECT customer-name FROM CUST
WHERE customer-city = ‘Pittsfield’
12
SQL example 6
13
SQL example 6 (cont.)
14
SQL example 7
Table schema:
CUST(customer-id, customer-name, customer-street, customer-city)
Write an SQL query to find the names of the customers who are living
in the ‘Park’ street in city ‘Pittsfield’.
Answer:
SELECT customer-name
FROM CUST
WHERE customer-street = ‘Park’ AND customer-city = ‘Pittsfield’
15
SQL example 8
Table schema:
CUST(customer-id, customer-name, customer-street, customer-city)
Find the customer-id and names of the customers who are living in
city ‘Pittsfield’ or ‘Rye’.
Answer:
SELECT customer-id, customer-name
FROM CUST
WHERE customer-city = ‘Pittsfield’ OR customer-city = ‘Rye’
16
SQL example 9 (string matching)
Table schema:
CUST(customer-id, customer-name, customer-street, customer-city)
Write an SQL query to find the ids of the customers whose names
start with the letter J and contain at least two letters.
Answer:
SELECT customer-id
FROM CUST
WHERE customer-name LIKE ‘J_%’
LIKE is used for string matching. `_’ stands for any one character and `
%’ stands for 0 or more arbitrary characters.
17
SQL example 10 (order by)
The previous queries do not have
any ordering requirements.
SELECT *
FROM ACC
WHERE balance > 10000
ORDER BY balance
SELECT *
FROM ACC
WHERE balance > 10000
ORDER BY balance DESC
18
SQL example 11 (arithmetic expressions )
19
SQL example 12 (cartesian product)
So far all our queries retrieve information from a single table.
Now let us consider two tables: CUST and ACC.
See next.
20
SQL example 12 (cartesian product)
21
SQL example 13
Write an SQL query to display, for each account, its id and the name
of its owner.
Answer:
SELECT ACC.acc-id, CUST.name
FROM CUST, ACC
WHERE CUST.cust-id = ACC.cust-id
23
SQL example 13 (cont.)
24
SQL example 13 (cont.)
25
SQL example 13 (cont.)
27
SQL example 14
Answer:
SELECT name FROM CUST, ACC
WHERE CUST.cust-id = ACC.cust-id and balance >= 30000
28
SQL example 14 (cont.)
29
SQL example 14 (cont.)
SELECT name
FROM CUST, ACC
WHERE CUST.cust-id =
ACC.cust-id and
balance >= 30000
Then, filtering.
30
SQL example 14 (cont.)
SELECT name
FROM CUST, ACC
WHERE CUST.cust-id =
ACC.cust-id and
balance >= 30000
Finally, projection.
31
SQL example 15
Find the ids of all accounts except the one with the largest balance.
Answer:
SELECT DISTINCT T1.acc-id
FROM ACC T1, ACC T2
WHERE T1.balance < T2.balance
32
SQL example 15 (cont.)
T1 T2
33
SQL example 15 (cont.)
Then, filtering.
34
SQL example 15 (cont.)
35
SQL example 16 (intersection)
(SELECT cust-id
FROM CUST)
INTERSECT
(SELECT cust-id
FROM ACC)
36
SQL example 16 (cont.)
37
SQL example 17 (union)
(SELECT cust-id
FROM CUST)
UNION
(SELECT cust-id
FROM ACC)
(SELECT cust-id
FROM CUST)
EXCEPT
(SELECT cust-id
FROM ACC)
Another example:
(SELECT cust-id
FROM ACC)
EXCEPT
(SELECT cust-id
FROM CUST)
40
What have we learned?
SELECT: projection
WHERE: filtering, also called selection
FROM: join
Also, self-join, with ‘renaming’
ORDER BY
INTERSECTION
UNION
EXCEPT
41
SQL example 19
Wait!!!!!
Remember that we solved a similar query before:
Find the ids of all accounts except the one with the largest
balance.
Our answer was:
SELECT DISTINCT T1.acc-id
FROM ACC T1, ACC T2
WHERE T1.balance < T2.balance
42
SQL example 19
Find the id of the account with the largest
balance.