SlideShare a Scribd company logo
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

DOCX
Database Query Using SQL_ip.docx
PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
PDF
Introduction to oracle functions
PPTX
12. Basic SQL Queries (2).pptx
PPT
Aggregate Functions,Final
PPT
PPT
PPT
Sql query [select, sub] 4
Database Query Using SQL_ip.docx
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to oracle functions
12. Basic SQL Queries (2).pptx
Aggregate Functions,Final
Sql query [select, sub] 4

Similar to SQL.pptx (20)

PDF
Group by clause mod
PPT
PPT
Oracle tips and tricks
PPTX
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
PPT
Module03
PPT
Databasessanddataanalysis122222222222.ppt
PPT
Reporting aggregated data using the group functions
PDF
Dynamic websites lec2
PPT
PHP mysql Aggregate functions
PPTX
Lecture 8 DML3 aggregate functions in DB.pptx
PDF
MySQL-commands.pdf
PPTX
SQL (1).pptx
PPTX
SQL Data Manipulation language and DQL commands
PPTX
Oracle: Functions
PPTX
Oracle: Functions
PPTX
Lab3 aggregating data
PPT
Aggregating Data Using Group Functions
PPT
SQL5.ppt
PPTX
Complex Queries using MYSQL00123211.pptx
Group by clause mod
Oracle tips and tricks
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
Module03
Databasessanddataanalysis122222222222.ppt
Reporting aggregated data using the group functions
Dynamic websites lec2
PHP mysql Aggregate functions
Lecture 8 DML3 aggregate functions in DB.pptx
MySQL-commands.pdf
SQL (1).pptx
SQL Data Manipulation language and DQL commands
Oracle: Functions
Oracle: Functions
Lab3 aggregating data
Aggregating Data Using Group Functions
SQL5.ppt
Complex Queries using MYSQL00123211.pptx
Ad

Recently uploaded (20)

PDF
.pdf is not working space design for the following data for the following dat...
PDF
annual-report-2024-2025 original latest.
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Computer network topology notes for revision
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
Managing Community Partner Relationships
PPTX
SAP 2 completion done . PRESENTATION.pptx
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PDF
Optimise Shopper Experiences with a Strong Data Estate.pdf
PPTX
Database Infoormation System (DBIS).pptx
PPT
Quality review (1)_presentation of this 21
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
Leprosy and NLEP programme community medicine
.pdf is not working space design for the following data for the following dat...
annual-report-2024-2025 original latest.
Data_Analytics_and_PowerBI_Presentation.pptx
Reliability_Chapter_ presentation 1221.5784
Miokarditis (Inflamasi pada Otot Jantung)
Computer network topology notes for revision
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
IB Computer Science - Internal Assessment.pptx
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
climate analysis of Dhaka ,Banglades.pptx
Introduction-to-Cloud-ComputingFinal.pptx
Managing Community Partner Relationships
SAP 2 completion done . PRESENTATION.pptx
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Optimise Shopper Experiences with a Strong Data Estate.pdf
Database Infoormation System (DBIS).pptx
Quality review (1)_presentation of this 21
Supervised vs unsupervised machine learning algorithms
Leprosy and NLEP programme community medicine
Ad

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