03 SQL
03 SQL
(SQL)
COMP3278C
Introduction to Database Management Systems
Acknowledgement: Dr. Chui Chun Kit, Dr. Reynold Cheng, Dr. Ping Luo, Dr. Yi Chen
We have learnt …
Step 1: Information modeling using E-R Diagram
Step 2: Reduce to relational tables
1. Data definition
• Create table — CREATE TABLE
Table name (cannot be a keyword
in database, e.g., CREATE)
• The DBMS may reject the DROP TABLE instruction when a table is
referenced by another table via some constraints (e.g., foreign key)
Foreign key
Account Owner After the foreign key is established, if
branch_id account_id balance account_id customer_id we drop the Account table, the
records in the Owner table will lost
their references. (e.g., Cannot find
which account a customer own
anymore.)
Branch
branch_id name asset
1. Data definition
• Modify table — ALTER TABLE
• Add columns to an existing table.
ALTER TABLE Branch ADD branch_phone INT (12);
Table name
2. Data modification B1
B2
Central
Causeway Bay
7100000
9000000
B3 Aberdeen 400000
B4 North Point 3700000
• Insert records into a table — INSERT INTO
• A DBMS often provides a function to put a
large number of records into a table.
• E.g., LOAD DATA LOCAL INFILE
UPDATE Branch
SET asset = 0
WHERE branch_id = ‘B1’;
2. Data modification
• Update records of a table — UPDATE
• The UPDATE command can also be used with arithmetic expressions.
• Increase all accounts with balances over $500 by 6%.
Account Account
branch_id account_id balance branch_id account_id balance
B1 A1 500 B1 A1 500
B2 A2 400 B2 A2 400
B2 A3 900 B2 A3 954
B1 A4 700 B1 A4 742
UPDATE Account
SET balance = balance * 1.06
WHERE balance > 500;
2. Data modification
• Update records of a table — UPDATE
• The UPDATE command can also be used with arithmetic expressions.
• Increase all accounts with balances under $500 by 5% and all other
accounts by 6%.
Account Account
branch_id account_id balance branch_id account_id balance
B1 A1 500 B1 A1 530
B2 A2 400 B2 A2 420
Question: Which SQL
B2 A3 900 B2 A3 954
shall be executed first?
B1 A4 700 B1 A4 742
UPDATE Account
SET balance = CASE
WHEN balance < 500 THEN balance * 1.05
ELSE balance * 1.06
END
3. Query: SELECT
• The SELECT clause lists the attributes desired in the result of a query.
• Query: List the load_id and amount of each loan records, display the amount
in USD (originally stored in HKD).
Loan Result
branch_id load_id amount load_id amount/7.8
B3 L1 900 L1 115.385
B1 L2 1500 L2 192.308
B1 L3 1000 L3 128.205
Customer Result
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326
C3 Jolly CB311
C4 Yvonne CB415
Borrower
customer_id load_id
C1 L3
C4 L2
C2 L1
3. Query: SELECT Cartesian product of A and B
means generating all possible
pairs of records from A and B.
• The FROM clause lists the tables involved in the query.
List the Cartesian product of
SELECT * FROM Customer, Borrower; Customer and Borrower
Customer Result
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326 C2 Ben CB326 C1 L3
C3 Jolly CB311
C4 Yvonne CB415
Borrower
customer_id load_id
C1 L3
C4 L2
C2 L1
3. Query: SELECT Cartesian product of A and B
means generating all possible
pairs of records from A and B.
• The FROM clause lists the tables involved in the query.
List the Cartesian product of
SELECT * FROM Customer, Borrower; Customer and Borrower
Customer Result
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326 C2 Ben CB326 C1 L3
C3 Jolly CB311 C3 Jolly CB311 C1 L3
C4 Yvonne CB415
Borrower
customer_id load_id
C1 L3
C4 L2
C2 L1
3. Query: SELECT Cartesian product of A and B
means generating all possible
pairs of records from A and B.
• The FROM clause lists the tables involved in the query.
List the Cartesian product of
SELECT * FROM Customer, Borrower; Customer and Borrower
Customer Result
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326 C2 Ben CB326 C1 L3
C3 Jolly CB311 C3 Jolly CB311 C1 L3
C4 Yvonne CB415 C4 Yvonne CB415 C1 L3
Borrower
customer_id load_id
C1 L3
C4 L2
C2 L1
3. Query: SELECT Cartesian product of A and B
means generating all possible
pairs of records from A and B.
• The FROM clause lists the tables involved in the query.
List the Cartesian product of
SELECT * FROM Customer, Borrower; Customer and Borrower
Customer Result
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326 C2 Ben CB326 C1 L3
C3 Jolly CB311 C3 Jolly CB311 C1 L3
C4 Yvonne CB415 C4 Yvonne CB415 C1 L3
C1 Kit CB320 C4 L2
Borrower
customer_id load_id
C1 L3
C4 L2
C2 L1
3. Query: SELECT Cartesian product of A and B
means generating all possible
pairs of records from A and B.
• The FROM clause lists the tables involved in the query.
List the Cartesian product of
SELECT * FROM Customer, Borrower; Customer and Borrower
Customer Result
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326 C2 Ben CB326 C1 L3
C3 Jolly CB311 C3 Jolly CB311 C1 L3
C4 Yvonne CB415 C4 Yvonne CB415 C1 L3
C1 Kit CB320 C4 L2
Borrower C2 Ben CB326 C4 L2
customer_id load_id C3 Jolly CB311 C4 L2
C1 L3 C4 Yvonne CB415 C4 L2
C4 L2 C1 Kit CB320 C2 L1
C2 L1 C2 Ben CB326 C2 L1
C3 Jolly CB311 C2 L1
C4 Yvonne CB415 C2 L1
3. Query: SELECT Cartesian product of A and B
means generating all possible
pairs of records from A and B.
• The FROM clause lists the tables involved in the query.
List the Cartesian product of
SELECT * FROM Customer, Borrower; Customer and Borrower
Customer Result
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326 C2 Ben CB326 C1 L3
C3 Jolly CB311 C3 Jolly CB311 C1 L3
Not very
C4 Yvonne CB415 C4 Yvonne CB415 C1 L3
useful!
C1 Kit CB320 C4 L2
Borrower C2 Ben CB326 C4 L2
customer_id load_id C3 Jolly CB311 C4 L2
C1 L3 C4 Yvonne CB415 C4 L2
C4 L2 C1 Kit CB320 C2 L1
C2 L1 C2 Ben CB326 C2 L1
C3 Jolly CB311 C2 L1
C4 Yvonne CB415 C2 L1
3. Query: SELECT
• 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) in the database that contain the information to answer this query?
• The information of
customer who borrow
every loan is in the
Borrower table.
Customer
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326 C2 Ben CB326 C1 L3
C3 Jolly CB311 C3 Jolly CB311 C1 L3
C4 Yvonne CB415 C4 Yvonne CB415 C1 L3
C1 Kit CB320 C4 L2
Borrower C2 Ben CB326 C4 L2
customer_id load_id C3 Jolly CB311 C4 L2
C1 L3 C4 Yvonne CB415 C4 L2
C4 L2 C1 Kit CB320 C2 L1
C2 L1 C2 Ben CB326 C2 L1
C3 Jolly CB311 C2 L1
C4 Yvonne CB415 C2 L1
SELECT Borrower.load_id, [Link]
FROM Customer, Borrower
3. Query: SELECT WHERE Customer.customer_id =
Borrower.customer_id;
• 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.
Customer
customer_id name address customer_id name address customer_id load_id
C1 Kit CB320 C1 Kit CB320 C1 L3
C2 Ben CB326 C2 Ben CB326 C1 L3
Result
C3 Jolly CB311 C3 Jolly CB311 C1 L3
load_id name
C4 Yvonne CB415 C4 Yvonne CB415 C1 L3
L3 Kit
C1 Kit CB320 C4 L2
L2 Ynonne
Borrower C2 Ben CB326 C4 L2
L1 Ben
customer_id load_id C3 Jolly CB311 C4 L2
C1 L3 C4 Yvonne CB415 C4 L2
C4 L2 C1 Kit CB320 C2 L1
C2 L1 C2 Ben CB326 C2 L1
C3 Jolly CB311 C2 L1
C4 Yvonne CB415 C2 L1
3. Query: SELECT
• In WHERE clause, comparison results can be combined using logical
connectives AND, OR, and NOT.
• Query: Find all loan ID of loans made at branch_id B1 with loan
amount > $1200.
Loan Result
branch_id load_id amount load_id
B3 L1 900 L2
B1 L2 1500
B1 L3 1000
SELECT load_id
FROM Loan
WHERE branch_id = ‘B1’ AND amount > 1200;
3. Query: SELECT
• Step 1: identify the tables that contain the necessary information to answer the query
SELECT [Link]
3. Query: SELECT - Exercise FROM Branch, Loan
WHERE Branch.branch_id =
• Query: Find the names of all branches that have a loan Loan.branch_id;
Foreign key
Branch Loan
branch_id name asset branch_id load_id amount
B1 Central 7100000 B3 L1 900
B2 Causeway Bay 9000000 B1 L2 1500
B3 Aberdeen 400000 B1 L3 1000
B4 North Point 3700000
• Step 1: identify the tables that contain the necessary information to answer the query
• Step 2: identify the conditions
SELECT [Link]
3. Query: SELECT - Exercise FROM Branch, Loan
WHERE Branch.branch_id =
• Query: Find the names of all branches that have a loan Loan.branch_id;
Foreign key
Branch Loan
branch_id name asset branch_id load_id amount name
B1 Central 7100000 B3 L1 900 Aberdeen
B2 Causeway Bay 9000000 B1 L2 1500
B3 Aberdeen 400000 B1 L3 1000
B4 North Point 3700000
• Step 1: identify the tables that contain the necessary information to answer the query
• Step 2: identify the conditions
SELECT [Link]
3. Query: SELECT - Exercise FROM Branch, Loan
WHERE Branch.branch_id =
• Query: Find the names of all branches that have a loan Loan.branch_id;
Foreign key
Branch Loan
branch_id name asset branch_id load_id amount name
B1 Central 7100000 B3 L1 900 Aberdeen
B2 Causeway Bay 9000000 B1 L2 1500 Central
B3 Aberdeen 400000 B1 L3 1000
B4 North Point 3700000
• Step 1: identify the tables that contain the necessary information to answer the query
• Step 2: identify the conditions
SELECT [Link]
3. Query: SELECT - Exercise FROM Branch, Loan
WHERE Branch.branch_id =
• Query: Find the names of all branches that have a loan Loan.branch_id;
Foreign key
Branch Loan
branch_id name asset branch_id load_id amount name
B1 Central 7100000 B3 L1 900 Aberdeen
B2 Causeway Bay 9000000 B1 L2 1500 Central
B3 Aberdeen 400000 B1 L3 1000 Central
B4 North Point 3700000
• Step 1: identify the tables that contain the necessary information to answer the query
• Step 2: identify the conditions
SELECT [Link]
3. Query: SELECT - Exercise FROM Branch, Loan
WHERE Branch.branch_id =
• Query: Find the names of all branches that have a loan Loan.branch_id;
Foreign key
Branch Loan
branch_id name asset branch_id load_id amount name
B1 Central 7100000 B3 L1 900 Aberdeen
B2 Causeway Bay 9000000 B1 L2 1500 Central
B3 Aberdeen 400000 B1 L3 1000 Central
B4 North Point 3700000
• Step 1: identify the tables that contain the necessary information to answer the query
• Step 2: identify the conditions
3. Operations on SELECT - Renaming
• Rename on tables
Result
SELECT DISTINCT [Link] AS ‘Branch name’ Branch name
FROM Branch B, Loan L Aberdeen
Central
WHERE B.branch_id = L.branch_id;
3. Operations on SELECT - String
• LIKE clause in WHERE is the most commonly used operations on strings
• Pattern matching
• % (percent): matches any substring
• ‘Perry%’ matches any string beginning with ‘Perry'
• _ (underscore): matches any character
• ‘_ _ _ %’ matches any string of at least 3 character
• Note: Patterns are case sensitive
3. Operations on SELECT - String
• LIKE clause in WHERE is the most commonly used operations on strings
• Query: Find the names of all customers whose address includes the substring
‘320’.
Customer
customer_id name address
Result
C1 Kit CB320
name
C2 Ben CB326
Kit
C3 Jolly CB311
C4 Yvonne CB415
SELECT name
FROM Customer
WHERE address LIKE ‘%320%’;
3. Functions on SELECT - Ordering results
• The ORDER BY clause list the result in sorted order
• Query: List the names of all customers in alphabetic order
Customer Result
customer_id name address name SELECT name
C1 Kit CB320 Ben
C2 Ben CB326 Jolly FROM Customer
C3 Jolly CB311 Kit
Yvonne
ORDER BY name ASC;
C4 Yvonne CB415
Result2
name SELECT name
Yvonne
Kit FROM Customer
Jolly
Ben
ORDER BY name DESC;
• Use DESC for descending order, and ASC for ascending order. Default: ascending
3. Functions on SELECT - Ordering results
• The ORDER BY clause list the result in sorted order
• Query: List the names of all customers in alphabetic order
Customer Result
customer_id name address name SELECT name
C1 Kit CB320 Ben
C2 Ben CB326 Jolly FROM Customer
C3 Jolly CB311 Kit
Yvonne
ORDER BY name ASC;
C4 Yvonne CB415
=
SELECT name
FROM Customer
ORDER BY name;
• Use DESC for descending order, and ASC for ascending order. Default: ascending
3. Functions on SELECT - Ordering results
• The ORDER BY clause list the result in sorted order
• Query: List the loan records in ascending order of branch_id. If two tuples have
the same branch_id, order by their loan amount in descending order.
Loan Intermediate result Result
branch_id load_id amount branch_id load_id amount branch_id load_id amount
B3 L1 900 B1 L2 1500 B1 L2 1500
B1 L2 1500 B1 L3 1000 B1 L3 1000
B1 L3 1000 B3 L1 900 B3 L1 900
SELECT *
FROM Loan
ORDER BY branch_id ASE, amount DESC;
3. Functions on SELECT - Aggregation
• Aggregation functions take a collection of values as input and return a single
value.
• Average: AVG (must be numbers)
• Total: SUM (must be numbers)
• Minmum: MIN
• Maximum: MAX
• Count: COUNT
3. Functions on SELECT - Aggregation
• Aggregation functions take a collection of values as input and return a single
value.
• Query: Find the average balance of all accounts at the branch with
branch_ID ‘B2’
Account
branch_id account_id balance
B1 A1 500 Result
B2 A2 400 AVG (balance)
B2 A3 900 650.0
B1 A4 700
SELECT AVG(balance)
FROM Account
WHERE branch_id = ‘B2’;
3. Functions on SELECT - Aggregation
• Aggregation can be applied to a group of sets of records by using
GROUP BY clause.
• Query: Find the average at each branch.
Account Intermediate result Result
branch_id account_id balance branch_id account_id balance branch_id AVG balance)
B1 A1 500 A1 500 B1 600.0
B1
B2 A2 400 A4 700 B2 650.0
B2 A3 900 A2 400
B2
B1 A4 700 A3 900
Step 3. Aggregation
HAVING AVG(balance) >= 650;
3. Query: Exercises Step 3. Create the database
CREATE TABLE Employee (
employee_id INT(12),
Step 1. Information modeling with E-R Diagram
name VARCHAR(30) NOT NULL,
salary INT UNSIGNED NOT NULL,
employee_id since department_id
PRIMARY KEY (employee_id)
);
employee works_in department
CREATE TABLE Department (
name salary budget name department_id INT(12),
name VARCHAR(30) NOT NULL,
budget INT UNSIGNED NOT NULL,
Step 2. Reduce to database tables PRIMARY KEY (department_id)
);
• Employee (employee_id, name, salary)
• Foreign key: none CREATE TABLE Works_in (
• Department (department_id, name, budget) employee_id INT(12),
department_id INT(12),
• Foreign key: none since DATE NOT NULL,
• Works_in (employee_id, department_id, since) PRIMARY KEY (employee_id, department_id),
• Foreign key: employee_id REFERENCES Employee (employee_id) FOREIGN KEY (employee_id)
department_id REFERENCES Department (department_id) REFERENCES Employee (employee_id),
FOREIGN KEY (department_id)
REFERENCES Department (department_id)
);
3. Query: Exercises
Step 3. Create the database
INSERT INTO Employee VALUES (1, ‘Jones’, 26000);
INSERT INTO Employee VALUES (2, ‘Smith’, 28000);
INSERT INTO Employee VALUES (3, ‘Parker’, 35000);
INSERT INTO Employee VALUES (4, ‘Smith’, 24000);
INSERT INTO Department VALUES (1, ‘Toys’, 122000), (2, ‘Tools’, 239000), (3, ‘Food’, 100000);
INSERT INTO Works_in VALUES (1, 1, ‘2001-1-1’), (2,1, ‘2002-4-1’), (2, 2, ‘2005-2-2’), (3, 3, ‘2003-1-1’), (4, 3, ‘2005-1-1’);
• Query 2: Find the employee_id and name of employee who work in department with department_id = 2
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 2: Find the employee_id and name of employee who work in department with department_id = 2
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 2: Find the employee_id and name of employee who work in department with department_id = 2
SELECT E.employee_id, E. name
FROM Employee E, Works_in W
WHERE W.department_id = 2 AND E.employee_id = W.employee_id;
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 2: Find the employee_id and name of employee who work in department with department_id = 2
SELECT E.employee_id, E. name employee_id name
FROM Employee E, Works_in W 2 Smith
WHERE W.department_id = 2 AND E.employee_id = W.employee_id;
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 3: Find the department name where employee with employee_id = 2 works.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 3: Find the department name where employee with employee_id = 2 works.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 3: Find the department name where employee with employee_id = 2 works.
SELECT [Link]
FROM Works_in W, Department D
WHERE
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 3: Find the department name where employee with employee_id = 2 works.
SELECT [Link]
name
FROM Works_in W, Department D Toys
WHERE W.employee_id = 2 AND D.department_id = W.department_id; Tools
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 5: Find the department name where employees named Smith work
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 5: Find the department name where employees named Smith work
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 5: Find the department name where employees named Smith work
SELECT [Link]
FROM Employee E, Works_in W, Department D
WHERE
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 5: Find the department name where employees named Smith work
SELECT [Link] name
FROM Employee E, Works_in W, Department D Toys
WHERE [Link] = ‘Smith’ AND Tools
Food
E.employee_id = W.employee_id AND
W.department_id = D.department_id;
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 6: Find the names of the department which have an employee named Smith and their budget is
greater than 100000.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 6: Find the names of the department which have an employee named Smith and their budget is
greater than 100000.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 6: Find the names of the department which have an employee named Smith and their budget is
greater than 100000.
SELECT [Link]
FROM Employee E, Works_in W, Department D
WHERE
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 6: Find the names of the department which have an employee named Smith and their budget is
greater than 100000.
SELECT [Link]
FROM Employee E, Works_in W, Department D name
Toys
WHERE [Link] = ‘Smith’ AND
Tools
E.employee_id = W.employee_id AND
W.department_id = D.department_id AND
[Link] > 100000;
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 7: Find the budgets of departments, who employ an employee called Smith.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 7: Find the budgets of departments, who employ an employee called Smith.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 7: Find the budgets of departments, who employ an employee called Smith.
SELECT [Link]
FROM Employee E, Works_in W, Department D
WHERE
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 7: Find the budgets of departments, who employ an employee called Smith.
SELECT [Link]
bugdet
FROM Employee E, Works_in W, Department D
122000
WHERE [Link] = ‘Smith’ AND 239000
E.employee_id = W.employee_id AND 100000
W.department_id = D.department_id;
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 8: For each department, find the total number of employees it employs.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 8: For each department, find the total number of employees it employs.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 8: For each department, find the total number of employees it employs.
SELECT
FROM Works_in W
GROUP BY W.department_id;
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 8: For each department, find the total number of employees it employs.
SELECT W.department_id, COUNT(*) department_id count(*)
FROM Works_in W 1 2
2 1
GROUP BY W.department_id;
3 2
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 10: In each department, find the highest salary of the employee in that department.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 10: In each department, find the highest salary of the employee in that department.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 10: In each department, find the highest salary of the employee in that department.
employee_id name salary employee_id department_id since
SELECT 1 Jones 26000 1 1 2001-1-1
FROM Employee E, Works_in W 2 Smith 28000 1 1 2001-1-1
3 Parker 35000 1 1 2001-1-1
4 Smith 24000 1 1 2001-1-1
1 Jones 26000 2 1 2002-4-1
2 Smith 28000 2 1 2002-4-1
3 Parker 35000 2 1 2002-4-1
4 Smith 24000 2 1 2002-4-1
……
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 10: In each department, find the highest salary of the employee in that department.
employee_id name salary employee_id department_id since
SELECT 1 Jones 26000 1 1 2001-1-1
FROM Employee E, Works_in W 2 Smith 28000 1 1 2001-1-1
WHERE E.employee_id = W.employee_id 3 Parker 35000 1 1 2001-1-1
4 Smith 24000 1 1 2001-1-1
1 Jones 26000 1 1 2001-1-1
2 Smith 28000 2 1 2002-4-1
3 Parker 35000 2 1 2002-4-1
4 Smith 24000 2 1 2002-4-1
……
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 10: In each department, find the highest salary of the employee in that department.
employee_id name salary employee_id department_id since
SELECT 1 Jones 26000 1 1 2001-1-1
FROM Employee E, Works_in W 2 Smith 28000 2 1 2002-4-1
WHERE E.employee_id = W.employee_id 2 Smith 28000 2 2 2005-2-2
3 Parker 35000 3 3 2003-1-1
4 Smith 24000 4 3 2005-1-1
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 10: In each department, find the highest salary of the employee in that department.
employee_id name salary employee_id department_id since
SELECT 1 Jones 26000 1 1 2001-1-1
FROM Employee E, Works_in W 2 Smith 28000 2 1 2002-4-1
WHERE E.employee_id = W.employee_id 2 Smith 28000 2 2 2005-2-2
3 Parker 35000 3 3 2003-1-1
GROUP BY W.department_id 4 Smith 24000 4 3 2005-1-1
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 10: In each department, find the highest salary of the employee in that department.
employee_id name salary employee_id department_id since
SELECT 1 Jones 26000 1 2001-1-1
1
FROM Employee E, Works_in W 2 Smith 28000 2 2002-4-1
WHERE E.employee_id = W.employee_id 2 Smith 28000 2 2 2005-2-2
3 Parker 35000 3 2003-1-1
GROUP BY W.department_id 4 Smith 24000 4
3
2005-1-1
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 10: In each department, find the highest salary of the employee in that department.
employee_id name salary employee_id department_id since
SELECT MAX([Link]), W.department_id 1 Jones 26000 1 2001-1-1
1
FROM Employee E, Works_in W 2 Smith 28000 2 2002-4-1
WHERE E.employee_id = W.employee_id 2 Smith 28000 2 2 2005-2-2
3 Parker 35000 3 2003-1-1
GROUP BY W.department_id 4 Smith 24000 4
3
2005-1-1
salary department_id
28000 1
28000 2
35000 3
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 11: Find the employee_id of all employees whose name includes the substring ‘one’.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 11: Find the employee_id of all employees whose name includes the substring ‘one’.
3. Query: Exercises
Step 4. Design SQL to access data for the application
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 11: Find the employee_id of all employees whose name includes the substring ‘one’.
SELECT employee_id
FROM Employee name
Jones
WHERE name LIKE ‘%one%’;
3. Query: Nested Query
• Nested queries have other subqueries embedded in them
SELECT
FROM
WHERE column IN ( )
SELECT SELECT
FROM FROM
WHERE column ? ALL ( ) WHERE column ? SOME ( )
SELECT
FROM
WHERE EXISTS ( )
SELECT
SELECT [Link]
FROM Employee E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1
SELECT
);
SELECT
• Query 2: Find the customer_id of all customer who have both an account and a
loan.
Owner Borrower Result
account_id customer_id customer_id load_id customer_id
A1 C1 C1 L3 C1
A1 C2 C4 L2 C2
A2 C2 C2 L1
SELECT
• Query 2: Find the customer_id of all customer who have both an account and a
loan.
Owner Borrower Result
account_id customer_id customer_id load_id customer_id
A1 C1 C1 L3 C1
A1 C2 C4 L2 C2
A2 C2 C2 L1
• Query 2: Find the customer_id of all customer who have both an account and a
loan.
Owner Borrower Result
account_id customer_id customer_id load_id customer_id
A1 C1 C1 L3 C1
A1 C2 C4 L2 C2
A2 C2 C2 L1
• Query 3: Find the customer_id of all customer who have a loan but not having an
account.
Owner Borrower Result
account_id customer_id customer_id load_id customer_id
A1 C1 C1 L3 C4
A1 C2 C4 L2
A2 C2 C2 L1
SELECT
• Query 3: Find the customer_id of all customer who have a loan but not having an
account.
Owner Borrower Result
account_id customer_id customer_id load_id customer_id
A1 C1 C1 L3 C4
A1 C2 C4 L2
A2 C2 C2 L1
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > ALL (
SELECT [Link]
FROM Department D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W department_id
WHERE W.employee_id = 4 3
)
);
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > ALL (
SELECT [Link]
budget
FROM Department D2
100000
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W department_id
WHERE W.employee_id = 4 3
)
);
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > ALL (
SELECT [Link]
FROM Department D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
);
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > ALL (
SELECT [Link]
FROM Department D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
);
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > ALL (
SELECT [Link]
FROM Department D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
);
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > ALL (
SELECT [Link]
FROM Department D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
);
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > ALL (
SELECT [Link]
FROM Department D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
);
SELECT
• Query 1 : Find the names of departments whose budget is greater than the budget
of every department where employee 4 has worked.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > ALL ( • Important NOTE: If nested query result is empty, then will > ALL
SELECT [Link]
return true for every [Link].
FROM Department D2
WHERE D2.department_id IN ( • Question: If the Food’s department_id = 4?
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
);
SELECT
• Query 2 : Find department names that have the greatest budget than all
departments.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT
• Query 2 : Find department names that have the greatest budget than all
departments.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] >= ALL (
SELECT [Link]
FROM Department D2
);
SELECT
• Query 2 : Find department names that have the greatest budget than all
departments.
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
SELECT [Link]
FROM Department D
WHERE [Link] > SOME ( • Important NOTE: If nested query result is empty, then will >
SELECT [Link]
SOME return false for every [Link].
FROM Department D2
WHERE D2.department_id IN ( • Question: If the Toys’s department_id = 4 and Tools’
SELECT W.department_id department_id = 5?
FROM Works_in W
WHERE W.employee_id = 2
)
);
SELECT
SELECT [Link]
FROM Employee E
WHERE EXISTS (
SELECT *
FROM Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1
);
SELECT
SELECT [Link]
FROM Employee E • The inner subquery could depend on the row currently
WHERE EXISTS ( examined in the outer query.
SELECT * • EXISTS is a boolean set-comparison operator that
FROM Works_in W returns false if the input set is empty and true otherwise.
WHERE E.employee_id = W.employee_id AND
W.department_id = 1
);
SELECT
SELECT SELECT
FROM FROM
WHERE column ? ALL ( ) WHERE column ? SOME ( )
SELECT
FROM
WHERE EXISTS ( )
• Subqueries are usually nested under WHERE clauses, may also be enclosed
under HAVING and FROM clauses (WHY?)
3. Query: Nested Query - Exercises
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 1: Find the employee_id and name of the employees who worked in the departments with budget
more than 100,000.
3. Query: Nested Query - Exercises
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 1: Find the employee_id and name of the employees who worked in the departments with budget
more than 100,000.
3. Query: Nested Query - Exercises
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 1: Find the employee_id and name of the employees who worked in the departments with budget
more than 100,000.
SELECT E.employee_id, [Link]
FROM Employee E, Works_in W
WHERE E.employee_id = W.employe_id AND employee_id name
1 Jones
W.department_id IN (
2 Smith
SELECT D.department_id
FROM Department D
WHERE [Link] > 100000
);
3. Query: Nested Query - Exercises
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 2: Find the name and budget of the department with the greatest budget.
3. Query: Nested Query - Exercises
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 2: Find the name and budget of the department with the greatest budget.
3. Query: Nested Query - Exercises
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
• Query 2: Find the name and budget of the department with the greatest budget.
SELECT [Link]
FROM Employee E
name
WHERE E.employee_id IN (
Smith
SELECT W.employee_id
FROM Works_in W
GROUP BY W.employee_id
HAVING count(*) >=2
);
3. Query: Nested Query - Exercises
Employee Works_in Department
employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
4 3 2005-1-1
A UNION B
A EXCEPT B B EXCEPT A
A INTERSECT B
3. Query - Set operation: UNION
• Query: Find the names of employees who work in department 1 or department 3.
SELECT [Link]
Employee who FROM Employee E, Works_in W
work in department 1 OR 3 WHERE E.employee_id = W.employee_id AND
( W.department_id = 1 OR
W.department_id = 3);
SELECT [Link]
FROM Employee E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1
UNION
SELECT [Link]
Employees who work Employees who
FROM Employee E, Works_in W
in department 1 work in department 3
WHERE E.employee_id = W.employee_id AND
W.department_id = 3;
• Note: The two SQLs are NOT equivalent to each other! Duplicates are eliminated when two sets are unified.
3. Query - Set operation: INTERSECT
• Query: Find the names of employees who work in department 1 and department 3.
Employee who
work in department 1 AND 3
SELECT [Link]
FROM Employee E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1
INTERSECT
SELECT [Link]
FROM Employee E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 3;
Employees who work Employees who
in department 1 work in department 3
Employee who
work in department 1 BUT NOT 3
SELECT [Link]
FROM Employee E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1
EXCEPT
SELECT [Link]
FROM Employee E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 3;
Employees who work Employees who
in department 1 work in department 3
SELECT *
FROM Employee E
LEFT OUTER JOIN Department D
ON E.department_id = D.department_id;
3. Query - JOIN - OUTER JOIN
• RIGHT OUTER JOIN
Employee Department Result
e_name department_id department_id d_name e_name department_id department_id d_name
Kit 31 31 CS Kit 31 31 CS
Ben 33 33 Civil Ben 33 33 Civil
John 33 34 ME John 33 33 Civil
Jolly 34 35 EEE Jolly 34 34 ME
Yvonne 34 Yvonne 34 34 ME
David NULL NULL NULL 35 EEE
SELECT *
FROM Employee E
RIGHT OUTER JOIN Department D
ON E.department_id = D.department_id;
3. Query - NULL value
• Handling null values is not trivial!
• null: value does not exist
3. Query - NULL value
• Use predict IS NULL to check for null values
• Query: Find all employee names for which the salary is unknown or
undermined.
Employee
employee_id name salary
1 Jones
SELECT name name
2 Smith 28000 FROM Employee Jones
3 Parker WHERE salary IS NULL; Parker
4 Smith 24000
3. Query - NULL value
• NULL value and Aggregation
Employee
employee_id name salary
1 Jones
SELECT AVG (salary) SUM(salary)
2 Smith 28000
3 Parker FROM Employee; 26000
4 Smith 24000
• All aggregate operations except COUNT(*) ignore tuples with null values on the
aggregated attributes.
3. Query - NULL value
• null: value does not exist
• UNKNOWN: True, False, UNKNOWN
• Assertions are checked whenever the involved tables are updated — VERY
EXPENSIVE!
Summary
• Data definition: CREATE TABLE, DROP TABLE, ALTER TABLE ADD/DROP
• Data modification: INSERT INTO, DELETE FROM, UPDATE SET CASE
• Query
• SELECT FROM WHERE
• Operations: Renaming AS, String %, _
• Functions: ORDER BY ASE/DESC, SUM, AVG, MAX, MIN, COUNT, GROUP BY
HAVING
• Nester Query: SELECT FROM WHERE XXX IN, ? ALL, ? SOME, EXISTS
• MORE: UNION/INTERSECT/EXCEPT, SELECT FROM INNER JOIN ON, SELECT
LEFT/RIGHT OUTER JOIN ON
• View: CREATE VIEW AS
• Authorization: GRANT ON TO, REVOKE ON FROM, CREATE ROLE
• Assertion: CREATE ASSERTION CHECK