SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.

Slides:



Advertisements
Similar presentations
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
Advertisements

SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Database Systems More SQL Database Design -- More SQL1.
Computer Science 101 Web Access to Databases SQL – Extended Form.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
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.
Chapter 3 Single-Table Queries
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.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Relational Algebra - Chapter (7th ed )
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
SQL Aggregeringsfunktioner. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the average.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
More SQL: Complex Queries,
MySQL DML Commands By Prof. B.A.Khivsara
CS580 Advanced Database Topics
Aggregating Data Using Group Functions
Chapter # 6 The Relational Algebra and Calculus
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
6/22/2018.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Group Functions Lab 6.
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
SQL Subquery.
GROUPING DATA FROM TABLE
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
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
Database Programming Using Oracle 11g
Presentation transcript:

SQL-5 (Group By.. Having)

Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some attribute values. For example, we may want to find the average salary of employees in each department or the number of employees who work on each project.  In these cases, we need to partition the relation into nonoverlapping subsets (or groups) of tuples.  Each group (partition) will consist of the tuples that have the same value of some attribute(s), called the grouping attribute(s).

Group By.. Having Act on record sets rather than on individual records based on grouping attribute(s). Group By Clause:  Another section of select statement.  This optional clause tells oracle to group rows based on distinct values that exist for specified columns.  The Group By clause creates a data set, containing several sets of records grouped together based on a condition.

Syntax Select,, ………, Aggregate_function ( ) From table_name where group by,, ;

Group By.. Having  The GROUP BY clause specifies the grouping attributes, which should also appear in the SELECT clause, so that the value resulting from applying each aggregate function to a group of tuples appears along with the value of the grouping attribute(s).  If NULLs exist in the grouping attribute, then a separate group is created for all tuples with a NULL value in the grouping attribute. Query. For each department, retrieve the department number, the number of employees in the department, and their average salary. Q: SELECT Dno, COUNT (*), AVG (Salary) FROM EMPLOYEE GROUP BY Dno;

Questions Find how many employees are there in each branch? Sol :--- Select branch_no “Branch_no”, count (Emp_no) “No. of employees” from employee group by branch_no; Output: Branch_no No. of employees B14 B25

Questions If the objective is to find out the maximum salary of each department then the query will be SELECT deptno, MAX (sal) FROM emp GROUP BY deptno; DEPTNO MAX(SAL) rows selected.

Questions For each project, retrieve the project number, the project name, and the number of employees who work on that project. Given the schemas as: PROJECT(Pnumber); WORKS_ON(Pno, Pname) SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON WHERE Pnumber=Pno GROUP BY Pnumber, Pname;

Having Having clause can be used in conjunction with group by clause. Imposes condition on Group By clause which further filters the group created by the Group By clause. HAVING clause is very much similar to WHERE clause except that having clause is applicable only for grouped data. Here, the rows are further restricted according to the having clause.

HAVING Each column specification specified in the having clause must occur within a statistical function or must occur in the list of columns named in the group by clause. e.g. If we want to find out the maximum salary for all departments having more than three employees, then the query will be-

Example SELECT deptno, MAX (sal) FROM emp GROUP BY deptno HAVING COUNT(*) >3; DEPTNO MAX(SAL) rows selected.

Example For each project on which more than two employees work, retrieve the project number, the project name, and the number of employees who work on the project. PROJECT(Pnumber, Proj_complet_date) WORKS_ON (Pno, Pname) SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON WHERE Pnumber=Pno GROUP BY Pnumber, Pname HAVING COUNT (*) > 2;

Example For each project, retrieve the project number, the project name, and the number of employees from department 5 who work on the project. SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON, EMPLOYEE WHERE Pnumber=Pno AND Ssn=Essn AND Dno=5 GROUP BY Pnumber, Pname;

Example We can also use the order by clause with group by & HAVING clauses to arrange the records in any specific order. SELECT deptno, MAX (sal) FROM emp GROUP BY deptno HAVING COUNT (*) >2 ORDER BY MAX(sal); DEPTNO MAX(SAL) rows selected.

Example Consider Relation BOOK(BOOK_ID,title, price), Compute total price of books belonging to each combination of BOOK_ID and title comprising the character ‘n’ in the title. select title, BOOK_ID, sum(price) from book group by title, BOOK_ID having title like '%n%';

Example (Cont.)

Example Not Recommended: select sum(price) from book group by title, BOOK_ID having title like '%n%';

Questions Ques: Which of the following STATEMENT is true? (A)select title, BOOK_ID, sum(price) from book group by title, BOOK_ID having sum(price)>555; (B) select title, BOOK_ID, sum(price) from book group by title, BOOK_ID having price > 555; Ans: (A)