0% found this document useful (0 votes)
129 views23 pages

Nested Queries, Aggregated Functions

Nested queries allow complete SELECT-FROM-WHERE blocks to be nested within the WHERE clause of an outer query and return relation tables that can then be compared to values in the outer query using operators like IN, ANY, ALL, and EXISTS. Correlated nested queries reference attributes from the outer query's relations, causing the nested query to be evaluated once for each tuple or combination of tuples in the outer query. Aggregate functions like COUNT, SUM, MAX, MIN, and AVG operate on groups of tuples and are used with the GROUP BY clause to first subgroup tuples before summarizing data across subgroups.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
129 views23 pages

Nested Queries, Aggregated Functions

Nested queries allow complete SELECT-FROM-WHERE blocks to be nested within the WHERE clause of an outer query and return relation tables that can then be compared to values in the outer query using operators like IN, ANY, ALL, and EXISTS. Correlated nested queries reference attributes from the outer query's relations, causing the nested query to be evaluated once for each tuple or combination of tuples in the outer query. Aggregate functions like COUNT, SUM, MAX, MIN, and AVG operate on groups of tuples and are used with the GROUP BY clause to first subgroup tuples before summarizing data across subgroups.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 23

Nested Queries

Nested Queries, Tuples,


and Set/ Multiset Comparisons

• Nested Queries: Complete select-from-where blocks within WHERE


clause of another query

• Nested Queries generally return a table (relation)

• 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


Example - Nested Queries

Make a list of all project numbers for projects that involve an employee whose last name is
‘Smith’, either as a worker or as a manager of the department that controls the project.

Query:
SELECT DISTINCT Pnumber
Selects the project numbers of
FROM PROJECT projects that have an employee
with last name ‘Smith’ involved
WHERE Pnumber IN
as manager
( SELECT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND
Mgr_ssn=Ssn AND Lname=‘Smith’ )
OR
Pnumber IN Selects the project numbers of
projects that have an employee
( SELECT Pno with last name ‘Smith’ involved
FROM WORKS_ON, EMPLOYEE as worker
WHERE Essn=Ssn AND Lname=‘Smith’ );
Example - Nested Queries (Cont.)

Select the Essns of all employees who work on the same project and hours as
some project that employee ‘John Smith’ (whose Ssn = ‘123456789’) works on.

Query: Parentheses important to


compare combination of Pno
and Hours attributes
SELECT DISTINCT Essn

FROM WORKS_ON

WHERE (Pno, Hours) IN ( SELECT Pno, Hours

FROM WORKS_ON

WHERE Essn=‘123456789’ );
Comparison Operators to Compare a single
value – ANY & ALL

• = ANY (or = SOME) operator: Returns TRUE if the value v is equal to


some value in the set V (equivalent to IN)

• ALL operator: (v > ALL V) returns TRUE if the value v is greater than
all the values in the set (or multiset) V.

• Other operators that can be combined with ANY (or SOME) and
ALL: >, >=, <, <=, and <>
Example - ALL Operator
Return the names of employees whose salary is greater than the salary of all
the employees in department 5

Query:
SELECT Lname, Fname

FROM EMPLOYEE

WHERE Salary > ALL ( SELECT Salary

FROM EMPLOYEE

WHERE Dno=5 );
Ambiguity in Nested Queries
• 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.  

• Thumb Rule: reference to an unqualified attribute refers to


the relation declared in the innermost nested query.
Example - Ambiguity in Nested Queries

• We must qualify E.Sex because it refers to the


Sex attribute of EMPLOYEE from the outer
query, and DEPENDENT also has an attribute
Retrieve the name of each employee who has a dependent with the
Sex.  
same first name and is the same sex as the employee. • Here, the unqualified references to Sex in the
nested query, refer to the Sex attribute of
Query: DEPENDENT

SELECT E.Fname, E.Lname

FROM EMPLOYEE AS E

WHERE E.Ssn IN ( SELECT Essn FROM DEPENDENT AS D WHERE E.Fname = Dependent_name AND E.Sex = Sex);

No need to qualify Fname and


Ssn of EMPLOYEE if they
appeared in the nested query
because the DEPENDENT
relation does not have Fname
and Ssn attributes, so there is
no ambiguity.
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.

• In a correlated query, the nested query is evaluated once


for each tuple (or combination of tuples) in the outer query
Example - Correlated Nested Queries

Retrieve the name of each employee who has a dependent with the same first
name and is the same sex as the employee.

• For each EMPLOYEE tuple, evaluate the


nested query, which retrieves the Essn
values for all DEPENDENT tuples with the
Query: same sex and name as that EMPLOYEE tuple.
• If the Ssn value of the EMPLOYEE tuple is in
SELECT E.Fname, E.Lname the result of the nested query, then select
that EMPLOYEE tuple
FROM EMPLOYEE AS E

