0% found this document useful (0 votes)
3 views

Chapter 3 - SQL_PartB

sql

Uploaded by

YouTubeATP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 3 - SQL_PartB

sql

Uploaded by

YouTubeATP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Chapter 3B.

Structured Query
Language (SQL II)
COMP3278 Introduction to
Database Management Systems

Department of Computer Science, The University of Hong Kong


Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/
Content
Revision
Set operations
More on nested queries
Null values
Views
Authorization
Assertion
Other SQL constructs
2
In this chapter…
Outcome 1. Information Modeling
Able to understand the modeling of real life information in a database
system.

Outcome 2. Query Languages


Able to understand and use the languages designed for data access.

Outcome 3. System Design


Able to understand the design of an efficient and reliable database
system.

Outcome 4. Application Development


Able to implement a practical application on a real database.
3
Revision
Step 1. Information modeling (Chapter 2A).
employee_id since department_id

employee works_in department

name salary budget name

Before we proceed to learning more


SQL constructs, lets have a revision
on what we have learned up to this
chapter.
4
Revision
Step 1. Information modeling (Chapter 2A).
employee_id since department_id

employee works_in department

name salary budget name

Step 2. Reduce to database tables (Chapter 2B).


Employees ( employee_id, name, salary)
Foreign key : none.
Departments ( department_id, name, budget)
Foreign key : none.
Works_in( employee_id, department_id, since)
Foreign key : employee_id REFERENCES Employee (employee_id).
department_id REFERENCES Department (department_id).
5
Revision
Step 3. Create the database (Chapter 3A).
CREATE TABLE Employees (
employee_id INT(12),
name VARCHAR(30) NOT NULL, INNODB storage engine,
salary INT UNSIGNED NOT NULL, just for MySQL to support
PRIMARY KEY(employee_id) foreign key constraints.
)ENGINE = INNODB;

CREATE TABLE Departments (


department_id INT(12),
name VARCHAR(30) NOT NULL,
budget INT UNSIGNED NOT NULL,
PRIMARY KEY(department_id)
) ENGINE = INNODB;

CREATE TABLE Works_in(


employee_id INT(12),
department_id INT(12),
since DATE NOT NULL,
PRIMARY KEY(employee_id, department_id),
FOREIGN KEY (employee_id) REFERENCES Employees (employee_id),
FOREIGN KEY (department_id) REFERENCES Departments (department_id)
6
) ENGINE = INNODB;
Revision
Step 3. Create the database (Chapter 3A).

INSERT INTO Employees VALUES ( 1, 'Jones', 26000);


INSERT INTO Employees VALUES ( 2, 'Smith', 28000);
INSERT INTO Employees VALUES ( 3, 'Parker', 35000);
INSERT INTO Employees VALUES ( 4, 'Smith', 24000);

INSERT INTO Departments 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');

Employees Works_in Departments


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
7
4 3 2005-1-1
Revision
Step 4. Design the SQL to access data for the
application (Chapter 3A).
Query 1: Find the names of all employees and remove
duplicates.

SELECT DISTINCT name Where can I find the name of all


FROM Employees; employees?

Employees Works_in Departments


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
8
4 3 2005-1-1
Exercises
Query 2: Find the employee_ids and names of employees who
work in department with department_id=2.

SELECT E.employee_id, E.name How to find the employee_id(s) of


FROM Works_in W , Employees E the employee in department #2?
WHERE W.department_id = 2 AND
E.employee_id = W.employee_id

Employees Works_in Departments


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
9
4 3 2005-1-1
Exercises
Query 2: Find the employee_ids and names of employees who
work in department with department_id=2.

SELECT E.employee_id, E.name How to find the employee_id(s) of


FROM Works_in W , Employees E the employee in department #2?
WHERE W.department_id = 2 AND
E.employee_id = W.employee_id;
Given an employee_id, find the
name of the employee.

Employees Works_in Departments


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
10
4 3 2005-1-1
Exercises
Query 3: Find the dept. names where employee with
employee_id = 2 works.

