2. Objectives
▪ Student can write a SQL script.
▪ Student can compose SQL queries using set (and
bag) operators, correlated subqueries, aggregation
queries.
▪ Student can manipulate proficiently on complex
queries
2
THE DATABASE LANGUAGE SQL
4. REVIEW
THE DATABASE LANGUAGE SQL 4
Getting
User
Requiremen
t
High-Level
Design
Relational
Database
Schema
Design
ER diagram Relational Database Schema
Relational
DBMS
Figure 4.1: The database modeling and implementation process
Studied:
- ER diagram
- Relational model
- Convert ERD Relational model
🡪
Now: we learn how to set up a relational database on
DBMS
5. REVIEW – Entity Relationship Diagram
COMPANY Database
THE DATABASE LANGUAGE SQL 5
6. Integrity constraints
▪ Purpose: prevent semantic inconsistencies in
data
▪ Kinds of integrity constraints:
1. Key Constraints (1 table): Primary key, Candidate
key (Unique)
2. Attribute Constraints (1 table): NULL/NOT NULL;
CHECK
3. Referential Integrity Constraints (2 tables):
FOREIGN KEY
4. Global Constraints (n tables): CHECK or CREATE
ASSERTION (self studying)
We will implement these constraints by SQL
THE DATABASE LANGUAGE SQL 6
7. Two strings are equal (=) if they are the same
sequence of characters
Other comparisons: <, >, ≤, ≤, ≠
Suppose a=a1a2…an and b=b1b2…bm are two
strings, the first is less than the second if ∃
k≤min(n,m):
◦ ∀i, 1≤i≤k: ai = bi, and
◦ ak+1<bk+1
Example
◦ fodder < foo
◦ bar < bargain
Comparison of Strings
THE DATABASE LANGUAGE SQL 7
8. Like or Not Like
Two special characters
▪% means any sequence of 0 or more
characters
▪_ means any one character
Pattern Matching in SQL
SELECT
FROM
WHERE s LIKE p;
SELECT
FROM
WHERE s NOT LIKE p;
THE DATABASE LANGUAGE SQL 8
9. Example 5.1:
◦ Find all employees named as ‘Võ Việt Anh’
Example 5.2
◦ Find all employees whose name is ended at
‘Anh’
Pattern Matching in SQL
THE DATABASE LANGUAGE SQL 9
10. USING ESCAPE keyword
◦ SQL allows us to specify any one character we
like as the escape character for a single pattern
◦ Example
◦ WHERE s LIKE ‘%20!%%’ ESCAPE !
◦ Or WHERE s LIKE ‘%20@%%’ ESCAPE @
🡺 Matching any s string contains the 20% string
◦ WHERE s LIKE ‘x%%x%’ ESCAPE x
🡺 Matching any s string that begins and ends with the
character %
Pattern Matching in SQL
THE DATABASE LANGUAGE SQL 10
11. Dates and times are special data types in SQL
A date constant’s presentation
◦ DATE ‘1948-05-14’
A time constant’s presentation
◦ TIME ‘15:00:02.5’
A combination of dates and times
◦ TIMESTAMP ‘1948-05-14 12:00:00’
Operations on date and time
◦ Arithmetic operations
◦ Comparison operations
Dates and Times
THE DATABASE LANGUAGE SQL 11
12. Null value: special value in SQL
Some interpretations
▪Value unknown: there is, but I don’t know what it is
▪Value inapplicable: there is no value that makes sense here
▪Value withheld: we are not entitled to know the value that
belongs here
Null is not a constant
Two rules for operating upon a NULL value in
WHERE clause
◦ Arithmetic operators on NULL values will return a NULL value
◦ Comparisons with NULL values will return UNKNOWN
Null Values
THE DATABASE LANGUAGE SQL 12
13. Truth table for True, False, and Unknown
We can think of TRUE=1; FALSE=0; UNKNOWN=1/2, so
◦ x AND y = MIN(x,y); x OR y = MAX(x, y); NOT x = 1-x
The Truth-Value UNKNOWN
x y x AND y x OR y NOT x
TRUE TRUE TRUE TRUE FALSE
TRUE UNKNOW
N
UNKNOW
N
TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
UNKNOW
N
TRUE UNKNOW
N
TRUE UNKNOW
N
UNKNOW
N
UNKNOW
N
UNKNOW
N
UNKNOW
N
UNKNOW
N
UNKNOW
N
FALSE FALSE UNKNOW
N
UNKNOW
N
FALSE TRUE FALSE TRUE TRUE
FALSE UNKNOW
N
FALSE UNKNOW
N
TRUE
THE DATABASE LANGUAGE SQL 13
14. SQL conditions in Where clause produce three
truth values: True, False, and Unknown
Those tuples which condition has the value True
become part of the answer
Those tuples which condition has the value False
or Unknown are excluded from the answer
The Truth-Value Unknown
THE DATABASE LANGUAGE SQL 14
15. ▪SQL (sequel) is a database language designed for managing
data in relational database management systems, and
originally based upon relational algebra.
▪There are many different dialects of SQL
▪Ansi SQL (or SQL-86), SQL-92, SQL-99
▪SQL:2003, SQL:2006, SQL:2008, SQL:2009
▪Transact-SQL (T-SQL) is Microsoft's and Sybase's
proprietary extension to SQL.
▪PL/SQL (Procedural Language/Structured Query
Language) is Oracle Corporation's procedural extension for
SQL and the Oracle relational database.
▪Today, SQL is accepted as the standard RDBMS language
SQL Overview
THE DATABASE LANGUAGE SQL 15
16. Data Definition Language - CREATE
▪ Database schema
Simple syntax: CREATE DATABASE dbname
Full syntax: https://docs.microsoft.com/en-us/sql/database
▪ Relation schema ~ table
CREATE TABLE tableName
(
fieldname1 datatype [integrity_constraints],
fieldname2 datatype [integrity_constraints],
….
)
Full syntax: https://docs.microsoft.com/en-us/sql/table
THE DATABASE LANGUAGE SQL 16
18. Data Definition Language – ALTER, DROP
▪ Used to modify the structure of table, database
▪Add more columns
ALTER TABLE tableName
ADD columnName datatype [constraint]
▪Remove columns
ALTER TABLE tableName
DROP columnName datatype [constraint]
▪Modify data type
ALTER TABLE tableName
ALTER columnName datatype [constraint]
THE DATABASE LANGUAGE SQL 18
19. Data Definition Language– ALTER, DROP
▪Add/remove constraints
ALTER TABLE tablename
ADD CONSTRAINT constraintName PRIMARY KEY
(<attribute list>);
ALTER TABLE tablename
ADD CONSTRAINT constraintName FOREIGN KEY (<attribute list>)
REFERENCES parentTableName (<attribute list>);
ALTER TABLE tablename
ADD CONSTRAINT constraintName CHECK (expressionChecking)
ALTER TABLE tablename
DROP CONSTRAINT constraintName
THE DATABASE LANGUAGE SQL 19
20. Data Definition Language– ALTER, DROP
▪ DROP TABLE tableName
▪ DROP DATABASE dbName
THE DATABASE LANGUAGE SQL 20
22. Data Manipulation Language (DML)
Key words: INSERT, UPDATE, DELETE, SELECT
INSERT INTO tableName
VALUES (<value 1>, ... <value n>)
INSERT INTO tableName(<listOfFields>)
VALUES (<value 1>, ... <value m>)
INSERT INTO tableName
SELECT listOfFields FROM another_tableName
THE DATABASE LANGUAGE SQL 22
23. Data Manipulation Language (DML)
UPDATE tableName
SET columnName = newValue
[WHERE condition]
Note: newValue could be a value/ an expression/
a SQL statement
THE DATABASE LANGUAGE SQL 23
◼Example: Update new salary and depNum
for the employee named ‘Mai Duy An’
24. Data Manipulation Language (DML)
DELETE FROM tableName
[WHERE condition]
TRUNCATE TABLE tableName
▪ What is difference between DELETE and TRUNCATE?
▪ What should we do before implement DELETE or TRUNCATE?
(referential integrity constraint)
▪Example:
▪remove a department named ‘Phòng Kế Toán’
▪remove a department which depNum is 7
THE DATABASE LANGUAGE SQL 24
25. Data Manipulation Language (DML)
SQL Queries and Relational Algebra
THE DATABASE LANGUAGE SQL 25
SELECT L
FROM R
WHERE C
πL(σC(R))
26. SELECT identifies what columns
▪ALL: Specifies that duplicate rows can appear in the result set. ALL is the default
▪DISTINCT: Specifies that only unique rows can appear in the result set. Null
values are considered equal for the purposes of the DISTINCT keyword
▪TOP n [ PERCENT ]:Specifies that only the first n rows are to be output from the
query result set. n is an integer between 0 and 4294967295. If PERCENT is also
specified, only the first n percent of the rows are output from the result set. When
specified with PERCENT, n must be an integer between 0 and 100
FROM identifies which table
The WHERE clause follows the FROM clause. Condition: is composed of column
names, expressions, constants, and a comparison operator
T-SQL : Basic Syntax for a simple SELECT
queries
SELECT [ ALL | DISTINCT ]
[ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
[FROM table]
[WHERE conditions]
THE DATABASE LANGUAGE SQL 26
27. Example 1: Listing all employees whose salary exceed at 50000
Common Query in SQL
◼ Example 2: Listing name and salary of all employees whose income
exceed 50000
THE DATABASE LANGUAGE SQL 27
28. Using alias name in select clause
Example 3:
◦ Listing full name and salary of all employees
whose income exceed 50000
Projection in SQL
THE DATABASE LANGUAGE SQL 28
29. Example 4
◦ List all under 40 year-old female or under 50
year-old male employees
Selection in SQL
THE DATABASE LANGUAGE SQL 29
30. Presenting the tuples produced by a query in sorted order
The order may be based on the value of any attribute
Syntax
Order by clause follows Where and any other clauses. The
ordering is performed on the result of the From, Where,
and other clauses, just before Select clause
Using keyword ASC for ascending order and DESC for
descending order
Ordering the Output
SELECT <list of attributes>
FROM <list of tables>
WHERE <conditions>
ORDER BY <list of attributes>
THE DATABASE LANGUAGE SQL 30
31. Example 6:
◦ Listing all employee by department number
ascreasingly, then by salary descreasingly
Ordering the Output
THE DATABASE LANGUAGE SQL 31
33. SQL allows we combine two or more relations
through joins, products, unions, intersections,
and differences
Queries Involving More Than One Relation
THE DATABASE LANGUAGE SQL 33
34. When data from more than one table in the
database is required, a join condition is used.
Simple way to couple relations: list each relation
in the From clause
Other clauses in query can refer to the attributes
of any of the relations in the From clause
Products and Joins in SQL
THE DATABASE LANGUAGE SQL 34
35. Example 7:
◦ List all employees who work on ‘Phòng Phần
mềm trong nước’ department
Products and Joins in SQL
THE DATABASE LANGUAGE SQL 35
36. … a query involves several relations, and there
are two or more attributes with the same name?
What we do if …
THE DATABASE LANGUAGE SQL 36
37. We may list a relation R as many times as we
need
We use tuple variables to refer to each
occurrence of R
Tuple Variables
THE DATABASE LANGUAGE SQL 37
38. Example 8:
◦ Find all cities in which our company is
Disambiguating Attributes
THE DATABASE LANGUAGE SQL 38
39. … a query involves two or more tuples from the
same relation?
Example 9:
◦ Find all those project numbers which have
more than two members
What we do if …
THE DATABASE LANGUAGE SQL 39
40. We combine relations using the set operations of
relational algebra: union, intersection, and
difference
SQL provides corresponding operators with
UNION, INTERSECT, and EXCEPT for , ∩, and
∪
-, respectively
Union, Intersection, Difference of Queries
THE DATABASE LANGUAGE SQL 40
41. Example 10.1
◦ Find all those employees whose name is begun
by ‘H’ or salary exceed 80000
Union, Intersection, Difference of Queries
THE DATABASE LANGUAGE SQL 41
42. Example 10.2
◦ Find all those normal employees, that is who do
not supervise any other employees
Union, Intersection, Difference of Queries
THE DATABASE LANGUAGE SQL 42
43. Example 10.3
◦ Find all employees who work on projectB and
projectC
Union, Intersection, Difference of Queries
THE DATABASE LANGUAGE SQL 43
45. One query can be used to help in the evaluation
of another
A query that is part of another is called a sub-
query
◦ Sub-queries return a single constant, this
constant can be compared with another value
in a WHERE clause
◦ Sub-queries return relations, that can be used
in WHERE clause
◦ Sub-queries can appear in FROM clauses,
followed by a tuple variable
Sub-queries
THE DATABASE LANGUAGE SQL 45
46. An atomic value that can appear as one
component of a tuple is referred to as a scalar
Let’s compare two queries for the same request
Sub-queries that Produce Scalar Values
THE DATABASE LANGUAGE SQL 46
47. Example 7: Find the employees of Phòng Phần
mềm trong nước department
Sub-queries that Produce Scalar Values
THE DATABASE LANGUAGE SQL 47
48. Example 11:
◦ Find the employees of Phòng Phần mềm trong
nước department
Sub-queries that Produce Scalar Values
THE DATABASE LANGUAGE SQL 48
49. Example 11:
◦ Find the employees of Phòng Phần mềm trong
nước department
Sub-queries that Produce Scalar Values
THE DATABASE LANGUAGE SQL 49
50. Example 11:
◦ Find the employees of Phòng Phần mềm trong
nước department
Sub-queries that Produce Scalar Values
THE DATABASE LANGUAGE SQL 50
51. Some SQL operators can be applied to a relation
R and produce a bool result
◦ (EXISTS R = True) R is not empty
⇔
◦ (s IN R = True) s is equal to one of the
⇔
values of R
◦ (s > ALL R = True) s is greater than every
⇔
values in unary R
◦ (s > ANY R = True) s is greater than at least
⇔
one value in unary R
Conditions Involving Relations
THE DATABASE LANGUAGE SQL 51
52. A tuple in SQL is represented by a list of scalar
values between ()
If a tuple t has the same number of components
as a relation R, then we may compare t and R
with IN, ANY, ALL
Conditions Involving Tuples
THE DATABASE LANGUAGE SQL 52
53. Example 12:
◦ Find the dependents of all employees of
department number 1
Sub-queries that Produce Scalar Values
THE DATABASE LANGUAGE SQL 53
54. To now, sub-queries can be evaluated once and
for all, the result used in a higher-level query
But, some sub-queries are required to be
evaluated many times
That kind of sub-queries is called correlated sub-
query
Note: Scoping rules for names
Correlated Sub-queries
THE DATABASE LANGUAGE SQL 54
55. Example 13:
◦ Find all those projects have the same location
with projectA
Correlated Sub-queries
THE DATABASE LANGUAGE SQL 55
56. Another example:
◦ Find the titles that have been used for two or
movies
SELECT title
FROM Movies Old
WHERE year < ANY
(SELECT year
FROM Movies
WHERE title =Old.title)
Correlated Sub-queries
THE DATABASE LANGUAGE SQL 56
57. In a FROM list we can use a parenthesized sub-
query
We must give it a tuple-variable alias
Example: Find the employees of Phòng Phần mềm
trong nước
SELECT *
FROM tblEmployee e,
(SELECT depNum
FROM tblDepartment
WHERE depName=N'Phòng phần mềm trong
nước') d
WHERE e.depNum=d.depNum
Sub-queries in FROM Clauses
THE DATABASE LANGUAGE SQL 57
58. SQL Join Expressions can be stand as a query
itself or can be used as sub-queries in FROM
clauses
Cross Join in SQL= Cartesian Product
◦ Syntax: R CROSS JOIN S;
◦ Meaning: Each tuple of R connects to each
tuple of S
Theta Join with ON keyword
◦ Systax: R JOIN S ON R.A=S.A;
◦ Meaning: Each tuple of R connects to those
tuples of S, which satisfy the condition after ON
keyword
SQL Join Expressions
THE DATABASE LANGUAGE SQL 58
59. Example 15.1
◦ Product two relations Department and
Employee
Example 15.2
◦ Find departments and employees who work in
those departments, respectively
SQL Join Expression
THE DATABASE LANGUAGE SQL 59
61. A natural join differs from a theta-join in that:
◦ The join condition: all pairs of attributes from
the two relations having a common name are
equated, and there are no other condition
◦ One of each pair of equated attributes is
projected out
◦ Syntax : Table1 NATURAL JOIN Table2
◦ Microsoft SQL SERVER DONOT SUPPORT
NATURAL JOINS AT ALL
Natural Joins
THE DATABASE LANGUAGE SQL 61
62. The outer join is a way to augment the result of
join by the dangling tuples, padded with null
values
When padding dangling tuples from both of its
arguments, we use full outer join
When padding from left/right side, we use left
outer join/right outer join
Outer Joins
THE DATABASE LANGUAGE SQL 62
63. Example 17.1:
◦ For each location, listing the projects that are
processed in it
Outer joins
THE DATABASE LANGUAGE SQL 63
64. Example 17.2:
◦ For each department, listing the projects that it
controls
Outer joins
THE DATABASE LANGUAGE SQL 64
65. Study some operations that acts on relations as
whole, rather than on tuples individually
6.4 Full-Relation Operations
THE DATABASE LANGUAGE SQL 65
66. A relation, being a set, cannot have more than
one copy of any given tuple
But, the SQL response to a query may list the
same tuple several times, that is, SELECT
preserves duplicates as a default
So, by DISTINCT we can eliminate a duplicates
from SQL relations
Eliminating Duplicates
THE DATABASE LANGUAGE SQL 66
67. Example 17.3: List all location in which the
projects are processed.
◦ Location name is repeated many times
SELECT DISTINCT l.locNum, l.locName
FROM tblLocation l JOIN tblProject p ON l.locNum=p.locNum
SELECT DISTINCT l.locNum, l.locName
FROM tblLocation l JOIN tblProject p ON l.locNum=p.locNum
Eliminating Duplicates
THE DATABASE LANGUAGE SQL 67
68. Set operations on relations will eliminate
duplicates automatically
Use ALL keyword after Union, Intersect, and
Except to prevent elimination of duplicates
Syntax:
R UNION ALL S;
R INTERSECT ALL S;
R EXCEPT ALL S;
Duplicates in Unions, Intersections, and
Differences
THE DATABASE LANGUAGE SQL 68
69. Grouping operator partitions the tuples of relation
into groups, based on the values of tuples in one
or more attributes
After grouping the tuples of relation, we are able
to aggregate certain other columns of relation
We use GROUP BY clause in SELECT statement
Grouping and Aggregation in SQL
THE DATABASE LANGUAGE SQL 69
70. Five aggregation operators
◦ SUM acts on single numeric column
◦ AVG acts on single numeric column
◦ MIN acts on single numeric column
◦ MAX acts on single numeric column
◦ COUNT act on one or more columns or all of
columns
Eliminating duplicates from the column before
applying the aggregation by DISTINCT keyword
Aggregation Operators
THE DATABASE LANGUAGE SQL 70
71. Example 18.1
◦ Find average salary of all employees
Example 18.2
◦ Find number of employees
Aggregation Operators
THE DATABASE LANGUAGE SQL 71
72. To partition the tuples of relation into groups
Syntax
Grouping
SELECT <list of attributes>
FROM <list of tables>
WHERE <condition>
GROUP BY <list of attributes>
THE DATABASE LANGUAGE SQL 72
73. Example 19.1:
◦ Group employees by department number
Example 19.2
◦ List number of employees for each department
number
Grouping
THE DATABASE LANGUAGE SQL 73
74. There are two kinds of terms in SELECT clause
◦ Aggregations, that applied to an attribute or
expression involving attributes
◦ Grouping Attributes, that appear in GROUP BY
clause
A query with GROUP BY is interpreted as follow:
◦ Evaluate the relation R expressed by the
FROM and WHERE clauses
◦ Group the tuples of R according to the
attributes in GROUP BY clause
◦ Produce as a result the attributes and
aggregation of the SELECT clause
Grouping
THE DATABASE LANGUAGE SQL 74
75. Example 20
◦ Compute the number of employees for each
project
Grouping
THE DATABASE LANGUAGE SQL 75
76. When tuples have nulls, there are some rules:
◦ The value NULL is ignored in any aggregation
◦ Count(*): a number of tuples in a relation
◦ Count(A): a number of tuples with non-NULL values
for A attribute
◦ NULL is treated as an ordinary value when
forming groups
◦ The count of empty bag is 0, other aggregation
of empty bag is NULL
Grouping, Aggregation, and Nulls
THE DATABASE LANGUAGE SQL 76
77. Example: Suppose R(A,B) as followed
Grouping, Aggregation, and Nulls
A B
NULL NULL
The result of query
SELECT A, count(B)
FROM R
GROUP BY A;
is one tuple (NULL,0)
The result of query
SELECT A, sum(B)
FROM R
GROUP BY A;
is one tuple (NULL,NULL)
THE DATABASE LANGUAGE SQL 77
78. If we want to apply conditions to tuples of
relations, we put those conditions in WHERE
clause
If we want to apply conditions to groups of tuples
after grouping, those conditions are based on
some aggregations, how can we do?
In that case, we follow the GROUP BY clause
with a HAVING clause
Considerations …
THE DATABASE LANGUAGE SQL 78
79. Syntax:
HAVING clause
SELECT <list of attributes>
FROM <list of tables>
WHERE <conditions on tuples>
GROUP BY <list of attributes>
HAVING <conditions on groups>
THE DATABASE LANGUAGE SQL 79
80. Example 21:
◦ Print the number of employees for each those
department, whose average salary exceeds
80000
HAVING clause
THE DATABASE LANGUAGE SQL 80
81. Some rules about HAVING clause
◦ An aggregation in a HAVING clause applies
only to the tuples of the group being tested
◦ Any attribute of relations in the FROM clause
may be aggregated in the HAVING clause, but
only those attributes that are in the GROUP BY
list may appear un-aggregated in the HAVING
clause (the same rule as for the SELECT
clause)
HAVING clause
THE DATABASE LANGUAGE SQL 81
82. Example:
SELECT proNum, COUNT(empSSN) AS Number_Of_Employees,
FROM tblWorksOn
GROUP BY proNum
HAVING AVG(workHours)>20
SELECT proNum, COUNT(empSSN) AS Number_Of_Employees,
FROM tblWorksOn
GROUP BY proNum
HAVING proNum=4
HAVING clause
THE DATABASE LANGUAGE SQL 82