SQL: Aggregate Functions with Grouping

Similar documents
The Relational Data Model

Name: Sample Answers Q.2(c) 7 7

CSC Web Programming. Introduction to SQL

Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

12. MS Access Tables, Relationships, and Queries

Data Manipulation Language (DML)

SQL Data Query Language

This lecture. Databases - SQL II. Counting students. Summary Functions

Databases - SQL II. (GF Royle, N Spadaccini ) Structured Query Language II 1 / 22

SQL Aggregation and Grouping. Outline

SQL Aggregation and Grouping. Davood Rafiei

Lesson 2. Data Manipulation Language

Part I: Structured Data

SQL Structured Query Language Introduction

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

Querying Data with Transact-SQL

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

Relational Database Management Systems for Epidemiologists: SQL Part I

Database Management Systems,

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan


Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Querying Data with Transact SQL

Data Infrastructure IRAP Training 6/27/2016

MySQL Workshop. Scott D. Anderson

Querying Data with Transact-SQL

Announcements. Multi-column Keys. Multi-column Keys (3) Multi-column Keys. Multi-column Keys (2) Introduction to Data Management CSE 414

SQL - Lecture 3 (Aggregation, etc.)

MTA Database Administrator Fundamentals Course

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Announcements. Multi-column Keys. Multi-column Keys. Multi-column Keys (3) Multi-column Keys (2) Introduction to Data Management CSE 414

NEO4J CQL - UNION. Like SQL, Neo4j CQL has two clauses to combine two different results into one set of results

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

T-SQL Training: T-SQL for SQL Server for Developers

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Lecture 6 - More SQL

Introduction to Data Management CSE 414

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

20461: Querying Microsoft SQL Server 2014 Databases

OVERVIEW OF RELATIONAL DATABASES: KEYS

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName

COMP 244 DATABASE CONCEPTS & APPLICATIONS

Database Management Systems,

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

COSC 122 Computer Fluency. Databases. Dr. Ramon Lawrence University of British Columbia Okanagan

Querying Data with Transact-SQL (20761)

20461: Querying Microsoft SQL Server

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Querying Microsoft SQL Server (MOC 20461C)

Structure Query Language (SQL)

SQL. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

Database Technology. Topic 3: SQL. Olaf Hartig.

Unit 1 - Chapter 4,5

After completing this course, participants will be able to:

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Querying Microsoft SQL Server 2014

Provider: MySQLAB Web page:

Chapter 4 SQL. Database Systems p. 121/567

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

Access Intermediate

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

II. Structured Query Language (SQL)

Operating systems fundamentals - B07

20761 Querying Data with Transact SQL

Unit Assessment Guide

SQL functions fit into two broad categories: Data definition language Data manipulation language

Lecture 06. Fall 2018 Borough of Manhattan Community College

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

COUNT Function. The COUNT function returns the number of rows in a query.

RESTRICTING AND SORTING DATA

Logical Operators and aggregation

SQL. Char (30) can store ram, ramji007 or 80- b

Simple Quesries in SQL & Table Creation and Data Manipulation

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

DEVELOPING DATABASE APPLICATIONS (INTERMEDIATE MICROSOFT ACCESS, X405.5)

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

XQ: An XML Query Language Language Reference Manual

Web Security. Attacks on Servers 11/6/2017 1

Fundamentals of Database Systems

Writing Queries Using Microsoft SQL Server 2008 Transact- SQL

CMPT 354: Database System I. Lecture 3. SQL Basics

Querying Data with Transact-SQL

PGQL 0.9 Specification

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

A taxonomy of SQL queries Learning Plan

INF 212 ANALYSIS OF PROG. LANGS SQL AND SPREADSHEETS. Instructors: Crista Lopes Copyright Instructors.

Transcription:

INFS0 - Database Management Systems SQL: Aggregate Functions with Grouping Peter Y. Wu Department of Computer and Information Systems Robert Morris University Simple SQL Query Syntax Select-From-Where select [ distinct all ] <goal-list> (removal of duplicates) from <source-table> (simple query has one table) where <conditions> (combination using operators) order by <column> [ asc desc ],... (order of presentation) (c) Peter Y Wu - RMU.

