Chapter 3 - SQL_PartB
Chapter 3 - SQL_PartB
Structured Query
Language (SQL II)
COMP3278 Introduction to
Database Management Systems
INSERT INTO Departments VALUES ( 1, 'Toys', 122000), ( 2, 'Tools', 239000), ( 3, 'Food', 100000);
SELECT employee_id
FROM Employees
WHERE name LIKE '%one%';
);
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
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
SELECT E.name
FROM Employees E, Works_in W
WHERE E.employee_id = W.employee_id AND
W.department_id = 1
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
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.
43
The IN clause (also mentioned in Ch. 3A)
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
)
);
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.
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.
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
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
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.
Create a role.
CREATE ROLE manager;
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)
);
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/