Docsity

NESTED QUERIES AND AGGREGATION, Exams of Database Management Systems (DBMS)

Partial preview of the text

Download NESTED QUERIES AND AGGREGATION and more Exams Database Management Systems (DBMS) in PDF only on Docsity!

NESTED QUERIES

AND AGGREGATION

CHAPTER 5 (6/E)

CHAPTER 8 (5/E)

LECTURE OUTLINE

 More Complex SQL Retrieval Queries

  • Self-Joins
  • Renaming Attributes and Results
  • Grouping, Aggregation, and Group Filtering
  • Ordering Results
  • Nested SPJ Queries

RENAMING IN SQL

 For convenience, include renaming (like ) as well

Aliases or tuple variables

  • Provide alternative names for tables or columns

SELECT name, sale_date, product, quantity AS amount FROM Customer C, Sale AS S(id,sale_date,custid), LineItem WHERE C.custid = S.custid AND id = saleid;

  • Keyword AS is optional

Customer custid name address phone

Sale saleid date custid

LineItem saleid product quantity price

SELF-JOINS

 Renaming is mandatory if table used more than once in a query

 Example Give the last names and salaries of employees and their managers whenever the employee earns more than the manager.

  • Think of the EMPLOYEE table as two tables, one for employees and one for managers.

SELECT E.Lname, E.Salary, M.Lname, M.Salary FROM EMPLOYEE E, EMPLOYEE M WHERE E.Super_ssn = M.Ssn and E.Salary > M.Salary;

EMPLOYEE Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno

M Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno

E Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno

GROUPING BEFORE AGGREGATION

 How can we answer a query such as

“ How many films were directed by each director after 2001? ”

  • Need to produce a result with one tuple per director
    1. Partition relation into subsets of tuples based on grouping column(s)
    2. Apply function to each such group independently
    3. Produce one tuple per group

GROUP BY clause to specify grouping attributes SELECT director, COUNT(*) FROM Film WHERE year > 2001 GROUP BY director;

  • Every selector in SELECT clause must be a grouping column or an aggregation function - e.g., SELECT director, year, COUNT(*) would not be allowed unless also grouping by year i.e., GROUP BY director, year

HAVING CLAUSE

 After partitioning into groups, whole partitions can be discarded.

  • Provides a condition on the grouped tuples

 Having clause cannot reference individual tuples within group

  • Can reference grouping column(s) and aggregates only

 Contrast WHERE clause to HAVING clause

Note : As for aggregation, no GROUP BY clause means relation treated as one group

SUMMARY OF SQL QUERIES

  1. Assemble all tables according to From clause (“,” means to use ).
  2. Keep only tuples matching Where clause.
  3. Group into blocks based on Group By clause.
  4. Keep only blocks matching Having clause.
  5. Create one tuple for each block using Select clause.
  6. Order resulting tuples according to Order By clause.

NESTED QUERIES

 Any table can be used in FROM clause.

 select-from-where produces a table.

 Thus can nest one query within another.

 Example:

Give the biographical information for directors of profitable movies.

SELECT name, birth, city FROM ( SELECT director FROM Film WHERE gross > budget) AS Profitable, Person WHERE director = name

Film title genre year director minutes budget gross

Person name birth city

USING IN FOR MEMBERSHIP TEST

 Comparison operator IN

  • Compares value v with a set (or bag) of values V
  • Evaluates to TRUE if v is one of the elements in V
  • Allows any relation in WHERE clause
  • Can omit DISTINCT from this solution. Why?

USING IN (CONT’D.)

 Use tuples of values in comparisons

  • Requires parentheses

CORRELATED NESTED QUERIES

Correlated nested query

  • Evaluated once for each tuple in the outer query

 Such queries are easiest to understand (and write correctly) if all column names are qualified by their relation names.

Note that the inner query can refer to E, but the outer query cannot refer to D.

EXISTS AND UNIQUE FUNCTIONS

 [NOT] EXISTS function

  • Check whether result of correlated nested query is empty or not
  • EXISTS equivalent to (SELECT COUNT(*) … ) <> 0

SELECT name, phone FROM Customer C WHERE NOT EXISTS ( SELECT * FROM Sale S WHERE C.custid = S.custid);

  • Note that columns selected in inner query are irrelevant.

 SQL function UNIQUE(Q)

  • Returns TRUE if no duplicate tuples in result of query Q

Customer custid name address phone

Sale saleid date custid