INFS0 - Database Management Systems SQL Query using aggregate functions Select-functions-From-Where select <goal-list-using-aggregate-functions > from <source-table> (simple query has one table) where <conditions> (filter to eliminate rows) Result has only ONE row. No need to use distinct keyword. No need to use order by. 3 SQL Query with GROUP BY Select-From-Where--Having select [ distinct all ] <goal-list-from-grouped-table> from <source-table> (simple query has one table) where <conditions> (filter: eliminate rows for consideration) group by <pivot-list> (to divide table into groups) having <conditions> (conditions based on the grouped table) order by <term-from-grouped-table> [ asc desc ],... (order of presentation) (c) Peter Y Wu - RMU.

INFS0 - Database Management Systems Aggregate Functions Earliest birth and latest death of composers who lived for longer than 50 years select min(born), max(died) from Composer where Born-Died > 50; Aggregate functions do not consider null values. Aggregate functions will consider duplicates, unless otherwise specified, by keyword distinct. select count(distinct ) from Composer; Keyword distinct applies only to a specific term, but cannot apply to the * argument in count( ). 5 Aggregate Functions select count(cname) from Composer; count(cname) ------------------ 5 Composer CName Born Died Vivaldi 678 7 Bach 685 750 3 Mozart 756 79 Prokofiev 89 953 5 Dvorak 8 90 Can we find the number of composers born in each century? 6 (c) Peter Y Wu - RMU. 3

INFS0 - Database Management Systems Aggregate Functions Composer PNo PName CName Born Died 000 The Four Seasons Vivaldi 678 7 00 B-minor Mass Bach 685 750 003 Christmas Oratorio 3 Mozart 756 79 00 Missa Solemnis 3 Prokofiev 89 953 006 Classical Symphony 5 Dvorak 8 90 007 Cinderella select count(pname) # of pieces from where = ; Can we find the number of pieces written by each composer? 7 Aggregate functions with group by We can apply aggregate function to sub-groups of values in any data set (i.e., a column). select, from group by ; 000 The Four Seasons 00 B-minor Mass 003 Christmas Oratorio 00 Missa Solemnis 3 006 Classical Symphony 007 Cinderella 3 8 (c) Peter Y Wu - RMU.

INFS0 - Database Management Systems Aggregate functions with group by We form the sub-groups by the unique values of the group by columns that is, the pivot. We apply the aggregate function to each sub-group... select, from group by ; 000 The Four Seasons 00 B-minor Mass 003 Christmas Oratorio 00 Missa Solemnis 3 006 Classical Symphony 007 Cinderella 3 9 Aggregate functions with group by The result table therefore consists of ONE row for each sub-group (unique values of the pivot). How many rows should the result table contain? select, from group by ; 000 The Four Seasons 00 B-minor Mass 003 Christmas Oratorio 00 Missa Solemnis 3 006 Classical Symphony 007 Cinderella 3 0 (c) Peter Y Wu - RMU. 5

INFS0 - Database Management Systems Aggregate function without GROUP BY always produces exactly one row of result. Aggregate function with GROUP BY becomes very powerful it produces a table! We must first understand how grouping works with the GROUP BY clause. The pivot is the collection of columns (terms) used in GROUP BY A table is a collection of records (rows). With GROUP BY, we attempt to sub-divide the collection of records into sub-groups. Table T GROUP BY C means that we will sub-divide table T into sub-groups by the unique values under column C that is, the unique values under the pivot. For each unique value, we have one sub-group (c) Peter Y Wu - RMU. 6

INFS0 - Database Management Systems Table T GROUP BY C Table T GROUP BY C T C C C3 000 John 33 00 Robert 7 00 Edward 003 John 33 00 Robert 00 Edward C 000 003 00 00 00 00 T C John John Robert Robert Edward Edward C3 33 33 7 There are groups! There are 3 groups! 3 Table T GROUP BY C3 Table T GROUP BY C,C3 T C C C3 000 John 33 003 John 33 00 Robert 7 00 Robert 00 Edward 00 Edward C 000 003 00 00 00 00 T C John John Robert Robert Edward Edward C3 33 33 7 There are groups! The same groups! (c) Peter Y Wu - RMU. 7

INFS0 - Database Management Systems Table Composer GROUP BY floor(born/00+) Composer CName Born Died Vivaldi 678 7 Bach 685 750 3 Mozart 756 79 Prokofiev 89 953 5 Dvorak 8 90 (extended) floor(born/00)+ 7 7 8 9 9 There are 3 groups one sub-group for each century! 5 The pivot is the collection of columns (terms) used in GROUP BY. Each sub-group has at least one record (row). Each sub-group has the same value under the pivot that is, all the records (rows) is each sub-group share the same value of the pivot. Under other columns, the values may be different. 6 (c) Peter Y Wu - RMU. 8