SELECT D.name What are the dpt. id(s) of the


FROM Works_in W, Departments D dpt(s) where employee #2 works?
WHERE W.employee_id = 2 AND
D.department_id = W.department_id;

Employees Works_in Departments


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
11
4 3 2005-1-1
Exercises
Query 3: Find the dept. names where employee with
employee_id = 2 works.

SELECT D.name What are the dpt. id(s) of the


FROM Works_in W, Departments D dpt(s) where employee #2 works?
WHERE W.employee_id = 2 AND
D.department_id = W.department_id;
Given a department_id, find the
name of the department.

Employees Works_in Departments


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
12
4 3 2005-1-1
Exercises
Query 4: Find the dept. ids where employees named Smith
work.
How to find the employee_id(s) of
SELECT W.department_id employee named Smith?
FROM Employees E, Works_in W
WHERE E.name = 'Smith' AND
E.employee_id = W.employee_id;

Employees Works_in Departments


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
13
4 3 2005-1-1
Exercises
Query 4: Find the dept. ids where employees named Smith
work.
How to find the employee_id(s) of
SELECT W.department_id employee named Smith?
FROM Employees E, Works_in W
WHERE E.name = 'Smith' AND
E.employee_id = W.employee_id;
Given an employee_id, find that
employee’s department id(s).

Employees Works_in Departments


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
14
4 3 2005-1-1
Exercises
Query 5: Find the dept. names where employees named
Smith work.
How to find the employee_id(s)
SELECT D.name of employee named Smith?
FROM Employees E, Works_in W, Departments D
WHERE E.name = 'Smith' AND
E.employee_id = W.employee_id AND
W.department_id = D.department_id;

Employees Works_in Departments


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
15
4 3 2005-1-1
Exercises
Query 5: Find the dept. names where employees named
Smith work.
How to find the employee_id(s)
SELECT D.name of employee named Smith?
FROM Employees E, Works_in W, Departments D
WHERE E.name = 'Smith' AND Given an employee_id, find the
E.employee_id = W.employee_id AND dept. id (s) of that employee.
W.department_id = D.department_id;

Employees Works_in Departments


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
16
4 3 2005-1-1
Exercises
Query 5: Find the dept. names where employees named
Smith work.
How to find the employee_id(s)
SELECT D.name of employee named Smith?
FROM Employees E, Works_in W, Departments D
WHERE E.name = 'Smith' AND Given an employee_id, find the
E.employee_id = W.employee_id AND dept. id (s) of that employee.
W.department_id = D.department_id;
Given a dept. id, find the name of
that department.

Employees Works_in Departments


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
17
4 3 2005-1-1
Exercises
Query 6: Find the names of departments which have an
employee named Smith and their budget is greater than 100000.
SELECT D.name How to find the dept.id(s) of the
FROM Departments D, Employees E, Works_in W dept with budget > 100000?
WHERE D.budget > 100000 AND
E.name = 'Smith' AND
W.employee_id = E.employee_id AND
D.department_id = W.department_id;

Employees Works_in Departments


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
18
4 3 2005-1-1
Exercises
Query 6: Find the names of departments which have an
employee named Smith and their budget is greater than 100000.
SELECT D.name How to find the dept.id(s) of the
FROM Departments D, Employees E, Works_in W dept with budget > 100000?
WHERE D.budget > 100000 AND
E.name = 'Smith' AND How to find the dept.id(s) of the
W.employee_id = E.employee_id AND dept with employee ‘Smith’?
D.department_id = W.department_id;