WHERE E.Ssn IN ( SELECT Essn

FROM DEPENDENT AS D WHERE E.Fname = D.Dependent_name 

AND E.Sex = D.Sex);


Nested Queries (Cont.)
Retrieve the name of each employee who has a dependent with the same first name and is the
same sex as the employee.

SELECT E.Fname, E.Lname


FROM EMPLOYEE AS E
WHERE E.Ssn IN ( SELECT Essn
FROM DEPENDENT AS D WHERE E.Fname = D.Dependent_name 
AND E.Sex = D.Sex);
=========================
SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E, DEPENDENT AS D
WHERE E.Ssn=D.Essn AND E.Sex=D.Sex
AND E.Fname=D.Dependent_name;
Nesting with Join Operation

For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s last
name, address, and birth date.

Query:

SELECT Pnumber, Dnum, Lname, Address, Bdate

FROM ((PROJECT JOIN DEPARTMENT ON Dnum=Dnumber)

JOIN EMPLOYEE ON Mgr_ssn=Ssn)

WHERE Plocation=‘Stafford’;
EXISTS Function

• EXISTS function: Check whether the result of a

correlated nested query is empty or not


• EXISTS and NOT EXISTS are typically used in conjunction with a
correlated nested query

• EXISTS(Q): returns TRUE if there is at least one tuple in the result of


the nested query Q, and it returns FALSE otherwise.  

• NOT EXISTS(Q): returns TRUE if there are no tuples in the result of


nested query Q, and it returns FALSE otherwise.  
Example – EXISTS
• For each EMPLOYEE tuple, evaluate
the nested query, which retrieves
Retrieve the name of each employee who all DEPENDENT tuples with the
has a dependent with the same first same Essn, Sex, and
Dependent_name as the
name and is the same sex as the employee. EMPLOYEE tuple.
• If at least one tuple EXISTS in the
SELECT E.Fname, E.Lname result of the nested query, then
select that EMPLOYEE tuple. 
FROM EMPLOYEE AS E
WHERE E.Ssn IN ( SELECT Essn
FROM DEPENDENT AS D WHERE E.Fname = D.Dependent_name 
AND E.Sex = D.Sex);
======================
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);
Example – NOT EXISTS
Retrieve the names of employees who have no dependents.

Query:

SELECT Fname, Lname

FROM EMPLOYEE

WHERE NOT EXISTS ( SELECT * FROM DEPENDENT WHERE Ssn=Essn );

• For each EMPLOYEE tuple, the correlated nested query selects all DEPENDENT tuples whose
Essn value matches the EMPLOYEE Ssn.
• If the result is empty, no dependents are related to the employee, so we select that EMPLOYEE
tuple and retrieve its Fname and Lname.
Aggregate Functions
and Group by clause
Aggregate Functions

• Used to summarize information

from multiple tuples into a single-tuple summary


• Grouping

• Create subgroups of tuples before summarizing

• Built-in aggregate functions: COUNT, SUM, MAX, MIN,


and AVG (NULL values discarded when aggregate functions are applied to
a particular column)

• Functions can be used in the SELECT clause or in a HAVING clause


Example - Aggregate
Functions
• Find the sum of the salaries of all employees, the maximum
salary, the minimum salary and the average salary.

Aggregate Functions
Query

SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG


(Salary)

FROM EMPLOYEE;
Example - Aggregate Functions (Cont.)

• Find the sum of the salaries of all employees of the ‘Research’


department, as well as the maximum salary, the minimum salary, and
the average salary in this department.

Aggregate Functions

Query

SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)

FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)

WHERE Dname=‘Research’;
Example - Aggregate Functions (Cont.)

• Retrieve the number of employees in the ‘Research’


department
Aggregate Functions

Refers to the rows

Query
SELECT COUNT ( * )

FROM EMPLOYEE, DEPARTMENT

WHERE DNO=DNUMBER AND DNAME=‘Research’;


Example - Aggregate Functions (Cont.)

• Count the number of distinct salary values in the


database.
Aggregate Functions
(will not count tuples with NULL)

Query

SELECT COUNT (DISTINCT Salary)


Eliminates duplicate
values
FROM EMPLOYEE;
Example - Aggregate Functions (Cont.)

• Retrieve the names of all employees who have two or


more dependents
Aggregate Functions

Query
SELECT Lname, Fname Counts the number of
dependents that each
FROM EMPLOYEE employee has; if this is
greater than or equal to
WHERE ( SELECT COUNT (*) two, the employee tuple is
selected
FROM DEPENDENT

WHERE Ssn=Essn ) >= 2;


To be continued

You might also like