0% found this document useful (0 votes)
3 views

Notes_on_group_by

The document outlines SQL commands and functions including DDL, DML, TCL, and DQL, along with various SQL clauses such as SELECT, WHERE, and GROUP BY. It explains the use of aggregate functions like MAX, MIN, SUM, AVG, and COUNT, emphasizing that these functions operate on sets of records rather than individual records. Additionally, it provides examples of SQL queries to calculate maximum salaries by department and job ID, highlighting the importance of including non-aggregate columns in the GROUP BY clause.

Uploaded by

nachiket dhabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Notes_on_group_by

The document outlines SQL commands and functions including DDL, DML, TCL, and DQL, along with various SQL clauses such as SELECT, WHERE, and GROUP BY. It explains the use of aggregate functions like MAX, MIN, SUM, AVG, and COUNT, emphasizing that these functions operate on sets of records rather than individual records. Additionally, it provides examples of SQL queries to calculate maximum salaries by department and job ID, highlighting the importance of including non-aggregate columns in the GROUP BY clause.

Uploaded by

nachiket dhabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

DDL

DML
TCL
DQL

SELECT
WHERE
AND
OR
NOT
LIKE
BETWEEN
IN

DESC
COUNT
DISTINCT
ORDER BY

IS NULL
IS NOT NULL

GROUP FUNCTIONS (AGGREGATE FUNCTIONS)


&
GROUP BY

GROUP FUNCTIONS (AGGREGATE FUNCTIONS) :

MAX()
MIN()
SUM()
AVG()
COUNT()

Group functions will work on set of the records. they will not work on single
record.

select * from employees;

SELECT MAX(SALARY) , MIN(SALARY) , SUM(SALARY) , AVG(SALARY) , COUNT(SALARY) FROM


EMPLOYEES;

SELECT SUM(COMMISSION_PCT) , AVG(COMMISSION_PCT) FROM EMPLOYEES;

SELECT NVL(COMMISSION_PCT,0) FROM EMPLOYEES;

SELECT SUM(NVL(COMMISSION_PCT,0)) , AVG(NVL(COMMISSION_PCT,0)) FROM EMPLOYEES;

Q: CALCULATE MAXIMUM SALARY IN EACH DEPARTMENT


Ans : Select DEPARTMENT_ID , MAX(SALARY)
FROM employees
GROUP BY DEPARTMENT_ID;
SELECT DEPARTMENT_ID ,MAX(SALARY) , MIN(SALARY) ,SUM(SALARY) , COUNT(SALARY),
AVG(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
ORDER BY DEPARTMENT_ID;

Q: CALCULATE MAXIMUM SALARY IN EACH DEPARTMENT AND IN EACH JOB_ID


Ans : SELECT DEPARTMENT_ID, JOB_ID , MAX(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID,JOB_ID

Rule : The columns which are out of the group functions in the select statement ,
must be present in group by clause.

Eg:
SELECT DEPARTMENT_ID , JOB_ID , MANAGER_ID , MAX(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID , JOB_ID , MANAGER_ID
ORDER BY DEPARTMENT_ID , JOB_ID , MANAGER_ID;

You might also like