SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
GROUP BY,HAVING and ORDER
BY clause
DEEPAM AGGARWAL
1
org payroll
X 1500
Y 3500
Z 3000
GROUP BY Clause
SELECT Statement – Grouping Rows
Find the payrollof each organization.
SELECT org, sum(salary) payroll
FROM salary_statistics GROUP BY org
Result:
salary_statistics
emp org salary
A X 500
B X 1000
C Y 1000
D Y 2500
E Z 3000
2
org payroll
X 1500
Z 3000
HAVING Clause
Find the organizations with payroll less than $3200.
SELECT org, sum(salary) payroll
FROM salary_statistics
GROUP BY org HAVING payroll < 3200
Result:
salary_statistics
emp org salary
A X 500
B X 1000
C Y 1000
D Y 2500
E Z 3000
3
The Difference Between WHERE and HAVING Clauses
 WHERE gets processed before any GROUP BY, and so it doesn't have access
to aggregated values (that is, the results of min(), max(), etc. functions).
 HAVING gets processed after GROUP BY and so can be used to constrain the
result set to only those with aggregated values that match a certain
predicate.
Find the organizations with payroll less than $3200.
SELECT org, sum(salary) payroll
FROM salary_statistics }✗Incorrect
WHERE payroll < 3200
GROUP BY org
SELECT org, sum(salary) payroll
FROM salary_statistics }✓ Correct
GROUP BY org
HAVING payroll < 3200
4
SELECT Statement – ORDER BY Clause
Find students ordered by merit position.
SELECT *
FROM result
ORDER BY marks DESC
result
student marks
A 25
B 56
C 82
D 25
E 39
5

More Related Content

PPT
Constraints In Sql
PDF
Python Programming Language | Python Classes | Python Tutorial | Python Train...
DOC
Plsql task answers
PDF
View & index in SQL
PDF
SQL JOINS
PPTX
Introduction to SQL
PPT
PL/SQL Introduction and Concepts
PPTX
Constraints In Sql
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Plsql task answers
View & index in SQL
SQL JOINS
Introduction to SQL
PL/SQL Introduction and Concepts

What's hot (20)

PPTX
Database constraints
PDF
Database Normalization
PPTX
SQL JOIN
PPTX
Sql queries presentation
PPTX
Constructor overloading & method overloading
PPTX
Lab2 ddl commands
PDF
Normalization in DBMS
PPT
Aggregate functions
PPT
Normalization PRESENTATION
PPTX
Integrity Constraints
PPTX
Sql subquery
PPT
Joins in SQL
PPTX
joins in database
PPT
Databases: Normalisation
PPT
SQL Views
PPTX
sql function(ppt)
PPTX
Group By, Order By, and Aliases in SQL
PDF
Oracle SQL Basics
PPTX
DATABASE CONSTRAINTS
Database constraints
Database Normalization
SQL JOIN
Sql queries presentation
Constructor overloading & method overloading
Lab2 ddl commands
Normalization in DBMS
Aggregate functions
Normalization PRESENTATION
Integrity Constraints
Sql subquery
Joins in SQL
joins in database
Databases: Normalisation
SQL Views
sql function(ppt)
Group By, Order By, and Aliases in SQL
Oracle SQL Basics
DATABASE CONSTRAINTS
Ad

Similar to Group By, Having Clause and Order By clause (20)

PPTX
groupbyhaving.pptxxxxxxxJIPIJPJPIXxxxXXX
PDF
Group by clause mod
PPTX
12. Basic SQL Queries (2).pptx
PPT
PDF
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
PPTX
08_SQL OLAP DBMS DB2 GROUP BY CUBE ROLL UP .pptx
PPT
PPTX
SQL.pptx
PPT
PDF
Dynamic websites lec2
PPTX
Lecture 8 DML3 aggregate functions in DB.pptx
PPT
Aggregate Functions,Final
PPT
Les06 (using subqueries to solve queries)
PPT
PHP mysql Aggregate functions
PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
PPT
PPT
PPT
James Colby Maddox Business Intellignece and Computer Science Portfolio
PPT
groupbyhaving.pptxxxxxxxJIPIJPJPIXxxxXXX
Group by clause mod
12. Basic SQL Queries (2).pptx
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
08_SQL OLAP DBMS DB2 GROUP BY CUBE ROLL UP .pptx
SQL.pptx
Dynamic websites lec2
Lecture 8 DML3 aggregate functions in DB.pptx
Aggregate Functions,Final
Les06 (using subqueries to solve queries)
PHP mysql Aggregate functions
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
James Colby Maddox Business Intellignece and Computer Science Portfolio
Ad

