1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

Slides:



Advertisements
Similar presentations
COMPANY schema EMPLOYEE
Advertisements

SQL (cont’d) CSE3330 Spring 2014 Chengkai Li. NULL values.
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Company Database. CREATE TABLE DEPARMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER),
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
CREATE VIEW SYNTAX CREATE VIEW name [(view_col [, view_col …])] AS [WITH CHECK OPTION];
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL-99: Schema Definition, Basic Constraints, and Queries by Pinar Senkul resources: mostly.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1.
Database Systems Chapter 8 ITM 354. Data Definition, Constraints, and Schema Changes Used in SQL to Create, Drop, and Alter the descriptions of the tables.
More SQL: Complex Queries, Triggers, Views, and Schema Modification 1.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
SQL: Schema Definition, Constraints, & Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 11 July 12,2012 More SQL: Complex Queries, Triggers,
Relational Algebra - Chapter (7th ed )
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Structured Query Language
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
Faculty of Information Science and Technology Mahanakorn University of Technology Topic 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Chapter 11 SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not.
1 Chapter 5 The SQL Language Data Definition Language The Data Definition Language (DDL) is used to create and destroy databases and database.
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –continue with SQL (chapter.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
COMP163 Database Management Systems September 18, 2008 Lecture 8 – Sections SQL Queries.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
1 CSE 480: Database Systems Lecture 16: Relational Algebra.
SQL Aggregeringsfunktioner. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the average.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
1 Database Systems SQL: Advanced Queries. 2 Union, Intersection, and Except (2)   Find the names of those people who are either a graduate student or.
Lecture # 25 Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views Muhammad Emran Database Systems.
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
CS580 Advanced Database Topics
6/22/2018.
Retrieval Queries in SQL(DML)
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
376a. Database Design Dept. of Computer Science Vassar College
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

2 NESTED QUERIES A nested query is specified within the WHERE-clause of the outer query

3 NESTED QUERIES l Query: Retrieve the name and address of employees who work for the 'Research' department SELECT Fname, Lname, Address FROM Employee, Department WHERE Dname='Research' AND Dnumber=Dno; (Non-nested query approach)

4 NESTED QUERIES Query: Retrieve the name and address of employees who work for the 'Research' department SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE WHEREDNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research' ); (Nested query approach)

5 Example l Query: List the names of projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project

6 NESTED QUERIES l Query: List the names of 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 PNAME FROMPROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith') UNION (SELECT PNAME FROMPROJECT, WORKS_ON, EMPLOYEE WHEREPNUMBER=PNO AND ESSN=SSN AND NAME='Smith'); Non-nested query approach

7 NESTED QUERIES l Query: List the names of projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project SELECTPNAME FROMPROJECT WHEREPNUMBER IN (SELECT PNUMBER FROM PROJECT,DEPARTMENT,EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith') OR PNUMBER IN (SELECT PNO FROMWORKS_ON, EMPLOYEE WHEREESSN=SSN AND NAME='Smith') Nested query approach

8 ANY, ALL, SOME v = ANY W (e.g., Id =ANY (1,2,3)) –evaluates to TRUE if v equals to one of the elements in W v = SOME W (e.g., Id =SOME (1,2,3)) –evaluates to TRUE if v equals to some elements in W v > ALL W (e.g., Age >ALL (23,19,34)) –evaluates to TRUE if v is greater than all the elements in W v < ALL W (e.g., Age <ALL (23,19,34)) –evaluates to TRUE if v is less than all the elements in W

9 NESTED QUERIES l Query: List the names of employees whose salary is greater than the salary of all managers SELECTFNAME, LNAME FROMEMPLOYEE WHERESALARY > ALL (SELECT SALARY FROM DEPARTMENT,EMPLOYEE WHERE MGRSSN=SSN);

10 THE EXISTS FUNCTION l EXISTS is used to check whether the result of a nested query is empty (contains no tuples) SELECT FROM WHERE EXISTS V –WHERE-clause evaluates to TRUE if V is not an empty table and FALSE otherwise

1 THE EXISTS FUNCTION This query will return the names of students with GPA > 3.0

12 THE EXISTS FUNCTION l Query: Retrieve the name of each employee who has a dependent with the same name as the employee’s first name. SELEC SELECT FNAME, LNAME FROM EMPLOYEE WHERE EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN AND FNAME=DEPENDENT_NAME); Correlated nested query

13 THE EXISTS FUNCTION l Query: Retrieve the names of employees who have no dependents SELECT FNAME, LNAME FROMEMPLOYEE WHERENOT EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN); Correlated nested query because it involves attribute of outer query

