SQL AGGREGATE FUNCTIONS

Slides:



Advertisements
Similar presentations
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Advertisements

Aggregate Functions Presenter: Sushma Bandekar CS 157(A) – Fall 2006.
4c. Structured Query Language - Built-in Functions Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
AGGREGATE FUNCTIONS Prof. Sin-Min Lee Surya Bhagvat CS 157A – Fall 2005.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group 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.
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Computer Science 101 Web Access to Databases SQL – Extended Form.
Structured Query Language
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
Introduction to SQL J.-S. Chou Assistant Professor.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
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
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
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.
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
Querying a Database - A question or an inquiry (dictionary.com) - WHAT ARE WE ASKING QUESTIONS ABOUT? THE DATA - BY ASKING QUESTIONS OF THE DATA WE OBTAIN?
Information Resource Engineering SQL4. Recap - Ordering Output  Usually, the order of rows returned in a query result is undefined.  The ORDER BY clause.
1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
Structured Query Language
© Jalal Kawash Database Queries Peeking into Computer Science.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
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.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
CS3220 Web and Internet Programming More SQL
Chapter 3 Introduction to SQL(3)
The Database Exercises Fall, 2009.
Group Functions Lab 6.
Chapter 5: Aggregate Functions and Grouping of Data
Aggregating Data Using Group Functions
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Built in Functions Massaging the data.
Chapter 4 Summary Query.
Structured Query Language
SQL Aggregation.
CS122 Using Relational Databases and SQL
Query Functions.
Section 4 - Sorting/Functions
Joins and other advanced Queries
Aggregate Functions.
Aggregate functions Objective- understand and able to use aggregate functions in select statement Aggregate functions are used to implement calculation.
LINQ to SQL Part 3.
Group Operations Part IV.
Presentation transcript:

SQL AGGREGATE FUNCTIONS Advanced SQL Week 41 SQL Aggregate functions COUNT MIN MAX SUM AVG

SQL AGGREGATE FUNCTIONS SQL provides serval built-in numeric functions COUNT: The number of rows containing non-null values MIN: The minimum attribute value encountered in a given column MAX: The maximum attribute value encountered in a given column SUM: The sum of all values for a givenColumn AVG: The arithmetic mean (average) for a specified column

SQL AGGREGATE FUNCTIONS Examples If want to know the number of rows in the employee table we can use: SELECT COUNT(*) FROM employee;

SQL AGGREGATE FUNCTIONS Examples SELECT MIN(hours) AS minimumHours, MAX(hours) AS maximumHours, AVG(hours) AS averageHours FROM project WHERE projectID > 7;

Exercise Write an SQL query that Lists the number of products? Write an SQL query that calculates the total price of all products bought before 2013.11.20? Write an SQL query that calculates the average price? write an SQL query that lists all departments with the number of employee in each department?. Write an SQL query that lists the row with highest price?. Write SQL query that lists the row with lowest price?

Answers 1. SELECT COUNT(*) FROM product; 2. SELECT SUM( p_price ) AS total_price_before_2013 FROM product; 3. SELECT AVG( p_price ) FROM product SQL AGGREGATE FUNCTIONS

Answers 4. SELECT did, COUNT( eid ) AS numberOfEmploys FROM works_in GROUP BY did; 5. SELECT p_code, p_descript, MAX( p_price ) AS maximum_price FROM product; 6. SELECT p_code, p_descript, MIN( p_price ) AS minimum_price