database management systems. nesting of queries some queries require that existing values in the...

17
Database Management Systems

Upload: elijah-freeman

Post on 03-Jan-2016

212 views

Category:

Documents


0 download

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

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

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

820202533
In Q8, the alternate relation names E and S are called aliases or tuple variables for the EMPLOYEE relationWe can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors
User
The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query).
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

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' )

820202533
In Q8, the alternate relation names E and S are called aliases or tuple variables for the EMPLOYEE relationWe can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors
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

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

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')

820202533
In Q8, the alternate relation names E and S are called aliases or tuple variables for the EMPLOYEE relationWe can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors
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

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

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

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.

User
Definition: Tuple is a term from set theory which refers to a collection of one or more attributes.
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

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

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)

820202533
In Q8, the alternate relation names E and S are called aliases or tuple variables for the EMPLOYEE relationWe can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors
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

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

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

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)

820202533
In Q8, the alternate relation names E and S are called aliases or tuple variables for the EMPLOYEE relationWe can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors
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

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.

User
by considering that the nested query is evalyated once for each tuple in the outer query . for example , for each employee tuple , evaluate the nested query , which retrives the ESSN values for all dependent tuples with the same sex and name as that employee tuple
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

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

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

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))

820202533
In Q8, the alternate relation names E and S are called aliases or tuple variables for the EMPLOYEE relationWe can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors