Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DB Lec 5

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

ADVANCED SQL

DR.AHMED ELASHRY
COMPLEX SQL RETRIEVAL QUERIES
COMPARISONS INVOLVING NULL AND THREE-VALUED
LOGIC
COMPLEX SQL RETRIEVAL QUERIES
COMPARISONS INVOLVING NULL AND THREE-VALUED
LOGIC

 SQL allows queries that check whether an attribute value is NULL.


 Rather than using = or <> to compare an attribute value to NULL, SQL
uses the comparison operators IS or IS NOT.
 This is because SQL considers each NULL value as being distinct from
every other NULL value
COMPLEX SQL RETRIEVAL QUERIES
COMPARISONS INVOLVING NULL AND THREE-VALUED
LOGIC

 Q1: Retrieve the names of all employees who do not have supervisors.
Q1: SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;
COMPLEX SQL RETRIEVAL QUERIES
NESTED QUERIES, TUPLES, AND SET/MULTISET COMPARISONS

 Nested Queries : are complete select-from-where blocks within another


SQL query.
 That other query is called the outer query.
 These nested queries can appear in the WHERE clause or the FROM
clause or the SELECT clause or other SQL clauses as needed
 The comparison operator IN usually used within nested queries.
COMPLEX SQL RETRIEVAL QUERIES
NESTED QUERIES, TUPLES, AND SET/MULTISET COMPARISONS

 Q 2A: SELECT DISTINCT Pnumber


FROM PROJECT WHERE Pnumber IN ( SELECT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND
Lname = ‘Smith’ )
OR Pnumber IN ( SELECT Pno FROM WORKS_ON,
EMPLOYEE WHERE Essn = Ssn AND Lname
= ‘Smith’ );
COMPLEX SQL RETRIEVAL QUERIES
NESTED QUERIES, TUPLES, AND SET/MULTISET COMPARISONS

 SQL allows the use of tuples of values in comparisons by placing them


within parentheses.
SELECT DISTINCT Essn FROM WORKS_ON
WHERE (Pno, Hours) IN ( SELECT Pno, Hours
FROM WORKS_ON WHERE Essn = ‘123456789’ );
COMPLEX SQL RETRIEVAL QUERIES
NESTED QUERIES, TUPLES, AND SET/MULTISET COMPARISONS

 a number of other comparison operators can be used to compare a single


value v (typically an attribute name) to a set or multiset v
 The = ANY (or = SOME) operator returns TRUE if the value v is equal to
some value in the set V and is hence equivalent to IN.
 Other operators that can be combined with ANY (or SOME) include >,
>=, <, <=, and <>.
COMPLEX SQL RETRIEVAL QUERIES
NESTED QUERIES, TUPLES, AND SET/MULTISET COMPARISONS

 The keyword ALL can also be combined with each of these operators.
SELECT Lname, Fname FROM EMPLOYEE
WHERE Salary > ALL ( SELECT Salary
FROM EMPLOYEE WHERE Dno = 5 );
COMPLEX SQL RETRIEVAL QUERIES
NESTED QUERIES, TUPLES, AND SET/MULTISET COMPARISONS

 Query 3. Retrieve the name of each employee who has a dependent with the same first
name and is the same sex as the employee.
 Q3: SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E
WHERE E.Ssn IN ( SELECT D.Essn
FROM DEPENDENT AS D
WHERE E.Fname = D.Dependent_name
AND E.Sex = D.Sex );
COMPLEX SQL RETRIEVAL QUERIES
CORRELATED NESTED QUERIES

 Whenever a condition in the WHERE clause of a nested query references


some attribute of a relation declared in the outer query, the two queries
are said to be correlated.
COMPLEX SQL RETRIEVAL QUERIES
THE EXISTS AND UNIQUE FUNCTIONS IN SQL

 EXISTS and UNIQUE are Boolean functions that return TRUE or


