SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.

Slides:



Advertisements
Similar presentations
The Relational Model Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Advertisements

Query Formulation with SQL
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
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
Chapter 11 Group Functions
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 9 Advanced Query Formulation with SQL.
Introduction to Oracle9i: SQL1 SQL Group Functions.
-Software School of Hunan University-
Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested.
Computer Science 101 Web Access to Databases SQL – Extended Form.
Ch 6: ER to Relational Mapping
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Chapter 10 Application Development with Views. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Background Creating.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 3 Query Formulation with SQL. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Background Getting started.
Advanced Query Formulation with SQL. 9-2 Outline  Outer join problems  Type I nested queries  Type II nested queries and difference problems  Nested.
Chapter 9 Advanced Query Formulation with SQL. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Outer join problems.
9 Advanced Query Formulation with SQL (Chapter 9).
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 4 Query Formulation with SQL.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
SQL – Simple Queries and JOIN MGMT 360 Database Management.
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. What is a View? Derived table Behaves like a base table (virtual) Stored.
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.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 10 Application Development with Views.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Ch 7: Normalization-Part 1
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
SQL Aggregeringsfunktioner. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the average.
Chapter 3 Query Formulation with SQL. Outline Background Getting started Joining tables Summarizing tables Reasoning tools Advanced problems Data manipulation.
Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
CS580 Advanced Database Topics
Application Development with Views
Aggregating Data Using Group Functions
6/22/2018.
Aggregating Data Using Group Functions
Query Formulation with SQL
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
SQL – Entire Select.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Access: SQL Participation Project
Reporting Aggregated Data Using the Group Functions
Query Functions.
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Database Programming Using Oracle 11g
Aggregating Data Using Group Functions
Presentation transcript:

SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa

SELECT Statement Overview SELECT FROM WHERE GROUP BY HAVING ORDER BY Expression: combination of columns, constants, operators, and functions

Example Relations Relations: E mp (eno, ename, bdate, title, salary, supereno, dno) Proj (pno, pname, budget, dno) Dept (dno, dname, mgreno) WorksOn (eno, pno, resp, hours) Foreign keys:  Emp: Emp.supereno to Emp.eno, Emp.dno to Dept.dno  Proj: Proj.dno to Dept.dno  Dept: Dept.mgreno to Emp.eno  WorksOn: WorksOn.eno to Emp.eno, WorksOn.pno to Proj.pno

Example Relation Instances

But First Join Revisited SELECT * FROM emp, dept WHERE emp.dno=dept.dno; Natural Join Style: How many columns in the output table? SELECT * FROM emp NATURAL JOIN dept; Cross Product Style: How many columns in the output table? Alternative SQL92 styles: How many columns in each output table? SELECT * FROM emp INNER JOIN dept USING (dno); SELECT * FROM emp INNER JOIN dept ON emp.dno = dept.dno;

More Join Practice Relational database schema: Return a list of all department names, the names of the projects of that department, and the name of the manager of each department. Return the names of all projects and the names of the employees who have worked on each project. Return the names of all employees who are supervisors. emp (eno, ename, bdate, title, salary, supereno, dno) proj (pno, pname, budget, dno) dept (dno, dname, mgreno) workson (eno, pno, resp, hours)

Ordering Result Data The query result returned is not ordered on any attribute by default. We can order the data using the ORDER BY clause: SELECT ename, salary, bdate FROM emp WHERE salary > ORDER BY salary DESC, ename ASC;  'ASC' sorts the data in ascending order, and 'DESC' sorts it in descending order. The default is 'ASC'.  The order of sorted attributes is significant. The first attribute specified is sorted on first, then the second attribute is used to break any ties, etc.  NULL is normally treated as less than all non-null values.

Aggregate Queries and Functions Several queries cannot be answered using the simple form of the SELECT statement. These queries require a summary calculation to be performed. Examples:  What is the maximum employee salary?  What is the total number of hours worked on a project?  How many employees are there in department 'D1'? To answer these queries requires the use of aggregate functions. These functions operate on a single column of a table and return a single value.

Aggregate Functions The five basic aggregate functions are:  COUNT - returns the # of values in a column  SUM - returns the sum of the values in a column  AVG - returns the average of the values in a column  MIN - returns the smallest value in a column  MAX - returns the largest value in a column Notes:  COUNT, MAX, and MIN apply to all types of fields, whereas SUM and AVG apply to only numeric fields.  Except for COUNT(*) all functions ignore nulls. COUNT(*) returns the number of rows in the table.  Use DISTINCT to eliminate duplicates.

Aggregate Function Example Return the number of employees and their average salary. SELECT COUNT(eno) AS numEmp, AVG(salary) AS avgSalary FROM emp;

Aggregate functions are often most useful when combined with the GROUP BY clause. The GROUP BY clause groups the tuples based on the values of the attributes specified. When used in combination with aggregation functions, the result is a table where each tuple consists of unique values for the group by attributes and the result of the aggregate functions applied to the tuples of that group. GROUP BY Clause

