database management systems. nesting of queries some queries require that existing values in the...
TRANSCRIPT
![Page 1: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/1.jpg)
Database Management Systems
![Page 2: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/2.jpg)
NESTING OF QUERIES Some queries require that existing values in
the database be retrieved and then used in a comparison condition.
A complete SELECT query, called a nested query , can be specified within the WHERE-clause of another query, called the outer query
Many of the previous queries can be specified in an alternative form using nesting
![Page 3: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/3.jpg)
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 DNUMBER=DNO;
SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE WHERE DNO IN (
SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research' )
![Page 4: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/4.jpg)
NESTING OF QUERIES The nested query selects the number of the
'Research' department The outer query select an EMPLOYEE tuple if its DNO
value is in the result of either nested query The comparison operator IN compares a value v with
a set (or multi-set) of values V, and evaluates to TRUE if v is one of the elements in V
In general, we can have several levels of nested queries
A reference to an unqualified attribute refers to the relation declared in the innermost nested query
In this example, the nested query is not correlated with the outer query
![Page 5: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/5.jpg)
Query 2 Make a list of all project numbers for 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 PNUMBER FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND
MGR_SSN=SSN AND LNAME='Smith')UNION
(SELECT PNUMBERFROM PROJECT, WORKS_ON, EMPLOYEEWHERE PNUMBER=PNO AND ESSN=SSN AND
LNAME='Smith')
![Page 6: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/6.jpg)
Query 2 using nested queriesSELECT distinct pnumberFROM projectWHERE 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')
![Page 7: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/7.jpg)
Nested Queries The first nested query selects the project
numbers of projects that have a ‘Smith’ involved as manager
The second nested query selects the project numbers of projects that have a ‘Smith’ involved as a worker
In the outer query, we use the OR logical connective to retrieve a project tuple if the pnumber value of the tuple is in the result of either nested query
![Page 8: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/8.jpg)
Nested Queries SQL allows the use of tuples of values in comparisons by
placing them within parentheses. To illustrate this, consider the following query:
SELECT DISTINCT ESSN FROM WORKS_ONWHERE (PNO, HOURS) IN (SELECT PNO, HOURS
FROM WORKS_ON WHERE ESSN='123456789')
This query will select the social security numbers of all employees who work in the same (project, hours) combination on same project that employee 'John Smith' (whose ssn = ‘123456789') works on.
![Page 9: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/9.jpg)
Nested Queries In addition to the IN operator, a number of other
comparison operators can be used to compare single value v (typically an attribute name) to a set or multiset V (typically a nested query). 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. The keywords ANY and SOME have the same meaning. Other operators that can be combined with ANY (or SOME) include >, >=,<,< = , and <>. The keyword ALL can also be combined with each of these operators
OperatorMeaning
INEqual to any member in the list
ANYCompare value to each value returned by the subquery
ALLCompare value to every value returned by the subquery
![Page 10: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/10.jpg)
Query 3 Retrieve the names of employees
whose salary is greater than the salary of all the employees in department 5
SELECT LNAME, FNAME FROM EMPLOYEEWHERE SALARY > ALL (SELECT SALARY
FROM EMPLOYEE WHERE
DNO=5)
![Page 11: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/11.jpg)
Nested Queries In general, we can have several levels of
nested queries. We can once again be faced with possible ambiguity among attribute names if attributes of the same name exist—one in a relation in the FROM clause of the outer query, and another in a relation in the FROM clause of the nested query. The rule is that a reference to an unqualified attribute refers to the relation declared in the innermost nested query.
![Page 12: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/12.jpg)
CORRELATED NESTED QUERIES
If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query , the two queries are said to be correlated
The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) the outer query
![Page 13: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/13.jpg)
Query 4 Retrieve the name of each employee who
has a dependent with the same first name and same sex as the employee.
SELECT E.FNAME, E.LNAME FROM EMPLOYEE E WHERE E.SSN IN (SELECT ESSN
FROM DEPENDENT WHERE E.FNAME=DEPENDENT_NAME AND E.SEX=SEX)
![Page 14: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/14.jpg)
CORRELATED NESTED QUERIES
Correlated subquery runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query.
Nested subquery runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.
![Page 15: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/15.jpg)
THE EXISTS FUNCTION EXISTS is used to check whether the result of a
correlated nested query is empty (contains no tuples) or not. Also we can use NOT EXISTS
Query 5 : Retrieve the name of each employee who has a dependent with the same first name as the employee.
SELECT FNAME, LNAMEFROM EMPLOYEEWHERE EXISTS (SELECT * FROM DEPENDENT
WHERE SSN=ESSN AND
FNAME=DEPENDENT_NAME)
![Page 16: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/16.jpg)
Query 5
List the names of employees who have no dependents
SELECT fname, lnameFROM employeeWHERE NOT EXISTS ( SELECT *
FROM dependent WHERE ssn=ESSN)
![Page 17: Database Management Systems. NESTING OF QUERIES Some queries require that existing values in the database be retrieved and then used in a comparison](https://reader036.vdocuments.us/reader036/viewer/2022072017/56649f055503460f94c19d38/html5/thumbnails/17.jpg)
Query 6 Retrieve the name of each employee who works on all
projects controlled by department number 5
SELECT lname , fnameFROM employeewhere 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))