Slide 9 - Advanced SQL Query (Part 1)
Slide 9 - Advanced SQL Query (Part 1)
Advanced
Acknowledgements
This slide is a modification to supplementary slide of
“Database System”, 7th edition, Elmasri/Navathe, 2015: Chapter 7 More SQL: Complex Queries,
Triggers, Views, and Schema Modification used in “Basis Data” course in academic years 2018/2019 in
the Faculty of Computer Science, Universitas Indonesia.
2
Review: SQL yang Sudah Di Pelajari
Cartesian Product
3
Outline
1. Join SQL
4
1. Join SQL
Unknown value
ex: A person’s date of birth is not known
Unavailable
ex: A person has a home phone but does not want it to be listed
5
1. Join SQL
...
6
... Join SQL
1.
IS or IS NOT NULL
Query 18. Retrieve the names of all employees who do not have supervisors.
Note: If a join condition is specified, tuples with NULL values for the join
attributes are not included in the result
7
... Join SQL
1.
Arithmetic Operations
The standard arithmetic operators '+', '-'. '*', and '/' (for addition, subtraction,
multiplication, and division, respectively) can be applied to numeric values
in an SQL query result
Query 13: Show the effect of giving all employees who work on the
'ProductX' project a 10% raise.
Q13:
8
... Join SQL
1.
Query 14: Retrieve all employees in department 5 whose salary is between $30,000 and
$40,000
Q14:
SELECT *
FROM EMPLOYEE
WHERE (SALARY BETWEEN 30000 AND 40000) AND DNO=5;
Q14A:
SELECT *
FROM EMPLOYEE
WHERE (SALARY >= 30000 AND SALARY <=40000) AND DNO=5;
9
... Join SQL
1.
10
... Join SQL
1.
Example: CROSS-JOIN
12
1. Join SQL
...
13
1. Join SQL
...
14
1. Join SQL
...
15
1. Join SQL
...
16
Outline
1. Join SQL
17
... More Complex SQL Queries
2.
Nested Queries
Some queries require that existing values in the database be fetched and then used in
a comparison condition -> using nested query
Comparison operator IN
➔ Compares value v with a set (or multiset) of values V
➔ Evaluates to TRUE if v is one of the elements in V
18
... More Complex SQL Queries
2.
Outer Query
Nested Query
19
... More Complex SQL Queries
2.
Query:
Retrieve the SSN from all employees who work the same (project,hours)
combination on same project that employee ‘John Smith’ (ESSN = ‘123456789’ )
works on.
20
... More Complex SQL Queries
2.
➔ Other operators that can be combined with ANY (or SOME): >, >=, <, <=, and <>
21
... More Complex SQL Queries
2.
The result of a correlated nested query is different for each tuple (or combination of
tuples) of the relation(s) the outer query
22
... More Complex SQL Queries
2.
23
... More Complex SQL Queries
2.
EXISTS and NOT EXISTS are usually used in conjunction with a correlated
nested query
24
... More Complex SQL Queries
2.
Query 12: Retrieve the name of each employee who has a dependent with the same
first name and same sex as the employee.
25
... More Complex SQL Queries
2.
26
... More Complex SQL Queries
2.
Query 7: List the names of managers who have at least one dependent.
➔ The first nested query select all DEPENDENT tuples related to an EMPLOYEE
➔ The second nested query select all DEPARTMENT tuples managed by the EMPLOYEE
➔ If at least one of the first and at least one of the second exists, we select the EMPLOYEE tuple.
Can you rewrite that query using only on a nested query or no nested query?
27
... More Complex SQL Queries
2.
List the names of managers who have at least one dependent without nested.
28
... More Complex SQL Queries
2.
Query 3: Retrieve the name of each employee who works on all the projects controlled by department number 5
Can be used: (S1 CONTAINS S2) that logically equivalent to (S2 EXCEPT S1) is empty.
29
... More Complex SQL Queries
2.
Exercise
30
?
Q&A