1c. Advanced SQL (Selected)
1c. Advanced SQL (Selected)
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 1 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 3
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 4 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 5
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 6 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 7
1
Nested Queries, Tuples,
and Set/Multiset Comparisons Nested Queries (cont’d.)
Nested queries
Complete select-from-where blocks within WHERE
clause of another query
Outer query and nested subqueries
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
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 8 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 9
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 10 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 11
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 11 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 13
2
The EXISTS and UNIQUE Functions
in SQL for correlating queries USE of EXISTS
EXISTS function Q7:
Check whether the result of a correlated nested
SELECT Fname, Lname
query is empty or not. They are Boolean functions FROM Employee
that return a TRUE or FALSE result. WHERE EXISTS (SELECT *
FROM DEPENDENT
EXISTS and NOT EXISTS WHERE Ssn= Essn)
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 14 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 15
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 18 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 19
3
Different Types of JOINed Tables in
SQL NATURAL JOIN
Specify different types of join Rename attributes of one relation so it can be joined with
another using NATURAL JOIN:
NATURAL JOIN
Various types of OUTER JOIN (LEFT, RIGHT, Q1B: SELECT Fname, Lname, Address
FULL ) FROM (EMPLOYEE NATURAL JOIN
NATURAL JOIN on two relations R and S (DEPARTMENT AS DEPT (Dname, Dno, Mssn,
Msdate)))
No join condition specified WHERE Dname=‘Research’;
Is equivalent to an implicit EQUIJOIN condition for
each pair of attributes with same name from R and The above works with EMPLOYEE.Dno = DEPT.Dno as an
S implicit join condition
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 20 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 20
If no matching tuple
ALTERNATE SYNTAX:
Padded with NULL values for attributes of right table SELECT E.Lname , S.Lname
RIGHT OUTER JOIN FROM EMPLOYEE E, EMPLOYEE S
WHERE E.Super_ssn + = S.Ssn
Every tuple in right table must appear in result
If no matching tuple
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 22 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 22
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 23 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 24
4
Renaming Results of Aggregation Aggregate Functions in SQL (cont’d.)
Following query returns a single row of computed values NULL values are discarded when aggregate
from EMPLOYEE table: functions are applied to a particular column
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 25 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 26
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 27 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 28
5
Combining the WHERE and the Combining the WHERE and the
HAVING Clause HAVING Clause (continued)
Consider the query: we want to count the total number of Correct Specification of the Query:
employees whose salaries exceed $40,000 in each
Note: the WHERE clause applies tuple by tuple
department, but only for departments where more than
five employees work. whereas HAVING applies to entire group of
tuples
INCORRECT QUERY:
SELECT Dno, COUNT (*)
FROM EMPLOYEE
WHERE Salary>40000
GROUP BY Dno
HAVING COUNT (*) > 5;
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 31 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 32
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 33 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 34
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 35 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 36
6
Recursive Queries in SQL An EXAMPLE of RECURSIVE Query
Q29: WITH RECURSIVE SUP_EMP (SupSsn, EmpSsn) AS
An example of a recursive relationship between
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 40 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 41
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 41 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 43
7
USE OF TRIGGERS Views (Virtual Tables) in SQL
AN EXAMPLE with standard Syntax.(Note : other Concept of a view in SQL
SQL implementations like PostgreSQL use a Single table derived from other tables called the
different syntax.) defining tables
Considered to be a virtual table that is not
R5: necessarily populated
CREATE TRIGGER SALARY_VIOLATION
BEFORE INSERT OR UPDATE OF Salary, Supervisor_ssn ON
EMPLOYEE
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 44 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 45
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 46 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 47
8
View Materialization (contd.) View Update
Multiple ways to handle materialization: Update on a view defined on a single table without any
aggregate functions
immediate update strategy updates a view as
Can be mapped to an update on underlying base
soon as the base tables are changed
table- possible if the primary key is preserved in the
lazy update strategy updates the view when view
needed by a view query
Update not permitted on aggregate views. E.g.,
periodic update strategy updates the view UV2: UPDATE DEPT_INFO
periodically (in the latter strategy, a view query SET Total_sal=100000
may get a result that is not up-to-date). This is WHERE Dname=‘Research’;
commonly used in Banks, Retail store operations, cannot be processed because Total_sal is a computed value
etc. in the view definition
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 49 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 51
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 51 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 52
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 54 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 55
9
The ALTER table command Adding and Dropping Constraints
Alter table actions include: Change constraints specified on a table
Adding or dropping a column (attribute) Add or drop a named constraint
Changing a column definition
Adding or dropping table constraints
Example:
ALTER TABLE COMPANY.EMPLOYEE ADD
COLUMN Job VARCHAR(12);
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 56 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 57
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 57 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 7- 59
10