Employees Works_in Departments


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
19
4 3 2005-1-1
Exercises
Query 6: Find the names of departments which have an
employee named Smith and their budget is greater than 100000.
SELECT D.name How to find the dept.id(s) of the
FROM Departments D, Employees E, Works_in W dept with budget > 100000?
WHERE D.budget > 100000 AND
E.name = 'Smith' AND How to find the dept.id(s) of the
W.employee_id = E.employee_id AND dept with employee ‘Smith’?
D.department_id = W.department_id;
Given a dept.id, find the name of
the department.
Employees Works_in Departments
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
20
4 3 2005-1-1
Exercises
Query 7: Find the budgets of departments, who employ an
employee called ‘Smith’ .
How to find the employee_id(s)
SELECT D.budget
of the employee named ‘Smith’?
FROM Employees E, Works_in W, Departments D
WHERE E.name = 'Smith' AND
E.employee_id = W.employee_id AND
W.department_id = D.department_id;

Employees Works_in Departments


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
21
4 3 2005-1-1
Exercises
Query 7: Find the budgets of departments, who employ an
employee called ‘Smith’ .
How to find the employee_id(s)
SELECT D.budget
of the employee named ‘Smith’?
FROM Employees E, Works_in W, Departments D
WHERE E.name = 'Smith' AND
Given an employee_id, find the
E.employee_id = W.employee_id AND
department_id of the employee.
W.department_id = D.department_id;

Employees Works_in Departments


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
22
4 3 2005-1-1
Exercises
Query 7: Find the budgets of departments, who employ an
employee called ‘Smith’ .
How to find the employee_id(s)
SELECT D.budget
of the employee named ‘Smith’?
FROM Employees E, Works_in W, Departments D
WHERE E.name = 'Smith' AND
Given an employee_id, find the
E.employee_id = W.employee_id AND
department_id of the employee.
W.department_id = D.department_id;

Given a department_id, find the


budget of the department.

Employees Works_in Departments


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
23
4 3 2005-1-1
Exercises
Query 8: For each department, find the total number of
employees it employs.

SELECT W.department_id, COUNT(*)


FROM Works_in W
GROUP BY W.department_id;

Employees Works_in Departments


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
24
4 3 2005-1-1
Exercises
Query 9: Find the dept. names with at least 2 employee.

SELECT COUNT(*), D.name Find the number of employees in


FROM Works_in W, Departments D each department.
WHERE D.department_id = W.department_id
GROUP BY D.department_id
HAVING COUNT(*) >= 1;

Employees Works_in Departments


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
25
4 3 2005-1-1
Exercises
Query 9: Find the dept. names with at least 2 employee.

SELECT COUNT(*), D.name Find the number of employees in


FROM Works_in W, Departments D each department.
WHERE D.department_id = W.department_id
GROUP BY D.department_id Find the department with
HAVING COUNT(*) >= 2; #employee >= 2.

Employees Works_in Departments


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
26
4 3 2005-1-1
Exercises
Query 9: Find the dept. names with at least 2 employee.

SELECT COUNT(*), D.name Find the number of employees in


FROM Works_in W, Departments D each department.
WHERE D.department_id = W.department_id
GROUP BY D.department_id Find the department with
HAVING COUNT(*) >= 2; #employee >= 2.

Given a department_id, find the


name of the department.

Employees Works_in Departments


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
27
4 3 2005-1-1
Exercises
Query 10: Find the employee_id of all employees whose name
includes the substring “one”.

SELECT employee_id
FROM Employees
WHERE name LIKE '%one%';

Employees Works_in Departments


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
28
4 3 2005-1-1
Exercises
Query 11: Find the employee_id and name of the employees
who worked in the departments with budget more than
100,000. Find the department_id(s) of the
SELECT DISTINCT E.employee_id, E.name department with budget > 100000.
FROM Employees E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id IN (
SELECT department_id
FROM Departments
WHERE budget > 100000);

Employees Works_in Departments


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
29
4 3 2005-1-1
Exercises
Query 11: Find the employee_id and name of the employees
who worked in the departments with budget more than
100,000. Find the department_id(s) of the
SELECT DISTINCT E.employee_id, E.name department with budget > 100000.
FROM Works_in W, Employees E
WHERE E.employee_id = W.employee_id AND Given a dpt. id, find the id of the
W.department_id IN ( employees in that dpt.
SELECT department_id
FROM Departments
WHERE budget > 100000);

