Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

Similar documents
Introductory SQL SQL Joins: Viewing Relationships Pg 1

Database Management Systems,

ER Modeling ER Diagram ID-Dependent and Weak Entities Pg 1

Insertions, Deletions, and Updates

Slicing and Dicing Data in CF and SQL: Part 1

CSE 530A SQL. Washington University Fall 2013

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

SQL Data Query Language

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

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

Database 2: Slicing and Dicing Data in CF and SQL

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

Subquery: There are basically three types of subqueries are:

Introduction to Oracle9i: SQL

Maintaining Data 3.3.1

chapter 12 C ONTROLLING THE LEVEL OF

COMP 430 Intro. to Database Systems. Encapsulating SQL code

Further GroupBy & Extend Operations

Databases - 5. Problems with the relational model Functions and sub-queries

Rows and Range, Preceding and Following

Intro to Structured Query Language Part I

Reporting Aggregated Data Using the Group Functions. Copyright 2004, Oracle. All rights reserved.

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

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

Common Data Model Patterns Ray Lockwood 1 Points: Certain patterns recur in data models. We should recognize and name these patterns.

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Intermediate SQL: Aggregated Data, Joins and Set Operators

Data Manipulation Language (DML)

Structure Query Language (SQL)

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

KORA. RDBMS Concepts II

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

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

Logical Operators and aggregation

Use the database company.accdb and finish the following exercises on SQL. Emp

CSC Web Programming. Introduction to SQL

HP NonStop Structured Query Language (SQL)

RESTRICTING AND SORTING DATA

ÇALIŞMA TEST SORULARI

CMPT 354: Database System I. Lecture 4. SQL Advanced

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

Introduction to SQL Server 2005/2008 and Transact SQL

Relational Database Management Systems for Epidemiologists: SQL Part II

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

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

Now, we can refer to a sequence without having to use any SELECT command as follows:

CS Reading Packet: "Sub-selects, concatenating columns, and projecting literals"

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Objectives. After completing this lesson, you should be able to do the following:

Database Programming with SQL

UFCEKG 20 2 : Data, Schemas and Applications

Full file at

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

EECS 647: Introduction to Database Systems

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Databases and MySQL. COMP 342: Programming Methods. 16 September Databases and MySQL

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CIS Slightly-different version of Week 10 Lab, also intro to SQL UPDATE and DELETE, and more

Relational Database Development

M I C R O S O F T A C C E S S : P A R T 2 G E T T I N G I N F O R M A T I O N O U T O F Y O U R D A T A

SQL CHEAT SHEET. created by Tomi Mester

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

Figure 1: Relationship among Three Entities/Tables

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

SQL 2 (The SQL Sequel)

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

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

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

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

STIDistrict Query (Basic)

My Query Builder Function

Querying Data with Transact SQL

Principles of Data Management