FALSE; hence, they can be used in a WHERE clause condition.
 The EXISTS function in SQL is used to check whether the result of a
nested query is empty (contains no tuples) or not.
 EXISTS and NOT EXISTS are typically used in conjunction with a
correlated nested query.
COMPLEX SQL RETRIEVAL QUERIES
THE EXISTS AND UNIQUE FUNCTIONS IN SQL

 Q3B: SELECT E.Fname, E.Lname FROM EMPLOYEE AS E


WHERE EXISTS ( SELECT * FROM DEPENDENT AS D
WHERE E.Ssn = D.Essn AND E.Sex = D.Sex
AND E.Fname = D.Dependent_name);
 Q4: SELECT Fname, Lname FROM EMPLOYEE
WHERE NOT EXISTS ( SELECT * FROM DEPENDENT
WHERE Ssn = Essn );
COMPLEX SQL RETRIEVAL QUERIES
THE EXISTS AND UNIQUE FUNCTIONS IN SQL

 Q5: SELECT Fname, Lname FROM EMPLOYEE


WHERE EXISTS ( SELECT * FROM DEPENDENT
WHERE Ssn = Essn )
AND
EXISTS ( SELECT * FROM
DEPARTMENT
WHERE Ssn =
Mgr_ssn );
COMPLEX SQL RETRIEVAL QUERIES
THE EXISTS AND UNIQUE FUNCTIONS IN SQL

 Q6A. This is an example of certain types of queries that require universal


quantification, One way to write this query is to use the construct (S2
EXCEPT S1) as explained next, and checking whether the result is empty.
This option is shown as Q6A.
 Q6A: SELECT Fname, Lname FROM EMPLOYEE WHERE NOT
EXISTS ( ( SELECT Pnumber FROM PROJECT WHERE Dnum = 5)
EXCEPT ( SELECT Pno FROM WORKS_ON WHERE Ssn = Essn) );
COMPLEX SQL RETRIEVAL QUERIES
THE EXISTS AND UNIQUE FUNCTIONS IN SQL

 Q6B: SELECT Lname, Fname FROM EMPLOYEE


WHERE NOT EXISTS ( SELECT * FROM
WORKS_ON B WHERE ( B.Pno IN ( SELECT
Pnumber FROM PROJECT WHERE Dnum
= 5 ) AND NOT EXISTS ( SELECT * FROM
WORKS_ON C WHERE C.Essn = Ssn AND C.Pno = B.Pno )));
COMPLEX SQL RETRIEVAL QUERIES
GROUPING: THE GROUP BY AND HAVING CLAUSES

 In these cases we need to partition the relation into nonoverlapping


subsets (or groups) of tuples.
 Each group (partition) will consist of the tuples that have the same value
of some attribute(s), called the grouping attribute(s).
 . HAVING provides a condition on the summary information regarding
the group of tuples associated with each value of the grouping attributes.
COMPLEX SQL RETRIEVAL QUERIES
GROUPING: THE GROUP BY AND HAVING CLAUSES

 Query 14. For each department, retrieve the department number, the
number of employees in the department, and their average salary.
 Q14: SELECT Dno, COUNT (*), AVG (Salary)
FROM EMPLOYEE GROUP BY Dno;
COMPLEX SQL RETRIEVAL QUERIES
GROUPING: THE GROUP BY AND HAVING CLAUSES

 Query 15. For each project, retrieve the project number, the project name,
and the number of employees who work on that project..
 Q15: SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE Pnumber = Pno GROUP BY Pnumber,
Pname;
COMPLEX SQL RETRIEVAL QUERIES
GROUPING: THE GROUP BY AND HAVING CLAUSES

 Query 16. For each project on which more than two employees work,
retrieve the project number, the project name, and the number of
employees who work on the project...
 Q16: SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT, WORKS_ON WHERE Pnumber =
