SlideShare a Scribd company logo
1 of 19
SQL
Structured Query Language
Lets do practical on DATABASE…
AGGREGATE functions
Aggregate function/Group Function is used to perform calculation
on group of rows and return the calculated summary like sum of
salary, average of salary etc.
Available aggregate functions are –
1. SUM()
2. AVG()
3. COUNT()
4. MAX()
5. MIN()
6. COUNT(*)
AGGREGATE functions
Select SUM(salary) from emp;
Output – 161000
Select SUM(salary) from emp where dept=‘sales’;
Output – 59000
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
AGGREGATE functions
Select AVG(salary) from emp;
Output – 32200
Select AVG(salary) from emp where dept=‘sales’;
Output - 29500
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
AGGREGATE functions
Select COUNT(name) from emp;
Output – 5
Select COUNT(salary) from emp where dept=‘HR’;
Output - 1
Select COUNT(DISTINCT dept) from emp;
Output - 3
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
AGGREGATE functions
Select MAX(Salary) from emp;
Output – 45000
Select MAX(salary) from emp where dept=‘Sales’;
Output - 35000
Find the second Highest Salary from Emp Relation?
Select max(salary) from emp where salary<select max(salary) from emp;
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
AGGREGATE functions
Select MIN(Salary) from emp;
Output – 24000
Select MIN(salary) from emp where dept=‘IT’;
Output - 27000
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
AGGREGATE functions
Select COUNT(*) from emp;
Output – 6
Select COUNT(salary) from emp;
Output - 5
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
6 Krish HR
count(*) Vs count()
Count(*) function is used to count the number of rows in query
output whereas count() is used to count values present in any
column excluding NULL values.
Note:
All aggregate function ignores the NULL values.
GROUP BY
GROUP BY clause is used to divide the table into logical groups and
we can perform aggregate functions in those groups. In this case
aggregate function will return output for each group. For example
if we want sum of salary of each department we have to divide
table records
Aggregate functions by default takes the entire table as a single group
that’s why we are getting the sum(), avg(), etc output for the entire table.
Now suppose organization wants the sum() of all the job separately, or
wants to find the average salary of every job. In this case we have to
logically divide our table into groups based on job, so that every group will
be passed to aggregate function for calculation and aggregate function will
return the result for every group.
aggregate
column value. In those logically divided records we can apply
functions.
For. E.g.
SELECT SUM(SAL) FROM EMP GROUP BY DEPT;
SELECT JOB,SUM(SAL) FROM EMP GROUP BY DEPT;
NOTE :- when we are using GROUP BY we can use only aggregate function and
the column on which we are grouping in the SELECT list because they will form a
group other than any column will gives you an error because they will be not
the part of the group.
For e.g.
SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB;
Error -> because Ename is not a group expression
HAVING with GROUP BY
• If we want to filter or restrict some rows from the output produced by
GROUP BY then we use HAVING clause. It is used to put condition of group of
rows. With having clause we can use aggregate functions also.
• WHERE is used before the GROUP BY. With WHERE we cannot use aggregate
function.
• E.g.
• SELECT DEPT,AVG(SAL) FROM EMP GROUP BY DEPT HAVING
JOB IN (‘HR’,’SALES’)
• SELECT DEPT,MAX(SAL),MIN(SAL),COUNT(*) FROM EMP
GROUP BY DEPT HAVING COUNT(*)>2
• SELECT DEPT,MAX(SAL),MIN(SAL) FROM EMP WHERE
SAL>=2000 GROUP BY DEPT HAVING DEPT IN(‘IT’,’HR’)
JUST A MINUTE…
• Create the following table and add the records
ItemNo Item Dcode Qty UnitPrice StockDate
5005 Ball Pen 0.5 102 100 16 2018-03-10
5003 Ball Pen 0.25 102 150 20 2017-05-17
5002 Gel Pen Premium 101 125 14 2018-04-20
5006 Gel Pen Classic 101 200 22 2018-10-08
5001 Eraser Small 102 210 5 2018-03-11
5004 Eraser Big 102 60 10 2017-11-18
5009 Sharpener Classic NULL 160 8 2017-06-12
JUST A MINUTE…(Few Basic Questions )
Write down the following queries based on the given table:
1) Select all record of table
2) Select ItemNo, name and Unitprice
3) Select all item record where Unitprice is more than 20
4) Select Item name of those items which are quantity
between 100-200
5) Select all record of Items which contains pen word in it
6) Select unique dcode of all items
7) Display all record in the descending order of UnitPrice
8) Display all items which are stocked in the month of March
JUST A MINUTE…(Group By/ Having )
CODE TEACHER SUBJECT DOJ PERIODS EXPERIENCE
1001 RAVI ENGLISH 12/03/2000 24 10
1009 PRIYA PHYSICS 03/09/1998 26 12
1203 LISA ENGLISH 09/04/2000 27 5
1045 YASH RAJ MATHS 24/08/2000 24 15
1123 GAGAN PHYSICS 16/07/1999 28 3
1167 HARISH CHEMISTRY 19/10/1999 27 5
1215 UMESH PHYSICS 11/05/1998 22 16
TABLE: SCHOOL
(a) consider the following tables School and Admin and answer the following
questions:
TABLE : ADMIN
CODE GENDER DESIGNATION
1001 MALE VICE PRINCIPAL
1009 FEMALE COORDINATOR
1203 FEMALE COORDINATOR
1045 MALE HOD
1123 MALE SENIOR TEACHER
1167 MALE SENIOR TEACHER
1215 MALE HOD
JUST A MINUTE…(Group By/ Having )
Give the output of the following SQL queries:
i. Select Designation, count(*) from Admin Group by
Designation having count(*)<2;
ii. Select max(experience) from school;
iii.Select teacher from school where experience >12 order
by teacher;
iv.Select count(*), gender from admin group by gender;
JUST A MINUTE…(Group By/ Having )
Write the queries for the following:
i. Display the subject wise total employee for those
subject only where employee count is more than 01.
ii. Display the total no of male employee only.
iii.Display the total experience owned by Physics subject
Whats App Number : 9636770012
Follow this link to join my
WhatsApp group:
https://chat.whatsapp.com/Ct7bBG
sbC0uDE6Pky2BS4s