GROUP BY Example For each employee title, return the number of employees with that title, and the minimum, maximum, and average salary. SELECT title, COUNT(eno) AS numEmp, MIN(salary) as minSal, MAX(salary) as maxSal, AVG(salary) AS avgSal FROM emp GROUP BY title; Result:

GROUP BY Clause Rules There are a few rules for using the GROUP BY clause: 1) A column name cannot appear in the SELECT part of the query unless it is part of an aggregate function or in the list of group by attributes.  Note that the reverse is true: a column can be in the GROUP BY without being in the SELECT part. 2) Any WHERE conditions are applied before the GROUP BY and aggregate functions are calculated.

HAVING Clause The HAVING clause is applied AFTER the GROUP BY clause and aggregate functions are calculated. It is used to filter out entire groups that do not match certain criteria.

HAVING Example Return the title and number of employees of that title where the number of employees of the title is at least 2. SELECT title, COUNT(eno) AS numEmp FROM emp GROUP BY title HAVING COUNT(eno) >= 2; Result:

GROUP BY/HAVING Example For employees born after December 1, 1965, return the average salary by department where the average is > 40,000. SELECT dname, AVG(salary) AS avgSal FROM emp NATURAL JOIN dept WHERE emp.bdate > DATE ’ ' GROUP BY dname HAVING AVG(salary) > 40000; Step #1: Perform Join and Filter in WHERE clause

GROUP BY/HAVING Example (2) Step #2: GROUP BY on dname Step #3: Calculate aggregate functions Step #4: Filter groups using HAVING clause

GROUP BY Examples Return the average budget per project: SELECT AVG(budget) FROM proj; Return the average # of hours worked on each project: SELECT pno, AVG(hours) FROM workson GROUP BY pno; Return the departments that have projects with at least 2 'EE's working on them: SELECT proj.dno, COUNT(*) FROM proj, workson, emp WHERE emp.title = 'EE' and workson.eno=emp.eno and workson.pno = proj.pno GROUP BY proj.dno HAVING COUNT(*) >=2;

Multi-Attribute Example Return the employee number, department number and hours the employee worked per department where the hours is >= 10. SELECT W.eno, D.dno, SUM(hours) FROM workson AS W, dept AS D, proj AS P WHERE W.pno = P.pno and P.dno = D.dno GROUP BY W.eno, D.dno HAVING SUM(hours) >= 10; Question: 1) How would you only return records for departments D2 and D3? Result:

GROUP BY Practice Questions Relational database schema: Emp (eno, ename, bdate, title, salary, supereno, dno) Proj (pno, pname, budget, dno) Dept (dno, dname, mgreno) WorksOn (eno, pno, resp, hours) Return the highest salary of any employee. For each project, return its name and the total number of hours employees have worked on it. For each employee, return the total number of hours they have worked. Calculate the average # of hours spent per project in each department.

Conceptual Evaluation Process

Conceptual Evaluation Lessons Row operations before group operations  FROM and WHERE before GROUP BY and HAVING  Check row operations first Grouping occurs only one time

Conceptual Evaluation Problem Relational database schema: Student(StdSSN, StdFirstName, StdLastName, StdCity, StdState, StdMajor, StdClass, StdGPA, StdZip) Faculty(FacSSN, FacFirstName, FacLastName, FacCity, FacState, FacDept, FacRank, FacSalary) Faculty_1(FacSSN, FacSupervisor, FacHireDate, FacZipCode) Offering(offerNo, CourseNo, OffTerm, OffTerm, OffYear, OffLocation, OffTime, FacSSN,OffDays) Course(CourseNo, CrsDesc, CrsUnits) Enrollment(OfferNo, StdSSN, EnrGrade) Example 15 from your text: List the number of offerings taught in 2006 by faculty rank and department. Exclude combinations of faculty rank and department with less than two offerings taught.

Conceptual Evaluation Problem (cont) List the number of offerings taught in 2006 by faculty rank and department. Exclude combinations of faculty rank and department with less than two offerings taught. SELECT FacRank, FacDept, COUNT(*) AS NumOfferings FROM Faculty, Offering WHERE Offering.FacSSN = Faculty.FacSSN AND OffYear = 2006 GROUP BY FacRank, FacDept HAVING COUNT(*) > 1

Query Formulation Process Problem Statement Database Representation Database Language Statement

Critical Questions What tables?  Columns in output  Conditions to test (including join conditions) How to combine the tables?  Usually join PK to FK  More complex ways to combine Individual rows or groups of rows?  Aggregate functions in output  Conditions with aggregate functions

Efficiency Considerations Little concern for efficiency Intelligent SQL compilers Correct and non redundant solution  No extra tables  No unnecessary grouping  Use HAVING for group conditions only Chapter 8 provides additional tips for avoiding inefficient SELECT statements