INFS0 - Database Management Systems Table GROUP BY Each sub-group has the same value under the pivot that is, all the records (rows) is each sub-group share the same value of the pivot. Under other columns, the values may be different. 000 The Four Seasons 00 B-minor Mass 003 Christmas Oratorio 00 Missa Solemnis 3 006 Classical Symphony 007 Cinderella 7 Aggregate function with Table GROUP BY Now when we apply aggregate function, we apply it once to each sub-group, generating exactly one row for each sub-group. Select count(pno) from GROUP BY ; 000 The Four Seasons 00 B-minor Mass 003 Christmas Oratorio 00 Missa Solemnis 3 006 Classical Symphony 007 Cinderella count(pno) 8 (c) Peter Y Wu - RMU. 9

INFS0 - Database Management Systems Aggregate function with When we apply aggregate function to each sub-group, we may also select columns (or terms) from the pivot to be listed... Select, from GROUP BY ; 000 The Four Seasons 00 B-minor Mass 003 Christmas Oratorio 00 Missa Solemnis 3 006 Classical Symphony 3 007 Cinderella 9 Aggregate function with But selecting from non-pivot columns will NOT be allowed, because that won t make sense! select PNo, from group by ; 000 The Four Seasons 00 B-minor Mass 003 Christmas Oratorio 00 Missa Solemnis 3 006 Classical Symphony 007 Cinderella PNo???? X 0 (c) Peter Y Wu - RMU. 0

INFS0 - Database Management Systems Aggregate function with Even when rows of the non-pivot column share the same value in each sub-group, the column cannot be selected because it is not in the pivot! select CName, from group by ; PNo PName CName 000 The Four Seasons Vivaldi 00 B-minor Mass Bach 003 Christmas Oratorio Bach 00 Missa Solemnis Mozart 3 006 Classical Symphony Prokofiev 007 Cinderella Prokofiev CName Vivaldi Bach Mozart Prokofiev X Aggregate function with The goal list to be selected can only be: () aggregate functions, or () terms involving only columns in the pivot. Select floor(born/00)+ as Century, count() as # of composers, avg(died-born) as Average Life from Composer group by floor(born/00)+; Composer CName Born Died Vivaldi 678 7 Bach 685 750 3 Mozart 756 79 Prokofiev 89 953 5 Dvorak 8 90 Century # of composers 7 8 9 Average Life 6 35 6.5 (c) Peter Y Wu - RMU.

INFS0 - Database Management Systems Aggregate functions with group by select <goal-list-with-aggr-func-and-pivot-cols> from <source-table> where <where-condition> group by <pivot-list>; First, the table in the source-list is identified. Second, the where-condition is evaluated for each row. Rows not meeting the where-condition are eliminated. Third, the columns (terms) for the pivot list are identified. Fourth, the table is grouped according to the unique values of the pivot columns, into sub-groups. Fifth, the columns referred to in the aggregate functions in the goal list are evaluated, one evaluation for each sub-group, producing one row of values for each sub-group. Sixth, the results are listed, with exactly ONE row for each unique value of the pivot columns. 3 Group by with having select <goal-list-with-aggr-func-and-pivot-cols> from <source-table> where <where-condition> group by <pivot-list> having <group-condition>; First, the table in the source-list is identified. Second, the where-condition is evaluated for each row. Rows not meeting the where-condition are eliminated. Third, the columns (terms) for the pivot list are identified. Fourth, the table is grouped according to the unique values of the pivot columns, into sub-groups. Fifth, the columns referred to in the aggregate functions in the goal list are evaluated, one evaluation for each sub-group, producing one row of values for each sub-group. Sixth, the results are listed, with exactly ONE row for each unique value of the pivot columns. Seventh, only those rows satisfying the group-condition (specified by having ) are listed in the result as output. (c) Peter Y Wu - RMU.

INFS0 - Database Management Systems Group by with having select, from group by having >= ; 000 The Four Seasons 00 B-minor Mass 003 Christmas Oratorio 00 Missa Solemnis 3 006 Classical Symphony 007 Cinderella 3 In the same way as the goal-list, the condition specified for having may only involve aggregate functions, or terms included in the pivot. 5 (c) Peter Y Wu - RMU. 3