More Related Content

Similar to SQL.pptx

Similar to SQL.pptx (20)

Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
 
Les05
Les05Les05
Les05
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Module03
Module03Module03
Module03
 
Clauses
ClausesClauses
Clauses
 
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
 
Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
 
SQL (1).pptx
SQL (1).pptxSQL (1).pptx
SQL (1).pptx
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
SQL5.ppt
SQL5.pptSQL5.ppt
SQL5.ppt
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptx
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
Les04
Les04Les04
Les04
 
Oracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurOracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |Thrissur
 
Ctes percona live_2017
Ctes percona live_2017Ctes percona live_2017
Ctes percona live_2017
 

Recently uploaded

Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
ELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxolyaivanovalion
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...amitlee9823
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...amitlee9823
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 

Recently uploaded (20)

Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
ELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 

SQL.pptx

  • 1. SQL Structured Query Language Lets do practical on DATABASE…
  • 2. AGGREGATE functions Aggregate function/Group Function is used to perform calculation on group of rows and return the calculated summary like sum of salary, average of salary etc. Available aggregate functions are – 1. SUM() 2. AVG() 3. COUNT() 4. MAX() 5. MIN() 6. COUNT(*)
  • 3. AGGREGATE functions Select SUM(salary) from emp; Output – 161000 Select SUM(salary) from emp where dept=‘sales’; Output – 59000 Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000
  • 4. AGGREGATE functions Select AVG(salary) from emp; Output – 32200 Select AVG(salary) from emp where dept=‘sales’; Output - 29500 Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000
  • 5. AGGREGATE functions Select COUNT(name) from emp; Output – 5 Select COUNT(salary) from emp where dept=‘HR’; Output - 1 Select COUNT(DISTINCT dept) from emp; Output - 3 Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000
  • 6. AGGREGATE functions Select MAX(Salary) from emp; Output – 45000 Select MAX(salary) from emp where dept=‘Sales’; Output - 35000 Find the second Highest Salary from Emp Relation? Select max(salary) from emp where salary<select max(salary) from emp; Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000
  • 7. AGGREGATE functions Select MIN(Salary) from emp; Output – 24000 Select MIN(salary) from emp where dept=‘IT’; Output - 27000 Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000
  • 8. AGGREGATE functions Select COUNT(*) from emp; Output – 6 Select COUNT(salary) from emp; Output - 5 Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 6 Krish HR
  • 9. count(*) Vs count() Count(*) function is used to count the number of rows in query output whereas count() is used to count values present in any column excluding NULL values. Note: All aggregate function ignores the NULL values.
  • 10. GROUP BY GROUP BY clause is used to divide the table into logical groups and we can perform aggregate functions in those groups. In this case aggregate function will return output for each group. For example if we want sum of salary of each department we have to divide table records
  • 11. Aggregate functions by default takes the entire table as a single group that’s why we are getting the sum(), avg(), etc output for the entire table. Now suppose organization wants the sum() of all the job separately, or wants to find the average salary of every job. In this case we have to logically divide our table into groups based on job, so that every group will be passed to aggregate function for calculation and aggregate function will return the result for every group.
  • 12. aggregate column value. In those logically divided records we can apply functions. For. E.g. SELECT SUM(SAL) FROM EMP GROUP BY DEPT; SELECT JOB,SUM(SAL) FROM EMP GROUP BY DEPT; NOTE :- when we are using GROUP BY we can use only aggregate function and the column on which we are grouping in the SELECT list because they will form a group other than any column will gives you an error because they will be not the part of the group. For e.g. SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB; Error -> because Ename is not a group expression
  • 13. HAVING with GROUP BY • If we want to filter or restrict some rows from the output produced by GROUP BY then we use HAVING clause. It is used to put condition of group of rows. With having clause we can use aggregate functions also. • WHERE is used before the GROUP BY. With WHERE we cannot use aggregate function. • E.g. • SELECT DEPT,AVG(SAL) FROM EMP GROUP BY DEPT HAVING JOB IN (‘HR’,’SALES’) • SELECT DEPT,MAX(SAL),MIN(SAL),COUNT(*) FROM EMP GROUP BY DEPT HAVING COUNT(*)>2 • SELECT DEPT,MAX(SAL),MIN(SAL) FROM EMP WHERE SAL>=2000 GROUP BY DEPT HAVING DEPT IN(‘IT’,’HR’)
  • 14. JUST A MINUTE… • Create the following table and add the records ItemNo Item Dcode Qty UnitPrice StockDate 5005 Ball Pen 0.5 102 100 16 2018-03-10 5003 Ball Pen 0.25 102 150 20 2017-05-17 5002 Gel Pen Premium 101 125 14 2018-04-20 5006 Gel Pen Classic 101 200 22 2018-10-08 5001 Eraser Small 102 210 5 2018-03-11 5004 Eraser Big 102 60 10 2017-11-18 5009 Sharpener Classic NULL 160 8 2017-06-12
  • 15. JUST A MINUTE…(Few Basic Questions ) Write down the following queries based on the given table: 1) Select all record of table 2) Select ItemNo, name and Unitprice 3) Select all item record where Unitprice is more than 20 4) Select Item name of those items which are quantity between 100-200 5) Select all record of Items which contains pen word in it 6) Select unique dcode of all items 7) Display all record in the descending order of UnitPrice 8) Display all items which are stocked in the month of March
  • 16. JUST A MINUTE…(Group By/ Having ) CODE TEACHER SUBJECT DOJ PERIODS EXPERIENCE 1001 RAVI ENGLISH 12/03/2000 24 10 1009 PRIYA PHYSICS 03/09/1998 26 12 1203 LISA ENGLISH 09/04/2000 27 5 1045 YASH RAJ MATHS 24/08/2000 24 15 1123 GAGAN PHYSICS 16/07/1999 28 3 1167 HARISH CHEMISTRY 19/10/1999 27 5 1215 UMESH PHYSICS 11/05/1998 22 16 TABLE: SCHOOL (a) consider the following tables School and Admin and answer the following questions: TABLE : ADMIN CODE GENDER DESIGNATION 1001 MALE VICE PRINCIPAL 1009 FEMALE COORDINATOR 1203 FEMALE COORDINATOR 1045 MALE HOD 1123 MALE SENIOR TEACHER 1167 MALE SENIOR TEACHER 1215 MALE HOD
  • 17. JUST A MINUTE…(Group By/ Having ) Give the output of the following SQL queries: i. Select Designation, count(*) from Admin Group by Designation having count(*)<2; ii. Select max(experience) from school; iii.Select teacher from school where experience >12 order by teacher; iv.Select count(*), gender from admin group by gender;
  • 18. JUST A MINUTE…(Group By/ Having ) Write the queries for the following: i. Display the subject wise total employee for those subject only where employee count is more than 01. ii. Display the total no of male employee only. iii.Display the total experience owned by Physics subject
  • 19. Whats App Number : 9636770012 Follow this link to join my WhatsApp group: https://chat.whatsapp.com/Ct7bBG sbC0uDE6Pky2BS4s