Chapter 3 - SQL_PartA
Chapter 3 - SQL_PartA
Structured Query
Language (SQL I)
COMP3278 Introduction to
Database Management Systems
loan-
branch loan
branch
account-
branch borrower customer_id
account_id
name
balance account owner customer
address 3
Revision
Step 2. Reduce to database tables (Chapter 2B, 2C).
Branch ( branch_id, name, asset)
Foreign key : none.
Loan( loan_id, amount, branch_id)
Foreign key : branch_id REFERENCES Branch (branch_id).
Customer( customer_id, name, address)
Foreign key : none.
Account( account_id, balance, branch_id)
Foreign key : branch_id REFERENCES Branch (branch_id).
5
Running example
Foreign key Foreign key
Foreign
Foreign key
Foreign key
key
Account Owner Customer
account_id customer_id customer_id name address
branch_id account_id balance Foreign
A1 C1 key C1 Kit CB320
B1 A1 500
B2 A2 400 A1 C2 C2 Ben CB326
A2 C2 C3 Diana CB311
B2 A3 900
A3 C4 C4 Tom CB415
B1 A4 700
A4 C4
6
What is SQL?
Structured Query Language (pronounced as “sequel”)
7
Section 1
Data definition
11
Foreign key constraints
A foreign key is a referential constraint between
two tables.
The columns in the referencing table must reference
the columns of the primary key or other superkey in
the referenced table.
I.e., The value in one row of the referencing columns
must occur in a single row in the referenced table (Why?).
customer_id account_id
Insert, Delete
and Update
Branch Branch
text.txt
17
The DELETE clause
The DELETE FROM command is used to delete records
(tuples) from a database table.
Query: Delete all records from the Branch table.
branch_id name asset branch_id name asset
B1 Central 7100000 Empty
B2 Causeway Bay 9000000 Branch
B3 Aberdeen 400000
B4 North Point 3700000
Branch
18
The DELETE clause
The DELETE FROM command is used to delete records
(tuples) from a database table.
Query: Delete the branch “Central” from the Branch table.
branch_id name asset branch_id name asset
B1 Central 7100000 B2 Causeway Bay 9000000
B2 Causeway Bay 9000000 B3 Aberdeen 400000
B3 Aberdeen 400000 B4 North Point 3700000
B4 North Point 3700000
Branch
Branch
The tuples that satisfy the conditions specified here are deleted.
19
The DELETE clause
The UPDATE command is used to update records
(tuples) from a database table.
Query: Update the asset of branch with branch_id 'B1' to $0.
UPDATE Branch
SET asset = 0
WHERE branch_id = 'B1';
20
The UPDATE clause
The UPDATE command can also be used with
arithmetic expressions.
Query: Increase all accounts with balances over $500 by 6%.
Account Account
UPDATE Account
SET balance = balance * 1.06
WHERE balance > 500;
21
The UPDATE clause
The UPDATE command can also be used with
arithmetic expressions.
Query: Increase all accounts with balances under $500
by 5% and all other accounts by 6%.
account_id branch_id balance account_id branch_id balance
A1 B1 500 A1 B1 530
A2 B2 400 A2 B2 420
A3 B2 900 A3 B2 954
A4 B1 700 A4 B1 742
Account Account
UPDATE Account
SET balance = CASE
WHEN balance <=500 THEN balance *1.05
ELSE balance * 1.06
END
23
Section 3
Querying
L1 B3 900 L1 115.385
L2 B2 1500 L2 192.308
L3 B1 1000 L3 128.205
Loan Loan
31
The WHERE clause
The WHERE clause specifies conditions that the result
must satisfy.
Query: For each loan, find out the name of the customer who
borrow the loan.
Step 1. What are the table(s) that contain
customer_id loan_id the information to answer this query?
C1 L3
C4 L2
C2 L1
Borrower
Observation 1.
First, the information of customers (customer_id)
customer_id name address who borrow loan is in the Borrower table.
C1 Kit CB320
C2 Ben CB326 Observation 2.
C3 Diana CB311 Second, we need to find out the name of the
C4 Tom CB415 customer, the name is in the Customer table.
Customer 32
32
The WHERE clause
Step 2. Now we want to relate
SELECT Borrower.loan_id, Customer.name two tables, if no conditions is
FROM Customer, Borrower specified, Cartesian product
will be returned. What is the
joining condition?
Loan
SELECT loan_id
FROM Loan
WHERE branch_id = 'B1' AND
amount > 1200; 35
Exercise
Query: Find the names of all branches that have a loan.
Step 1. Identify the tables that contain the necessary
information to answer the query.
Branch Loan Borrower
branch_id name asset branch_id loan_id amount customer_id loan_id
B1 Central 7100000 B3 L1 900 C1 L3
B2 Causeway Bay 9000000 B1 L2 1500 C4 L2
B3 Aberdeen 400000 B1 L3 1000 C2 L1
B4 North Point 3700000
Renaming
; Result
43
Renaming
name
SELECT DISTINCT Branch.name
Central
FROM Branch, Loan
Aberdeen
WHERE Branch.branch_id = Loan.branch_id
Result
;
name
SELECT DISTINCT B.name
Central
FROM Branch B, Loan L
Aberdeen
WHERE B.branch_id = L.branch_id
Result
;
44
Section 5
String operations
46
The LIKE clause
Query: Find the names of all customers whose
address includes the substring '320'.
Customer
customer_id name address name
C1 Kit CB320 Kit
C2 Ben CB326 Result
C3 Diana CB311
C4 Tom CB415
SELECT name
FROM Customer
WHERE address LIKE '%320%';
Ordering results
SELECT *
FROM Loan
ORDER BY branch_id ASC,
amount DESC;
50
Section 7
Simple Nested
Query
Aggregation
SELECT AVG(balance)
FROM Account
WHERE branch_id = 'B2'; 55
Aggregate functions
Aggregation functions.
AVG
MIN
MAX
SUM
COUNT
56
The GROUP BY clause
Aggregation function can be applied to a group of sets
of tuples by using GROUP BY clause.
Query: Find the average balance at each branch.
Step1. Grouping
GROUP BY branch_id
Account
branch_id account_id balance branch_id account_id balance
B1 A1 500 A1 500
B1
B2 A2 400 A4 700
B2 A3 900 A2 400
B2
B1 A4 700 A3 900 57
The GROUP BY clause
Aggregation function can be applied to a group of sets
of tuples by using GROUP BY clause.
Query: Find the average balance at each branch.
SELECT branch_id, AVG(balance)
FROM Account
GROUP BY branch_id;
Join
Slides prepared by - Dr. Chui Chun Kit for students in COMP3278
For other uses, please email : [email protected]
Join
A join takes 2 tables as input and returns a table.
Employee Department
e_name department_id department_id d_name
Kit 31 31 CS Cartesian product, then
33 Civil
E.department_id = D.department_id
Ben 33
John 33 34 ME
Diana 34 35 EEE
Tom 34
David NULL
Result 61
The OUTER JOIN clause
An outer join does not require each record in the two
joined tables to have a matching record.
Employee Department
e_name department_id department_id d_name
Even if the LEFT table record does not
have matching records in the RIGHT
Kit 31 31 CS
table, we still output the tuple in the
Ben 33 33 Civil
LEFT table (with null values for the
John 33 34 ME columns of the RIGHT table).
Diana 34 35 EEE
Tom 34
David NULL
SELECT * Kit 31 31 CS
Ben 33 33 Civil
FROM Employee E LEFT OUTER JOIN
John 33 33 Civil
Department D
Diana 34 34 ME
ON E.department_id =
Tom 34 34 ME
D.department_id; David NULL NULL NULL
Result 62
The OUTER JOIN clause
An outer join does not require each record in the two
joined tables to have a matching record.
Employee Department
e_name department_id department_id d_name
Even if the RIGHT table record does
not have matching records in the LEFT
Kit 31 31 CS
table, we still output the tuple in the
Ben 33 33 Civil
RIGHT table (with null values for the
John 33 34 ME columns of the LEFT table).
Diana 34 35 EEE
Tom 34
David NULL
SELECT * Kit 31 31 CS
Ben 33 33 Civil
FROM Employee E RIGHT OUTER JOIN
John 33 33 Civil
Department D
Diana 34 34 ME
ON E.department_id =
Tom 34 34 ME
D.department_id; NULL NULL 35 EEE
Result 63
Chapter 3A.
END
COMP3278 Introduction to
Database Management Systems