Employees Works_in Departments


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
30
4 3 2005-1-1
Exercises
Query 11: Find the employee_id and name of the employees
who worked in the departments with budget more than
100,000. Find the department_id(s) of the
SELECT DISTINCT E.employee_id, E.name department with budget > 100000.
FROM Works_in W, Employees E
WHERE E.employee_id = W.employee_id AND Given a dpt. id, find the id of the
W.department_id IN ( employees in that dpt.
SELECT department_id
FROM Departments Given an employee_id, find the
WHERE budget > 100000); name of the employee.
Employees Works_in Departments
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
31
4 3 2005-1-1
Exercises
Query 12: Find the name and budget of the department with
the greatest budget.

SELECT D.name, D.budget Find the greatest budget among all


FROM Departments D departments.
WHERE D.budget = (
SELECT MAX(D.budget)
FROM Departments D
);

Employees Works_in Departments


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
32
4 3 2005-1-1
Exercises
Query 12: Find the name and budget of the department with
the greatest budget.

SELECT D2.name, D2.budget Find the greatest budget among all


FROM Departments D2 departments.
WHERE D2.budget = (
SELECT MAX(D.budget) Find the department with budget
FROM Departments D equals to the greatest budget.
);

Employees Works_in Departments


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
33
4 3 2005-1-1
Exercises
Query 13: Find the names of employees who work in at least 2
departments.
For each employee, find the
SELECT E.name number of departments that
FROM Employees E he/she works in.
WHERE 2 <= (

// Find the number of dpts that he/she works in.

);

Employees Works_in Departments


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
34
4 3 2005-1-1
Exercises
Query 13: Find the names of employees who work in at least 2
departments.
For each employee, find the
SELECT E.name number of departments that
FROM Employees E he/she works in.
WHERE 2 <= (
SELECT COUNT(W.department_id)
FROM Works_in W
WHERE W.employee_id = E.employee_id
);

Employees Works_in Departments


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
35
4 3 2005-1-1
Exercises
Query 14: In each department, find the highest salary of the
employee in that department.

SELECT MAX(E.salary) , W.department_id


FROM Employees E, Works_in W
WHERE E.employee_id = W.employee_id
GROUP BY W.department_id;

Employees Works_in Departments


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
36
4 3 2005-1-1
Section 1

Set Operations

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/
Set operations
Set operations can be expressed in SQL using clauses
UNION, INTERSECT, EXCEPT.
Using the set operations can ease the design of SQL by
breaking down a complex query to a number of
simpler sub-queries.
A UNION B

Answer of query A Answer of query B

38
A EXCEPT B A INTERSECT B B EXCEPT A
The UNION clause
Query: Find the names of employees who work in department 1
or department 3.
SELECT E.name
Employees Employees
FROM Employees E, Works_in W
who work in who work in WHERE E.employee_id= W.employee_id AND
dpt 1. dpt 3. (W. department_id = 1 OR
W. department_id = 3)

SELECT E.name
FROM Employees E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1
Employees who work in dpt 1 OR dpt 3
UNION

Note : The two SQLs are NOT SELECT E.name


equivalent to each other! FROM Employees E, Works_in W
Duplicates are eliminated when WHERE E.employee_id = W.employee_id AND
two sets are unified. W. department_id = 3
39
The INTERSECT clause
Query: Find the name of employees who work in department 1
and department 3.
Employees Employees
who work in who work in
dpt 1. dpt 3.

SELECT E.name
FROM Employees E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1

Employees who work in both dpt 1 AND dpt 3 INTERSECT

SELECT E.name
Note : MySQL doesn’t support the
FROM Employees E, Works_in W
keyword INTERSECT.
WHERE E.employee_id = W.employee_id AND
But we can replace INTERSECT by
W. department_id = 3
joining tables.
40
The EXCEPT clause
Query: Find the name of employees who work in department 1
but not department 3.
Employees Employees
who work in who work in
dpt 1. dpt 3.