14 Division Query Student (Id, Name) Course (CrsCode, Dept) Transcript (StudId, CrsCode, Grade) Query: Find the names of students who have taken every course offered by the CS department Student Course CS CS101 CS234 Transcript CS101 CS Adam Bill Cathy Didi Eva AdamResult

15 Division Query in SQL Student (Id, Name) Course (CrsCode, CrsName, Dept) Transcript (StudId, CrsCode, Semester, Grade) Query: Find the names of students who have taken every course offered by the CS department SELECT Name FROM Student WHERE ? Student has taken ALL the CS courses

16 Division Query in SQL Student (Id, Name) Course (CrsCode, CrsName, Dept) Transcript (StudId, CrsCode, Semester, Grade) Query: Find the names of students who have taken every course offered by the CS department This will return names of students who have taken at least 1 CS course SELECT Name FROM Student WHERE EXISTS (SELECT * FROM Transcript, Course WHERE Transcript.StudId = Student.Id AND Transcript.CrsCode = Course.CrsCode AND Course.Dept = ‘CS’)

17 Division Query in SQL Student (Id, Name) Course (CrsCode, CrsName, Dept) Transcript (StudId, CrsCode, Semester, Grade) Query: Find the names of students who have taken every course offered by the CS department SELECT Name FROM Student WHERE ? Student has taken ALL the CS courses

18 Division Query in SQL Student (Id, Name) Course (CrsCode, CrsName, Dept) Transcript (StudId, CrsCode, Semester, Grade) Query: Find the names of students who have taken every course offered by the CS department SELECT Name FROM Student WHERE ( ? ) Equivalent to saying “there are no CS courses the student has not taken”

19 Division Query in SQL Student (Id, Name) Course (CrsCode, CrsName, Dept) Transcript (StudId, CrsCode, Semester, Grade) Query: Find the names of students who have taken every course offered by the CS department SELECT Name FROM Student WHERE NOT EXISTS ( ? ) A CS course the student has not taken

20 Division Query in SQL Student (Id, Name) Course (CrsCode, CrsName, Dept) Transcript (StudId, CrsCode, Semester, Grade) Query: Find the names of students who have taken every course offered by the CS department SELECT Name FROM Student WHERE NOT EXISTS ( SELECT * FROM Course WHERE Dept = ‘CS’ AND ? ) Student has not taken the CS course (Student, course) combination does not exist in Transcript

21 Division Query in SQL Student (Id, Name) Course (CrsCode, CrsName, Dept) Transcript (StudId, CrsCode, Semester, Grade) Query: Find the names of students who have taken every course offered by the CS department SELECT Name FROM Student WHERE NOT EXISTS ( SELECT * FROM Course WHERE Dept = ‘CS’ AND NOT EXISTS ( SELECT * FROM Transcript WHERE Transcript.StudId = Student.Id AND Transcript.Crscode = Course.Crscode ) )

2 Another Possible Solution Student (Id, Name) Course (CrsCode, CrsName, Dept) Transcript (StudId, CrsCode, Semester, Grade) Query: Find the names of students who have taken every course offered by the CS department SELECT Name FROM Student WHERE NOT EXISTS ( SELECT * FROM Course WHERE Dept = ‘CS’ AND CrsCode NOT IN ( SELECT CrsCode FROM Transcript WHERE Transcript.StudId = Student.Id) )

23 Division Query l Retrieve the names of employees who work on every project controlled by the “Research” department

24 Example: Division Query l Retrieve the names of employees who work on every project controlled by the “Research” department SELECT DISTINCT E.Lname, E.Fname FROMEMPLOYEE E WHERE NOT EXISTS ( A project controlled by the Research department in which the employee had NOT worked on )

