{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

Info icon This 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
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
Image of page 3

Info icon This 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
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
Image of page 5

Info icon This 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
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern