SlideShare a Scribd company logo
SQL Fundamentals
Part - 4
Topics
1. Aggregate Functions
2. Group By
3. Having
Aggregate Functions
● Sql provides variety of aggregate functions.
● It returns single output.
Most Common Aggregate Functions
1. AVG() Return avg value
2. COUNT() Return number of values
3. MAX() Return maximum value
4. MIN() Return minimum value
5. SUM() Return sum of all values
Aggregate Function
● Aggregate function only used with
Select Clause or having clause.
Group By
● Group By allows us to aggregate columns per some category.
Group By
Group By
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
Select category_column, AVG(data_col)
FROM table
GROUP BY category_column
Group By
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
Select category_column, AVG(data_col)
FROM table
WHERE category_col != ‘A’
GROUP BY category_column
Group By
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
Select category_column, AVG(data_col)
FROM table
WHERE category_col != ‘A’
GROUP BY category_column
ORDER BY AVG(data_col)
Group By
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
Select category_column, AVG(data_col)
FROM table
WHERE category_col != ‘A’
GROUP BY category_column
ORDER BY AVG(data_col)
LIMIT 5
Having
● Having Clause allows us to filter after an aggregation has already taken
place.
● To perform filtering on Group By.
Sql fundamentals   group by part2
Having
Select company, SUM(sales)
FROM finance_table
GROUP BY company
Having sum(sales) > 1000
Having
Select company, SUM(sales)
FROM finance_table
WHERE company != ‘Google’
GROUP BY company
Having sum(sales) > 1000
Thank You
Ad

More Related Content

What's hot (19)

SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference Card
Techcanvass
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
SANTOSH RATH
 
Les03
Les03Les03
Les03
Sudharsan S
 
Oracle SQL DML Statements
Oracle SQL DML StatementsOracle SQL DML Statements
Oracle SQL DML Statements
A Data Guru
 
DML Commands
DML CommandsDML Commands
DML Commands
Randy Riness @ South Puget Sound Community College
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
Felipe Costa
 
Obtain better data accuracy using reference tables
Obtain better data accuracy using reference tablesObtain better data accuracy using reference tables
Obtain better data accuracy using reference tables
Kiran Venna
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
Commands of DML in SQL
Commands of DML in SQLCommands of DML in SQL
Commands of DML in SQL
Ashish Gaurkhede
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
suman kumar
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
Swapnali Pawar
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
Giuseppe Maxia
 
SQL Views
SQL ViewsSQL Views
SQL Views
baabtra.com - No. 1 supplier of quality freshers
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference Card
Techcanvass
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
SANTOSH RATH
 
Oracle SQL DML Statements
Oracle SQL DML StatementsOracle SQL DML Statements
Oracle SQL DML Statements
A Data Guru
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
Felipe Costa
 
Obtain better data accuracy using reference tables
Obtain better data accuracy using reference tablesObtain better data accuracy using reference tables
Obtain better data accuracy using reference tables
Kiran Venna
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
suman kumar
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
Giuseppe Maxia
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 

Similar to Sql fundamentals group by part2 (20)

Les05
Les05Les05
Les05
Vijay Kumar
 
Les04
Les04Les04
Les04
Sudharsan S
 
Les04
Les04Les04
Les04
Achmad Solichin
 
Les04
Les04Les04
Les04
Sudharsan S
 
Les17
Les17Les17
Les17
Vijay Kumar
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
ssuserc5aa74
 
SQL Keywords
SQL KeywordsSQL Keywords
SQL Keywords
I L0V3 CODING DR
 
Lesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptx
Lesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptxLesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptx
Lesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptx
barorojason35
 
dbms.pdf
dbms.pdfdbms.pdf
dbms.pdf
walter brand
 
Reporting aggregated data using the group functions
Reporting aggregated data using the group functionsReporting aggregated data using the group functions
Reporting aggregated data using the group functions
Syed Zaid Irshad
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functions
ecomputernotes
 
Avinash database
Avinash databaseAvinash database
Avinash database
avibmas
 
T sql denali code Day of .Net
T sql denali code Day of .NetT sql denali code Day of .Net
T sql denali code Day of .Net
KathiK58
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Julian Hyde
 
Sql basics 2
Sql basics 2Sql basics 2
Sql basics 2
rchakra
 
SQL Class Note By Amit Maity PowerPoint Presentation
SQL Class Note By Amit Maity PowerPoint PresentationSQL Class Note By Amit Maity PowerPoint Presentation
SQL Class Note By Amit Maity PowerPoint Presentation
maitypradip938
 
Object oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAPObject oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAP
Noman Mohamed Hanif
 
Case 9 and 10. SQL For Data Analysis 4.pptx
Case 9 and 10. SQL For Data Analysis 4.pptxCase 9 and 10. SQL For Data Analysis 4.pptx
Case 9 and 10. SQL For Data Analysis 4.pptx
DilaTriarini1
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
MS SQL Database basic
MS SQL Database basicMS SQL Database basic
MS SQL Database basic
wali1195189
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
ssuserc5aa74
 
Lesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptx
Lesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptxLesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptx
Lesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptx
barorojason35
 
Reporting aggregated data using the group functions
Reporting aggregated data using the group functionsReporting aggregated data using the group functions
Reporting aggregated data using the group functions
Syed Zaid Irshad
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functions
ecomputernotes
 
Avinash database
Avinash databaseAvinash database
Avinash database
avibmas
 
T sql denali code Day of .Net
T sql denali code Day of .NetT sql denali code Day of .Net
T sql denali code Day of .Net
KathiK58
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Julian Hyde
 
Sql basics 2
Sql basics 2Sql basics 2
Sql basics 2
rchakra
 
SQL Class Note By Amit Maity PowerPoint Presentation
SQL Class Note By Amit Maity PowerPoint PresentationSQL Class Note By Amit Maity PowerPoint Presentation
SQL Class Note By Amit Maity PowerPoint Presentation
maitypradip938
 
Object oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAPObject oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAP
Noman Mohamed Hanif
 
Case 9 and 10. SQL For Data Analysis 4.pptx
Case 9 and 10. SQL For Data Analysis 4.pptxCase 9 and 10. SQL For Data Analysis 4.pptx
Case 9 and 10. SQL For Data Analysis 4.pptx
DilaTriarini1
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
MS SQL Database basic
MS SQL Database basicMS SQL Database basic
MS SQL Database basic
wali1195189
 
Ad

Recently uploaded (20)

Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Ad

Sql fundamentals group by part2

  • 2. Topics 1. Aggregate Functions 2. Group By 3. Having
  • 3. Aggregate Functions ● Sql provides variety of aggregate functions. ● It returns single output.
  • 4. Most Common Aggregate Functions 1. AVG() Return avg value 2. COUNT() Return number of values 3. MAX() Return maximum value 4. MIN() Return minimum value 5. SUM() Return sum of all values
  • 5. Aggregate Function ● Aggregate function only used with Select Clause or having clause.
  • 6. Group By ● Group By allows us to aggregate columns per some category.
  • 8. Group By CREATE TABLE food ( food_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, type VARCHAR NOT NULL, created_on TIMESTAMP NOT NULL ); CREATE TABLE food ( food_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, type VARCHAR NOT NULL, created_on TIMESTAMP NOT NULL ); Select category_column, AVG(data_col) FROM table GROUP BY category_column
  • 9. Group By CREATE TABLE food ( food_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, type VARCHAR NOT NULL, created_on TIMESTAMP NOT NULL ); CREATE TABLE food ( food_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, type VARCHAR NOT NULL, created_on TIMESTAMP NOT NULL ); Select category_column, AVG(data_col) FROM table WHERE category_col != ‘A’ GROUP BY category_column
  • 10. Group By CREATE TABLE food ( food_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, type VARCHAR NOT NULL, created_on TIMESTAMP NOT NULL ); CREATE TABLE food ( food_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, type VARCHAR NOT NULL, created_on TIMESTAMP NOT NULL ); Select category_column, AVG(data_col) FROM table WHERE category_col != ‘A’ GROUP BY category_column ORDER BY AVG(data_col)
  • 11. Group By CREATE TABLE food ( food_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, type VARCHAR NOT NULL, created_on TIMESTAMP NOT NULL ); CREATE TABLE food ( food_id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, type VARCHAR NOT NULL, created_on TIMESTAMP NOT NULL ); Select category_column, AVG(data_col) FROM table WHERE category_col != ‘A’ GROUP BY category_column ORDER BY AVG(data_col) LIMIT 5
  • 12. Having ● Having Clause allows us to filter after an aggregation has already taken place. ● To perform filtering on Group By.
  • 14. Having Select company, SUM(sales) FROM finance_table GROUP BY company Having sum(sales) > 1000
  • 15. Having Select company, SUM(sales) FROM finance_table WHERE company != ‘Google’ GROUP BY company Having sum(sales) > 1000