25 Example: Division Query l Retrieve the names of employees who work on every project controlled by the “Research” department SELECT DISTINCT E.Lname, E.Fname FROMEMPLOYEE E WHERE NOT EXISTS ( SELECT P.Pnumber FROM DEPARTMENT D, PROJECT P WHERE D.Dnumber = P.Dnum AND D.Dname=‘Research’ AND NOT EXISTS ( (employee,project) combination in the Works_On table )

26 Example: Division Query l Retrieve the names of employees who work on every project controlled by the “Research” department SELECT DISTINCT E.Lname, E.Fname FROMEMPLOYEE E WHERE NOT EXISTS (SELECT P.Pnumber FROM DEPARTMENT D, PROJECT P WHERE D.Dnumber = P.Dnum AND D.Dname=‘Research’ AND NOT EXISTS ( SELECT * FROM WORKS_ON W WHERE W.ESSN=E.SSN AND P.Pnumber=W.Pno))

27 Exercise l Find the names of projects worked on by every employee in the Research department –Exclude the projects worked on only by a few but not all employees in the Research department l Find the names of employees who work for the Research department but do not work on all the projects controlled by the Research department

28 Aggregate Functions l Used to compute summary statistics for a group of tuples in a table  Include COUNT, SUM, MAX, MIN, and AVG

29 Aggregate Functions l Need to be careful when dealing with NULL values

30 Aggregate Functions l Query: Find the sum of the salaries, maximum salary, minimum salary, and average salary of employees in the ‘Research’ department

31 Aggregate Functions l Query: Count the number of employees who work for the 'Research' department

32 Aggregate Functions Query: Count the number of employees in department number 3 who have worked on at least one project overestimate

3 Aggregate Functions Query: Count the number of employees in Department number 3 who have worked on at least one project

34 Aggregate Functions Cannot use aggregate function directly in the WHERE clause Query: Find the id of employees who have two or more dependents

35 Aggregate Functions Query: Find the name of employees who have two or more dependents This is ok

36 Grouping Clause l Suppose we want to apply the aggregate functions to subgroups of tuples in a relation –Each subgroup consists of the set of tuples that have the same value for the grouping attribute(s) l SQL has a GROUP BY-clause for specifying the grouping attributes

37 Grouping Clause Example l Query: For each department, retrieve the department number, the number of employees in the department, and their average salary. SELECT DNO, COUNT (*), AVG (SALARY) FROMEMPLOYEE GROUP BYDNO; –The EMPLOYEE tuples are divided into groups, each group having the same value for the grouping attribute DNO  The COUNT and AVG functions are applied to each such group of tuples separately  The SELECT-clause includes only the grouping attribute and the aggregate functions to be applied on each group of tuples

38 Grouping Clause Example Effect of null value

39 Grouping Clause Example l Query: For each project, retrieve the project number, project name, and the number of employees who work on that project. In this case, the grouping and aggregate functions are applied after joining the two relations

40 Grouping Clause Example Query: For each project, retrieve the project number, project name, and number of employees from department 3 who work on the project

41 HAVING Clause l The HAVING-clause is used to retrieve only groups whose values of the aggregate functions satisfy certain condition

42 HAVING Clause Example l Query: For each project that has more than one employees work, retrieve the project number, project name, and the number of employees who work on that project.

43 HAVING Clause Example l Query: For each project that has more than one employees work on it, retrieve the project number, project name, and the number of employees who work on that project.

4 HAVING Clause Example  Query: For each department that has more than 1 employee, retrieve the department name and the number of employees who are making more than $50,000  Answer should be: (Payroll, 1) and (Research, 1)

45 HAVING Clause WRONG! This query looks for departments that have more than 1 employee making more than $50,000  Query: For each department that has more than 1 employee, retrieve the department name and the number of employees who are making more than $50,000

46 HAVING Clause  Query: For each department that has more than 1 employee, retrieve the department name and the number of employees who are making more than $50,000

47 ORDER BY Clause l Sort tuples based on the values of some attribute(s) l Query: Retrieve a list of employees and the projects they work on, ordered by their department name, and within each department, ordered alphabetically by last name and first name. SELECT Dname, Lname, Fname, Pname FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHEREDnumber=Dno AND SSN=ESSN AND Pno=Pnumber ORDER BY Dname, Lname, Fname; l The default order is in ascending order of values –We can also specify the keyword DESC or ASC

48 ORDER BY Clause l Query: Retrieve a list of employees and the projects they work on, ordered by their department name, and within each department, ordered alphabetically by salary (in descending order) SELECT Dname, Lname, Fname, Pname FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHEREDnumber=Dno AND SSN=ESSN AND Pno=Pnumber ORDER BY Dname ASC, Salary DESC;

49 Summary of SQL Queries SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ] l SELECT-clause lists the attributes/functions to be retrieved l FROM-clause specifies all relations (or aliases) needed in the query but not those needed in nested queries l WHERE-clause specifies the conditions for selection and join of tuples from the relations specified in the FROM-clause l GROUP BY specifies grouping attributes l HAVING specifies a condition for selection of groups l ORDER BY specifies an order for displaying the result of a query l A query is evaluated by first applying the WHERE-clause, then GROUP BY and HAVING, and finally the SELECT-clause

50 Limit (in MySQL) l Find the name and salary of the highest paid employee