1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP.

Slides:



Advertisements
Similar presentations
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Advertisements

4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions (up to p.402)
Information Resources Management February 27, 2001.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Database Management Systems & Programming Faculty of Information & Media Studies Summer 2000 LIS Week 6 Structured Query Language.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Queries and SQL in Access Please use speaker notes for additional information!
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
SQL Unit 5 Aggregation, GROUP BY, and HAVING Kirk Scott 1.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Grouping Data. GROUP BY clause Groups results by column name used with aggregate functions must come first when used with ORDER BY.
6 Chapter 6 Structured Query Language (SQL). 6 2 Sample Database for the following examples Publishing company has three subsidiary publishers. Need to.
Subqueries Steve Perry 1.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Grouping Data Steve Perry
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
COMP 430 Intro. to Database Systems Grouping & Aggregation Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!
Advanced Select Statements Select List Variations SELECT *SELECT * Column NamingColumn Naming Arithmetic ExpressionsArithmetic Expressions ConstantsConstants.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
1 Section 8 - Manipulating Data u The INSERT statement adds rows of data to the database u The UPDATE statement changes columns of existing data u The.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
Joining Tables Steve Perry
1 Section 9 - Views, etc. u Part 1: Views u Part 2:Security Issues u Part 3:Transaction Management u Part 4:Set Operations u Part 5:Triggers and Stored.
Connect to SQL Server and run select statements
Aggregating Data Using Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
SQL – Entire Select.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Reporting Aggregated Data Using the Group Functions
Queries and SQL in Access
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP BY divides a table into sets, then Aggregate functions return summary values for those sets.

2 GROUP BY Syntax u SELECT select_list FROM table_list [WHERE conditions] GROUP BY group_by_list;

3 Example u SELECT pub_id, COUNT(title) FROM titles GROUP BY pub_id; u All items in the Select list that are not in the Group By list must generate a single value for each group

4 Groups within Groups u You may nest Groups within other groups by separating the columns with commas u Example: SELECT pub_id, type, COUNT(type) FROM titles GROUP BY pub_id, type;

5 Restrictions u Again: Each item in the SELECT list must produce a single value u Wrong: SELECT pub_id, type, COUNT(type) FROM titles GROUP BY pub_id;

6 More Restrictions u You can NOT use expressions in the GROUP BY clause Wrong: SELECT pub_id, SUM(price) FROM titles GROUP BY pub_id, SUM(price);

7 No Column Numbers u Unlike the ORDER BY clause, you cannot use the column select list position number in the GROUP BY clause Wrong: SELECT pub_id, SUM(price) FROM titles GROUP BY 1;

8 Multiple Summaries u To see the summary values for a publisher and for the type of books within that publisher you will need two SELECT statements u SELECT pub_id, SUM(price) FROM titles GROUP BY pub_id; u SELECT pub_id, type, SUM(price) FROM titles GROUP BY pub_id, type;

9 Exercise u Display a list of the authors and the state they live in. Sort the list by the author’s last name within state

10 Discussion u SELECT au_lname, au_fname, state FROM authors ORDER BY state, au_lname; We don't need a Group By for this statement because no summary information was asked for

11 Exercise u Display a list of states and the number of authors that are from each state. Also, show how many different cities are in each state. Sort in state order.

12 Discussion u SELECT state, count(*), count(distinct city) FROM authors GROUP BY state ORDER BY state, au_lname; This gets us the number of authors per state and the number of distinct cities in each state. If we didn't use the DISTINCT keyword we would count all authors who lived in a city.

13 NULLs and GROUPS u NULLs never equal another NULL u BUT... GROUP BY will create a separate group for the NULLs u Think of it as a Group of Unknowns

14 Example u The Type column contains NULLs SELECT type, COUNT(*) FROM titles GROUP BY type; u Returns count of 1, if we used a COUNT(type) instead of Count(*) we'd get back a zero instead. Why?

15 Discussion u Count(*) counts whole rows and there is 1 row of a NULL type group u Count(type) counts the non-NULL type columns in the NULL type group and there are zero non-NULL values in the NULL group.

16 More NULLs u More than one NULL in a column? SELECT advance, COUNT(*) FROM titles GROUP BY advance; u Two books have a NULL advance and they are grouped. [Note: zero is different group]

17 GROUP BY with WHERE u The WHERE clause allows grouping of a subset of rows. The WHERE clause acts first to find the rows you want u Then the GROUP BY clause divides the rows into groups SELECT type, AVG(price) FROM titles WHERE advance > 5000 GROUP BY type;

18 No WHERE u Same statement, no WHERE SELECT type, AVG(price) FROM titles GROUP BY type; u NULL group returned [In the previous example, the WHERE clause eliminated the NULLs]

19 ORDER the GROUPS u GROUP BY puts rows into sets, but doesn't put them in order. SELECT type, AVG(price) FROM titles WHERE advance > 5000 GROUP BY type ORDER BY 2;

20 Exercise u Show the average position that an author appears on a book if the author has a royalty share less than 100%. Also, show the number of books written by the author. List the author using his social security number and sort by social security number within number of books order. Show the authors with the most number of books first.

21 Discussion u SELECT au_id, AVG(au_ord), COUNT(title_id) FROM titleauthors WHERE royaltyshare < 1.0 GROUP BY au_id ORDER BY 3 DESC, au_id;

22 HAVING Clause u HAVING is like a WHERE clause for a GROUP u WHERE limits rows u HAVING limits GROUPs

23 HAVING Syntax u SELECT select_list FROM table_list [WHERE conditions] GROUP BY group_list [HAVING conditions];

24 HAVING Aggregates u The WHERE conditions apply before Aggregates are calculated u Then the HAVING conditions apply after Aggregates are calculated

25 HAVING vs. WHERE u WHERE comes after the FROM u HAVING comes after the GROUP BY u WHERE conditions cannot include Aggregates u HAVING conditions almost always include Aggregates

26 Example u SELECT type, count(*) FROM titles GROUP BY type HAVING COUNT(*) > 1; u NOTE: Cannot use WHERE instead of HAVING since WHERE does not allow Aggregates

27 HAVING without Aggregates u Applies to grouping columns SELECT type FROM titles GROUP BY type HAVING type LIKE 'p%'; You could of used the WHERE clause to find types that began with 'p', as well

28 Exercise u List the editor positions that have at least three editors

29 Answer u SELECT ed_pos, count(*) FROM editors GROUP BY ed_pos HAVING count(*) >= 3;

30 HAVING Conditions u You may use more than one condition on a HAVING clause SELECT pub_id, SUM(advance), AVG(price) FROM titles GROUP BY pub_id HAVING SUM(advance) > AND AVG(price) '0800';

31 Exercise u List the publisher id and the average advance for each book that the publisher sells and the total number of books they sell, but only if the total cost of all the books they sell (that are priced more than $10.00) is more than eighty dollars and they sell more than one book. Sort by pub_id and book count.

32 Discussion u SELECT pub_id, AVG(advance), COUNT(*) FROM titles WHERE price > 10 GROUP BY pub_id HAVING SUM(price) > 80 AND Count(*) > 1 ORDER BY 1, 3;

33 Discussion u The WHERE clause first eliminates all books that do not cost more than $10 u Then the GROUP BY forms the pub_id groups u Then the HAVING clause eliminates any groups whose total cost ( sum(price) ) is not greater than $80 and any pub_id that has not sold more than one book.

34 Section 5 - Last Slide u Please complete Assignment 4