More from Deepam Aggarwal (13)

PPTX
Frames and its components
PDF
Bank managment system
PPTX
Monolithic and Procedural Programming
PPTX
Inventory Management
PPTX
Dynamic memory allocation and linked lists
PPTX
Cyber Criminal and Cyber Security
PPTX
Mdi vb.net
PPTX
Stress Management
PPT
Impressionism
PPTX
Powersharinginindia 150527161221-lva1-app6892
PPTX
Triangle and its properties
PPTX
probability-game of chances
PPTX
Political and public awareness through media (B.st)
Frames and its components
Bank managment system
Monolithic and Procedural Programming
Inventory Management
Dynamic memory allocation and linked lists
Cyber Criminal and Cyber Security
Mdi vb.net
Stress Management
Impressionism
Powersharinginindia 150527161221-lva1-app6892
Triangle and its properties
probability-game of chances
Political and public awareness through media (B.st)

Recently uploaded (20)

PDF
Mushroom cultivation and it's methods.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
August Patch Tuesday
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation theory and applications.pdf
Mushroom cultivation and it's methods.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
Encapsulation_ Review paper, used for researhc scholars
August Patch Tuesday
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Getting Started with Data Integration: FME Form 101
Group 1 Presentation -Planning and Decision Making .pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
NewMind AI Weekly Chronicles - August'25-Week II
MIND Revenue Release Quarter 2 2025 Press Release
cloud_computing_Infrastucture_as_cloud_p
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectroscopy.pptx food analysis technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TLE Review Electricity (Electricity).pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Per capita expenditure prediction using model stacking based on satellite ima...
Tartificialntelligence_presentation.pptx
Network Security Unit 5.pdf for BCA BBA.
Encapsulation theory and applications.pdf

Group By, Having Clause and Order By clause

  • 1. GROUP BY,HAVING and ORDER BY clause DEEPAM AGGARWAL 1
  • 2. org payroll X 1500 Y 3500 Z 3000 GROUP BY Clause SELECT Statement – Grouping Rows Find the payrollof each organization. SELECT org, sum(salary) payroll FROM salary_statistics GROUP BY org Result: salary_statistics emp org salary A X 500 B X 1000 C Y 1000 D Y 2500 E Z 3000 2
  • 3. org payroll X 1500 Z 3000 HAVING Clause Find the organizations with payroll less than $3200. SELECT org, sum(salary) payroll FROM salary_statistics GROUP BY org HAVING payroll < 3200 Result: salary_statistics emp org salary A X 500 B X 1000 C Y 1000 D Y 2500 E Z 3000 3
  • 4. The Difference Between WHERE and HAVING Clauses  WHERE gets processed before any GROUP BY, and so it doesn't have access to aggregated values (that is, the results of min(), max(), etc. functions).  HAVING gets processed after GROUP BY and so can be used to constrain the result set to only those with aggregated values that match a certain predicate. Find the organizations with payroll less than $3200. SELECT org, sum(salary) payroll FROM salary_statistics }✗Incorrect WHERE payroll < 3200 GROUP BY org SELECT org, sum(salary) payroll FROM salary_statistics }✓ Correct GROUP BY org HAVING payroll < 3200 4
  • 5. SELECT Statement – ORDER BY Clause Find students ordered by merit position. SELECT * FROM result ORDER BY marks DESC result student marks A 25 B 56 C 82 D 25 E 39 5