DB Lec 5
DB Lec 5
DB Lec 5
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
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
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
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
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