Pno GROUP BY Pnumber, Pname HAVING
COUNT (*) > 2;
COMPLEX SQL RETRIEVAL QUERIES
GROUPING: THE GROUP BY AND HAVING CLAUSES

 Query 17. For each project, retrieve the project number, the project name,
and the number of employees from department 5 who work on the
project.
 Q16: SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber = Pno AND Ssn = Essn AND Dno
= 5 GROUP BY Pnumber, Pname;
COMPLEX SQL RETRIEVAL QUERIES
GROUPING: THE GROUP BY AND HAVING CLAUSES

 Query 18. For each department that has more than five employees,
retrieve the department number and the number of its employees who are
making more than $40,000.
 Q18: SELECT Dno, COUNT (*) FROM EMPLOYEE
WHERE Salary>40000 AND Dno IN
( SELECT Dno FROM
EMPLOYEE GROUP BY Dno
HAVING COUNT (*) > 5) GROUP BY Dno;
COMPLEX SQL RETRIEVAL QUERIES
OTHER SQL CONSTRUCTS: WITH AND CASE

 The WITH clause allows a user to define a table that will only be used in
a particular query;
 it is somewhat similar to creating a view that will be used only in one
query and then dropped
 Q5.18′: WITH BIGDEPTS (Dno) AS ( SELECT Dno FROM
EMPLOYEE GROUP BY Dno HAVING COUNT (*) > 5)
SELECT Dno, COUNT (*) FROM EMPLOYEE WHERE Salary>40000
AND Dno IN BIGDEPTS GROUP BY Dno;
COMPLEX SQL RETRIEVAL QUERIES
OTHER SQL CONSTRUCTS: WITH AND CASE

 SQL also has a CASE construct, which can be used when a value can be
different based on certain conditions
 UPDATE EMPLOYEE SET Salary =
CASE WHEN Dno = 5 THEN Salary + 2000
WHEN Dno = 4 THEN Salary + 1500
WHEN Dno = 1 THEN Salary + 3000
ELSE Salary + 0;
COMPLEX SQL RETRIEVAL QUERIES
RECURSIVE QUERIES IN SQL

 An example of a recursive relationship between tuples of the same type is


the relationship between an employee and a supervisor
COMPLEX SQL RETRIEVAL QUERIES
RECURSIVE QUERIES IN SQL

 An example of a recursive operation is to retrieve all supervisees of a supervisory


employee e at all levels—that is, all employees e′ directly supervised by e, all employees
e′ directly supervised by each employee e′, all employees e″′ directly supervised by each
employee e″, and so on.
 Q19: WITH RECURSIVE SUP_EMP (SupSsn, EmpSsn) AS
( SELECT SupervisorSsn, Ssn FROM EMPLOYEE
UNION
SELECT E.Ssn, S.SupSsn FROM EMPLOYEE AS E, SUP_EMP AS S
WHERE E.SupervisorSsn = S.EmpSsn)
SELECT* FROM SUP_EMP;
COMPLEX SQL RETRIEVAL QUERIES
SPECIFYING GENERAL CONSTRAINTS AS ASSERTIONS IN SQL

 In SQL, users can specify general constraints via declarative assertions,


using the CREATE ASSERTION statement.
 Each assertion is given a constraint name and is specified via a condition
similar to the WHERE clause of an SQL query.
COMPLEX SQL RETRIEVAL QUERIES
SPECIFYING GENERAL CONSTRAINTS AS ASSERTIONS IN SQL

 For example, to specify the constraint that the salary of an employee must
not be greater than the salary of the manager of the department that the
employee works for in SQL, we can write the following assertion:
 CREATE ASSERTION SALARY_CONSTRAINT
CHECK ( NOT EXISTS ( SELECT * FROM EMPLOYEE E,
EMPLOYEE M, DEPARTMENT D WHERE E.Salary>M.Salary AND
E.Dno = D.Dnumber AND D.Mgr_ssn = M.Ssn ) );
THANK YOU
SOMEONE@EXAMPLE.COM

You might also like