Grouping Data Steve Perry

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the 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
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Chapter 11 Group Functions (up to p.402)
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to Structured Query Language (SQL)
SQL Select Statement Prof. Yitzchak Rosenthal. Syntax for SELECT statement Clauses must be written in the following order –SELECT –FROM –WHERE –GROUP.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
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.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Introduction to Structured Query Language (SQL)
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.
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.
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.
Introduction to SQL J.-S. Chou Assistant Professor.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
You can use a query to view a subset of your data or to answer questions about your data. For example, if you want to view a list of student names and.
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.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
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 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Subqueries Steve Perry 1.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Relational Schema and SQL Queries James Wang.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
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.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
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.
COMP 430 Intro. to Database Systems Grouping & Aggregation Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
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.
SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1.
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
Using Subqueries to Solve Queries
Connect to SQL Server and run select statements
Subqueries Schedule: Timing Topic 25 minutes Lecture
Group Functions Lab 6.
Using Subqueries to Solve Queries
SQL – Entire Select.
Chapter 4 Summary Query.
Using Subqueries to Solve Queries
Section 4 - Sorting/Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Grouping and Aggregating Data
Presentation transcript:

Grouping Data Steve Perry

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

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

Example SELECT pub_id, COUNT(title) FROM book GROUP BY pub_id; 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 You may nest Groups within other groups by separating the columns with commas Example: SELECT pub_id, type, COUNT(type) FROM book GROUP BY pub_id, type; 5

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

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

GROUP BY with WHERE You can use the WHERE clause when grouping of a subset of rows. The WHERE clause acts first to find the rows you want Then the GROUP BY clause divides the rows into groups SELECT type, AVG(price) FROM book WHERE advance > 5000 GROUP BY type; 8

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

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

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

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

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

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

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

HAVING Conditions You may use more than one condition on a HAVING clause SELECT pub_id, SUM(advance), AVG(price) FROM book GROUP BY pub_id HAVING SUM(advance) > AND AVG(price) 3; 16

Last Slide 17