{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

7 Nested SQL cont - NESTED QUERIES AND AGGREGATION C HAPTER...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CHAPTER 5 OUTLINE (CONT’D) More Complex SQL Retrieval Queries Self-Joins Renaming Attributes and Results Grouping, Aggregation, and Group Filtering Ordering Results Nested SPJ Queries 2
Background image of page 2
REVIEW OF SPJ QUERIES IN SQL SPJ (select-project-join) queries SQL’s basic select-from-where queries Equivalent to using only , , and (or ) in Relational Algebra (and possibly , if attributes need to be renamed before joining) 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 4 Customer custid name address phone Sale saleid date custid LineItem saleid product quantity price
Background image of page 4
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; 5 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
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
AGGREGATE FUNCTIONS Used to accumulate information from multiple tuples, forming a single- tuple summary Built-in aggregate functions
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}