SELECT E.name
FROM Employees E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1

Employees who work in dpt 1 but not in dpt 3 EXCEPT

SELECT E.name
Note : MySQL doesn’t support the
FROM Employees E, Works_in W
keyword EXCEPT.
WHERE E.employee_id = W.employee_id AND
But we can replace EXCEPT by
W. department_id = 3
using NOT IN.
41
Section 2

More on
Nested Queries

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/
Nested queries
Nested queries have other subqueries embedded in
them.
Used for the ease of expressing a natural language
request in SQL.

Subqueries are usually nested under WHERE clauses.


May also be enclosed under FROM or HAVING clauses

43
The IN clause (also mentioned in Ch. 3A)

Query: Find the names of the employees in department 1.


SELECT E.name
FROM Employees E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1;

In natural language : Find employee names whose


employee_id appears in the set of employee_ids working for
department 1.
SELECT E.name
FROM Employees E
WHERE E.employee_id IN ( Just like searching
SELECT W.employee_id the employee_id in
FROM Works_in W the result of the
WHERE W.department_id = 1); nested query.
44
The SOME clause
Query: Find department names that have greater budget
than some department where employee 4 works.
SELECT D.name
FROM Departments D
WHERE D.budget > SOME(
SELECT D2.budget
FROM Departments D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
); Find the
department ID where
employee 4 works.

45
The SOME clause
Query: Find department names that have greater budget
than some department where employee 4 works.
SELECT D.name
FROM Departments D
WHERE D.budget > SOME(
SELECT D2.budget
FROM Departments D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W Find the budget of
WHERE W.employee_id = 4 those departments.
)
); Find the
department ID where
employee 4 works.

46
The SOME clause
Query: Find department names that have greater budget
than some department where employee 4 works.
SELECT D.name Find the name of
FROM Departments D the department with
WHERE D.budget > SOME( budget > some
SELECT D2.budget budgets (those
FROM Departments D2 returned by inner
WHERE D2.department_id IN ( query).
SELECT W.department_id
FROM Works_in W Find the budget of
WHERE W.employee_id = 4 those departments.
)
); Find the
department ID where
IMPORTANT NOTE: If nested query result is employee 4 works.
empty, then > SOME will return false for
every D.budget! 47
The ALL clause
Query: Find department names that have greater budget
than all departments where employee 4 works.
SELECT D.name
FROM Departments D
WHERE D.budget > ALL (
SELECT D2.budget
FROM Departments D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
); Find the
department ID where
employee 4 works.

48
The ALL clause
Query: Find department names that have greater budget
than all departments where employee 4 works.
SELECT D.name
FROM Departments D
WHERE D.budget > ALL (
SELECT D2.budget
FROM Departments D2
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W Find the budget of
WHERE W.employee_id = 4 those departments.
)
);

49
The ALL clause
Query: Find department names that have greater budget
than all departments where employee 4 works.
SELECT D.name Find the name of
FROM Departments D the department with
WHERE D.budget > ALL ( budget > ALL budgets
SELECT D2.budget (those returned by
FROM Departments D2 inner query).
WHERE D2.department_id IN (
SELECT W.department_id
FROM Works_in W
WHERE W.employee_id = 4
)
);

IMPORTANT NOTE: If nested query result is empty, then


> ALL will return true for every D.budget!
50
The ALL clause
Query: Find department names that have the greatest
budget than all departments.
SELECT D.name
FROM Departments D
WHERE D.budget >= ALL (
SELECT D2.budget
FROM Departments D2
);

Question: What would the result be if >ALL is used?

Can you rewrite the above query


using Aggregate function MAX in a
nested query?

