CSE 480: Database Systems
Lecture 11: SQL
1
SQL Query
SELECT <attribute list>
FROM <table list>
WHERE <condition that must be satisfied by a tuple>
– In MySQL, FROM and WHERE clauses are optional
– Example:
2
SELECT Clause
SELECT <attribute list>
Attribute list may include
– * (wildcard)
– Keywords such as AS and DISTINCT
– Arithmetic expression
– String functions
– Boolean expression
3
Wildcard(*) in SELECT-Clause
Retrieve the values for all columns of the selected tuples
4
AS in SELECT-Clause
Rename the columns in query result
Example: Find the names of employees who earn more
than their supervisors
SELECT [Link] AS EMP_FNAME, [Link] AS EMP_LNAME
FROM EMPLOYEE E, EMPLOYEE S
WHERE [Link]=[Link] AND [Link] >
[Link];
Output: EMP_FNAME EMP_LNAME
5
DISTINCT in SELECT-Clause
Eliminate duplicate tuples in query result
6
Arithmetic Expression in SELECT-Clause
Query: Show the effect of giving employees a 10% raise
7
String functions in SELECT-Clause
8
String functions in SELECT-Clause
9
String functions in SELECT-Clause
For more string functions, go to
[Link]
10
Boolean Expression in SELECT-Clause
11
FROM Clause
SELECT <attribute list>
FROM <table list>
Table list may include
– Names of 1 or more tables
– Subquery for joined tables
12
Joined Relations in FROM-Clause
Query 1: Retrieve the name and address of all employees who work
for the 'Research' department
SELECT Fname, Lname, Address
FROM EMPLOYEE, DEPARTMENT
WHERE Dname='Research' AND Dno=Dnumber
is equivalent to:
SELECT Fname, Lname, Address
FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)
WHERE Dname='Research'
13
Joined Relations in FROM-Clause
Many types of Join
– regular JOIN
– NATURAL JOIN
– CROSS JOIN
– LEFT OUTER JOIN
– RIGHT OUTER JOIN,
– etc
14
JOIN
R SELECT *
FROM R JOIN S on [Link] = S. Id S
Id Name
Id Value
111 John
foreach row r in table R 111 B
222 Mary
foreach row s in table S 111 A
333 Bill
if [Link] = [Link] then 222 A
444 Joe
output the merged
row of r and s
Id Name Id Value
111 John 111 B
111 John 111 A
222 Mary 222 A
15
JOIN – Example
Student Transcript
Id Name Addr Status StudId CrsCode Sem Grade
111 John ….. ….. 111 CSE305 S00 B
222 Mary ….. ….. 111 CSE306 S99 A
333 Bill ….. ….. 222 CSE304 F99 A
444 Joe ….. …..
SELECT * FROM Student JOIN Transcript on Id = StudId
Id Name Addr Status StudId CrsCode Sem Grade
111 John ….. ….. 111 CSE305 S00 B
111 John ….. ….. 111 CSE306 S99 A
222 Mary ….. ….. 222 CSE305 F99 A
Produces columns (attributes) with identical values, which is redundant
16
Natural Join
Join condition equates all attributes with the same name
Duplicate columns are automatically eliminated from result
R SELECT *
FROM R NATURAL JOIN S S
Id Name
foreach row r in table R Id Value
111 John
foreach row s in table S 111 B
222 Mary Examine their common attributes 111 A
333 Bill If their values are the same, then 222 A
444 Joe merge the rows while removing
their duplicate columns
Id Name Value
111 John B
111 John A
222 Mary A
17
Natural Join
Join attribute must have the same name (Id)
Student Transcript2
Id Name Addr Status Id CrsCode Sem Grade
111 John ….. ….. 111 CSE305 S00 B
222 Mary ….. ….. 111 CSE306 S99 A
333 Bill ….. ….. 222 CSE304 F99 A
444 Joe ….. …..
SELECT * FROM Student NATURAL JOIN Transcript2
Id Name Addr Status CrsCode Sem Grade
111 John ….. ….. CSE305 S00 B Duplicate attribute
111 John ….. ….. CSE306 S99 A (Id) was removed
222 Mary ….. ….. CSE305 F99 A
18
Cross Join
R SELECT *
FROM R CROSS JOIN S S
Id Name
Id Value
111 John
foreach row r in table R 111 B
222 Mary
foreach row s in table S 111 A
333 Bill
output the merged row r and s 222 A
444 Joe
Id Name Id Value
111 John 111 B
111 John 111 A
111 John 222 A
Output has 12 rows
… … … …
444 Joe 222 A
19
Left Outer Join
SELECT *
R FROM R LEFT OUTER JOIN S S
Id Name ON [Link] = [Link]
Id Value
111 John foreach row r in table R 111 B
222 Mary foreach row s in table S. 111 A
333 Bill if [Link] = [Link] then
222 A
444 Joe output the merged row r and s
if row r is not merged with any rows in S
output row r with NULL values for s
Id Name Id Value
111 John 111 B
111 John 111 A Output has 5 rows
222 Mary 222 A
333 Bill NULL NULL
444 Joe NULL NULL
20
Left Outer Join - Example
Retrieve the name of all employees and their supervisors
SELECT [Link], [Link], [Link], [Link]
FROM (EMPLOYEE E LEFT OUTER JOIN
EMPLOYEE S ON [Link]=[Link])
21
Right Outer Join
SELECT *
R FROM R RIGHT OUTER JOIN S S
Id Name ON [Link] = [Link]
Id Value
111 John foreach row s in table S 111 B
222 Mary foreach row r in table R 111 A
333 Bill if [Link] = [Link] then
555 A
444 Joe output the merged row r and s
if row s is not merged with any rows in R
output row s with NULL values for r
Id Name Id Value
111 John 111 B
111 John 111 A Output has 3 rows
NULL NULL 555 A
22
WHERE-Clause
WHERE <condition>
Selection condition is a Boolean expression
– Simple selection condition:
<attribute> operator <constant>
<attribute> operator <attribute>
<attribute> operator <set> or <attribute> operator <relation>
– Complex conditions:
<condition> AND <condition>
<condition> OR <condition>
NOT <condition>
23
Boolean Expression in WHERE-Clause
<attribute> operator <constant>
<attribute> operator <attribute>
– Operator: =, >, <, >=, <=, <> (not equal to)
– Applicable to integers, floats, strings, dates, etc. (except for
NULL)
SELECT *
FROM EMPLOYEE
WHERE SSN > SUPERSSN AND SALARY > 49999.99
AND MINIT='B' AND LNAME='SMITH'
AND BDATE >= '1980-01-01';
24
Substring Comparison in WHERE-Clause
Find employees who live in “Houston, TX”.
Use the LIKE operator to compare partial strings
Two reserved characters are used:
– % matches an arbitrary number of characters
– _ matches a single arbitrary character
25
Substring Comparison
Query: Retrieve all employees whose address is in
Houston, Texas.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE ADDRESS LIKE '%Houston, TX%‘;
26
Substring Comparison
Query: Retrieve all employees who were born during the
1950s.
Q26: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE BDATE LIKE '195_ - _ _ - _ _‘;
27
Arithmetic Expression in WHERE-Clause
Query: Retrieve the names of employees who earn more
than half the salary of their supervisors
SELECT [Link], [Link]
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE [Link] > [Link]/2
AND [Link] = [Link];
28
Arithmetic Expression in WHERE-Clause
Between comparison operator
Query: Retrieve the first and last names of employees in
department 5 whose salary is between $30,000 and
$40,000.
SELECT Fname, Lname
FROM EMPLOYEE
WHERE (Salary BETWEEN 30000 AND 40000)
AND Dno = 5
29
UNSPECIFIED WHERE-clause
If there is only one relation in the FROM-clause and there
is no join condition, this implies all tuples of the relation
are selected
SELECT SSN
FROM EMPLOYEE;
If more than one relation is specified in the FROM-clause
and there is no join condition, then the CARTESIAN
PRODUCT (Cross Join) of tuples is selected
SELECT SSN, DNAME
FROM EMPLOYEE, DEPARTMENT;
30
NULLS IN SQL QUERIES
Cannot use equality (=) comparison to check for null values
Query: Retrieve the names of employees who do not have
supervisors
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SUPERSSN IS
NULL;
Query: Retrieve the names of employees who have supervisors
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SUPERSSN IS NOT
NULL;
31
SET OPERATIONS
SQL has directly incorporated some set operations
– UNION
– INTERSECT
– MINUS or EXCEPT
The resulting relations of these set operations are sets of tuples;
duplicate tuples are eliminated from the result
The set operations are applicable to union compatible relations
– the two SQL relations must have the same attributes and the attributes
must appear in the same order
Note: MySQL supports only UNION; Oracle supports all 3 set
operations
32
Example in Oracle
Duplicate rows are eliminated
Not available
in MySQL
33
Example
Query: List the names of projects that involve an
employee whose last name is 'Smith' as a worker or as a
manager of the department that controls the project
34
Example
Query: List the names of projects that involve an
employee whose last name is 'Smith' as a worker or as a
manager of the department that controls the project
(SELECT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME='Smith')
UNION
(SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN
AND NAME='Smith');
35
Example
Query: List the first name and last name of employees
who do not work on any project
36
Example
Query: List the first name and last name of employees
who do not work on any project
(SELECT Fname, Lname
FROM EMPLOYEE)
MINUS
(SELECT Fname, Lname
FROM WORKS_ON, EMPLOYEE
WHERE ESSN=SSN);
Caution: Not applicable in MySQL
(see slide 40 on how to write this query in MySQL)
37
IN Operator
v IN W
The comparison operator IN compares a value v with a
set of values W, and evaluates to TRUE if v is one of the
elements in W. This is SET membership test.
Examples:
– 3 in {1, 2, 3} TRUE
– 0 in {1, 2, 3} FALSE
38
IN Operator
Query: Retrieve the social security numbers of all
employees who work on project number 1, 2, or 3
SELECT DISTINCT ESSN
FROM WORKS_ON
WHERE (PNO = 1) OR (PNO = 2) OR (PNO = 3);
Using IN Operator:
SELECT DISTINCT ESSN
FROM WORKS_ON
WHERE PNO IN (1, 2, 3);
39
Example
Query: List the first name and last name of employees
who do not work on any project
SELECT Fname, Lname
FROM EMPLOYEE
WHERE SSN NOT IN
(SELECT ESSN FROM WORKS_ON);
40
Exercise
Find the names of supervisors who are also managers
Find the names of employees who are not supervisors
Find the names of supervisors who supervise exactly one
employee
Try to write it in two ways:
– Using IN operator
– Using SET operations (UNION, INTERSECT, MINUS)
41