Query-by-Example (QBE) QBE: Intro. `Example Tables in QBE. Reserves sid bid day. Boats bid bname color. Sailors sid sname rating age.

In this exercise you will practice some more SQL queries. First let s practice queries on a single table.

Chapter 3: Introduction to SQL

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

EMPLOYEE (Name, Salary, DeptNum) DEPARTMENT(DeptNum, ManagerName)

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

CS1100: Data, Databases, and Queries QUERY CONSTRUCTION. CS1100 Microsoft Access 1

LIKE Condition. % allows you to match any string of any length (including zero length)

DATABASE MANAGEMENT SYSTEMS PREPARED BY: ENGR. MOBEEN NAZAR

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

CIS Reading Packet: "Views, and Simple Reports - Part 1"

COSC 304 Introduction to Database Systems. Views and Security. Dr. Ramon Lawrence University of British Columbia Okanagan

STRUCTURED QUERY LANGUAGE (SQL)

CSE 135. Applications View of a Relational Database Management System (RDBMS) SQL. Persistent data structure. High-level API for access &modification

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2

ER Modeling Data Modeling and the Entity-Relationship (ER) Diagram Pg 1

เพ มภาพตามเน อหาของแต ละบท. MS-Access. by Asst. Prof. Wassanaa Naiyapo adapted into English by Dr. Prakarn Unachak IT AND MODERN LIFE

Consistency The DBMS must ensure the database will always be in a consistent state. Whenever data is modified, the database will change from one

MIS2502: Data Analytics SQL Getting Information Out of a Database Part 1: Basic Queries

Summer 2017 Discussion 13: August 3, Introduction. 2 Creating Tables

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

Transcription:

Advanced SQL Clause and Functions Pg 1 Clause and Functions Ray Lockwood Points: s (such as COUNT( ) work on groups of Instead of returning every row read from a table, we can aggregate rows together using the clause. After the groups are made, we can filter the groups we want to see by using the HAVING clause. Functions Here's our Employee table: Employee EmployeeNum LastName FirstName DeptNum Salary 014 Smith Bob 100 20000 086 Jones NULL 200 35000 127 Doe John 100 60000 859 Thompson Joe 300 45000 273 Watson Ed 200 55000 662 Wilson Tom 200 30000 589 Morrison Fred 100 40000 840 Estes Jerry 300 25000 509 Harris William 200 50000 This table has nine There's a NULL in the FirstName column. COUNT( ) If we run this query: SELECT * WHERE DeptNum = 100; We'll get a set of rows: 014 Smith Bob 100 20000 127 Doe John 100 60000 589 Morrison Fred 100 40000 The output of a SQL statement is a table.

Advanced SQL Clause and Functions Pg 2 When we use an Function, it returns a single number (a scalar) built from a collection (aggregation) of many rows combined. For example, we can use the aggregate COUNT(*) to tell us how many employees are in department 100: SELECT COUNT(*) WHERE DeptNum = 100; Instead of a set of rows, this query returns a single scalar value: 3 The result is an aggregate of the rows returned by the WHERE clause. Two versions of COUNT( ) There are two versions of the COUNT : COUNT(*) gives a count of all the rows returned by the query. COUNT(column name) returns a count of the rows for which the named is not NULL. The left-hand query below returns the number of rows in the table. The right-hand query returns the number of rows in which FirstName is non-null: s work on groups of The COUNT(*) counts rows, not s. A single value is called a Scalar Value. It is really a set with one row and one column. COUNT(*) doesn t care if an is NULL. COUNT(Column) omits NULLs from the count. SELECT COUNT(*) ; The values returned by each: SELECT COUNT(FirstName) ; There's one NULL in the FirstName column. 9 All the rows in 8 the table All the rows in which FirstName is not NULL All the Functions There are five aggregate s: 1. AVG( ) 2. COUNT( ) 3. MAX( ) 4. MIN( ) 5. SUM( ) AVG( ) and SUM( ) work only on numeric data. Let s try all the aggregate s: SELECT COUNT(*),AVG(Salary),MAX(Salary),MIN(Salary),SUM(Salary) WHERE DeptNum = 100 All of these s work on an aggregation of

Advanced SQL Clause and Functions Pg 3 Here's the result: 3 40000 60000 20000 120000 Row count Average Salary Greatest Salary Least Salary Sum of the Salaries All the s produce a scalar (single value) output. You Can t Mix and Non- Results It would make no sense to make a query like this, because the COUNT(*) wants to return a single row, and the LastName wants to return three This returns a scalar value SELECT COUNT(*), LastName WHERE DeptNum = 100; This returns three rows This query causes an error! If you use an aggregate, then all the items in the SELECT clause must either be aggregates or named in a clause, which we look at next. You can t output s that have different numbers of The aggregate will produce a result over the group. Clause We've produced a head count of one department, and by omiting the WHERE clause we'd get the head count of the whole company. How can we get a count of each one of the departments at once? We use the clause: divides results into groups. SELECT DeptNum, COUNT(*) DeptNum; reports on each group. This query gives a count for each group. aggregates the rows specified in the clause. This statement returns the DeptNum of each department, and the corresponding row count for each department: DeptNum COUNT(*) 100 3 200 4 300 2 The aggregate is applied to each group.

Advanced SQL Clause and Functions Pg 4 The steps the clause takes are: 1. Sort the table by the. 2. Combine all rows having the same value in the into groups. 3. Apply the aggregate s to each group. SELECT Clause Is Restricted To Grouped Attributes & Functions The statement builds groups of rows, so the SELECT clause can contain only the things that make sense in the context of groups. The SELECT clause can contain only: The grouping. s pertaining to the group. The only things allowed in the SELECT clause are: s s In the above SQL statement, the SELECT clause contains the grouping DeptNum, and the aggregate COUNT(*) which counts the rows in each group. Here is an invalid SQL statement: SELECT DeptNum, COUNT(*), Salary DeptNum Multiple values per group! BAD! This query causes an error! The SELECT clause contains something other than or an aggregate! The above query will produce an error because there are many salaries for each department number. We can fix this by applying an aggregate to salary: SELECT DeptNum, COUNT(*), AVG(Salary) DeptNum This query gives the count and average salary for each group. Good! Only the and aggregate s are in the SELECT clause. This statement returns the DeptNum of each department, and the corresponding row count and average salary: DeptNum COUNT(*) AVG(Salary) 100 3 40000 200 4 42500 300 2 35000 The aggregate is applied to each group.

Advanced SQL Clause and Functions Pg 5 Multiple Attributes You can put multiple s in the clause to make fine grained groups: SELECT ZipCode, PlusFour, COUNT(*) FROM Address ZipCode, PlusFour Each group consists of addresses from a PlusFour zone within a ZipCode Multiple GROUP BY s are OK. HAVING clause The HAVING clause filters the groups produced by the clause. It's applied after the groups are built. Let s add it to an earlier query to restrict the output to groups with more than two rows: SELECT DeptNum, COUNT(*) DeptNum HAVING COUNT(*) > 2 The HAVING clause takes the output of the and includes only the groups that test TRUE for the Boolean expression. The output contains only those groups (departments) that have more than two employees: DeptNum COUNT 100 3 200 4 or The values in the HAVING clause must pertain to groups, not individual HAVING filters groups after WHERE filters HAVING filters groups according to a Boolean. The Order of Clauses In a SQL Statement When building a SELECT statement, the clauses have to appear in a specific order: 1. SELECT 2. FROM 3. WHERE 4. 5. HAVING 6. ORDER BY Applies to rows Applies to groups When you build a SELECT statement, it must follow this structure.

Advanced SQL Clause and Functions Pg 6 Here s an example using all of the clauses: Row filter Group filter SELECT DeptNum, COUNT(*), AVG(Salary), MAX(Salary) WHERE Salary >= 35000 DeptNum HAVING COUNT(*) > 2 ORDER BY AVG(Salary) or This is the order that the statement is executed. Values in the ORDER BY clause pertain to groups, not individual The order of execution is the same as the order that the statement is written. Remember that HAVING and ORDER BY work on groups because they re placed after the clause. Order of Execution SELECT statements process the rows first, then the groups. The order of execution is: 1. WHERE Filters the rows from the input table. 2. s the remaining rows into groups. 3. HAVING Filters the groups. 4. ORDER BY Sorts the remaining groups.