51
The EXISTS clause
The inner subquery could depend on the row
currently examined in the outer query.
Query: Find the names of employees who work in
department with department_id=1.
SELECT E.name For each employee
FROM Employees E
record r.
WHERE EXISTS (
SELECT * If the inner query
FROM Works_in W can return some
WHERE W.department_id = 1 AND records, we will return
E.employee_id = W.employee_id); the record r.

EXISTS is a boolean set-comparison operator that


returns false if the input set is empty and true
otherwise. 52
Section 3

Null values

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/
NULL value
Handling null values is a non-trivial topic in database
research.
null: unknown value or value does not exist.
Use predicate IS NULL to check for null values.
Query: Find all employee names for which the salary is
unknown or undetermined.
Employees
employee_id name salary SELECT name
1 Jones FROM Employees
2 Smith 28000
WHERE salary IS NULL
3 Parker
4 Smith 24000
54
NULL value
The result of any arithmetic expression involving null
is null.
5 + null returns null.

Any comparison with null returns UNKNOWN.


Both 5 < null , null = null return UNKNOWN.

Use P IS UNKNOWN to check if a predicate P is


unknown or not.
For the result of WHERE or HAVING clause, predicate
is false if it evaluates to UNKNOWN.
55
Three valued logic

OR AND NOT
T Un F T Un F T F
T T T T T T Un F Un Un
Un T Un Un Un Un Un F F T
F T Un F F F F F

56
NULL value and aggregates
SELECT SUM (budget)
FROM Departments

The statement above ignores null amounts.

All aggregate operations except COUNT(*) ignore


tuples with null values on the aggregated attributes.
COUNT counts not null values only.
Departments
department_id name budget
SUM(budget) returns 100000.
1 Toys
COUNT(*) returns 3. 2 Tools
3 Food 100000
COUNT(budget) returns 1. 57
Section 4

Views

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/
The CREATE VIEW clause
Views provide a mechanism to hide certain data from
the view of certain users. View level

Syntax: View 1 View 2 …


CREATE VIEW view_name AS <expression>

CREATE VIEW Employee_hide_salary AS (


SELECT employee_id, name
FROM Employees);
Logical level
CREATE VIEW Dpt_size(name,num_of_employee) AS (
SELECT D.name, COUNT(*)
FROM Departments D, Works_in W
WHERE D.department_id = W.department_id
GROUP BY W.department_id ); Physical level

59
Section 5

Authorization

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/
Authorization
The DBA can grant access/update authorization to
users.
Syntax: GRANT <priviledge list>
ON <table name or view name>
TO <user/role list>
Johnson, Brown are usernames.

GRANT SELECT ON Departments TO Johnson, Brown

GRANT UPDATE(budget) ON Departments TO Johnson

GRANT UPDATE(budget) ON Departments TO manager

manager is not a username, it


is a role.
61
Authorization
Rights can be revoked.
REVOKE SELECT ON Departments FROM Johnson, Brown

Create a role.
CREATE ROLE manager;

Grant a role to a user.


GRANT manager TO Brown;

62
Section 6

Assertion

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/
Assertions
An assertion ensures a certain condition will always
exist in the database.
Assume that we want to enforce that the number of
departments cannot exceed the number of
employees at any valid instance of our database.
CREATE ASSERTION EmpsNoLessThanDepts
CHECK (
(SELECT COUNT(*) FROM Departments)
<=
(SELECT COUNT(*) FROM Employees)
);

Assertions are checked every time the involved


tables are updated and they could be very expensive.
64
Section 7

Other topics
in SQL

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/
List of other topics
Join types: Full outer join, Inner join, Cross join…
TRIGGER (Recommended: https://www.mysqltutorial.org/mysql-triggers/)
Transaction controls.
Derived relations and the WITH clause.
Set operations: UNIQUE
More on foreign key constraints: ON DELETE CASCADE
More on DCL (Data Control Language) WITH GRANT
OPTION
66
Chapter 3B.

END
COMP3278 Introduction to
Database Management Systems

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Acknowledgement – Professor Nikos Mamoulis http://www.cs.hku.hk/~nikos/

You might also like