Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

EXPERIMRNT-4

Queries using Group By, Order By, and Having Clauses

SQL: GROUP BY Clause

Description
The SQL GROUP BY clause can be used in a SELECT statement to
collect data across multiple records and group the results by one or more
columns.

Syntax
The syntax for the GROUP BY clause in SQL is:

SELECT column1, column2, ... column_n,


aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY column1, column2, ... column_n;

Parameters or Arguments
Column1, Column2, ... Column_n
Columns that are not encapsulated within an aggregate function
and must be included in the GROUP BY Clause at the end of the
SQL statement.
aggregate_function
This is an aggregate function such as
the SUM, COUNT, MIN, MAX, or AVG functions.
aggregate_expression
This is the column or expression that the aggregate_function will
be used on.

tables
The tables that you wish to retrieve records from. There must be at
least one table listed in the FROM clause.
WHERE conditions
Optional. These are conditions that must be met for the records to
be selected.

Q1)Display the details of department wise sum of salaries.

SELECT deptno,SUM(sal) AS total_salaries


FROM emp
GROUP BY deptno;

o/p:
DEPTNO TOTAL_SALARIES
------ --------------
10 8750
20 10875
30 9400
Q2)Display the details of job wise average salaries.

SELECT job,AVG(sal) AS Average_salaries


FROM emp
GROUP BY job;

o/p:

JOB AVERAGE_SALARIES
--------- ----------------
ANALYST 3000
CLERK 1037.5
MANAGER 2758.3333
PRESIDENT 5000
SALESMAN 1400

Q3) Display the details of department wise maximum and minimum


salaries.

SELECT deptno, MAX(sal) AS highest_salary ,MIN(sal) AS


lowest_salary
FROM emp
GROUP BY deptno;
o/p:

DEPTNO HIGHEST_SALARY LOWEST_SALARY


------ -------------- -------------
10 5000 1300
20 3000 800
30 2850 950
Q4)Display the details of number of employees in each and every year.
SELECT TO_CHAR(hiredate,’yyyy’) as year,count( empno)
FROM emp 
GROUP BY TO_CHAR(hiredate,’yyyy’);
0/p:

YEAR COUNT(EMPNO)
---- ------------
1980 1
1981 10
1982 1
1987 2
Q5)Display the details of number of employees in each and every
month.
SELECT TO_CHAR(hiredate,’mon’) as month,count( empno)
FROM emp 
GROUP BY TO_CHAR(hiredate,’mon’);
0/p:

MON COUNT(EMPNO)
--- ------------
apr 2
dec 3
feb 2
jan 1
jun 1
may 2
nov 1
sep 2

8 rows selected.
Q6) Display department wise total salary spent for each job
category
SELECT deptno,job,SUM(sal)
FROM emp 
GROUP BY deptno,job;
o/p:
DEPTNO JOB SUM(SAL)
--------- --------- ---------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600

9 rows selected.
SQL HAVING Clause
The HAVING clause is often used with the GROUP BY clause in
the SELECT statement to filter group of rows based on a specified
condition. The HAVING clause was added to SQL because the
WHERE keyword could not be used with aggregate functions. The
WHERE clause places conditions on the selected columns, whereas the
HAVING clause places conditions on groups created by the GROUP
BY clause.
Syntax
The syntax for the HAVING clause in SQL is:

SELECT expression1, expression2, ... expression_n,


aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
HAVING condition;

Parameters or Arguments
expression1, expression2, ... expression_n
Expressions that are not encapsulated within an aggregate function
and must be included in the GROUP BY Clause near the end of the
SQL statement.
aggregate_function
This is an aggregate function such as
the SUM, COUNT, MIN, MAX, or AVG functions.
aggregate_expression
This is the column or expression that the aggregate_function will
be used on.
tables
The tables that you wish to retrieve records from. There must be at
least one table listed in the FROM clause.
WHERE conditions
Optional. These are the conditions for the records to be selected.
HAVING condition
This is a further condition applied only to the aggregated results to
restrict the groups of returned rows. Only those groups whose
condition evaluates to TRUE will be included in the result set.

Q1) List the Deptno where there are no emps.


select deptno ,count(*) from emp
group by deptno
having count(*) = 0;
Q2) List the no. of emps in each department where the no. is
more than 3.

SELECT deptno, COUNT(*) AS "Number of employees"


FROM emp
GROUP BY deptno
HAVING COUNT(*) > 3;

o/p:
DEPTNO Number of employees
-------- -------------------
20 5
30 6

Q3) List the department details where at least two emps are
working
select deptno ,count(*) from emp group by deptno having
count(*) >= 2;
Q4)List the total salary of every department more than 9000.

SELECT deptno,SUM(sal) AS total_salaries


FROM emp
GROUP BY deptno
HAVING SUM(sal)>9000;

o/p:
DEPTNO TOTAL_SALARIES
------- --------------
20 10875
30 9400
Q5)List the minimum salary of every department more than 5000.

SELECT deptno, MIN(sal) AS "Lowest salary"


FROM emp
GROUP BY deptno
HAVING MIN(sal) > 5000;

o/p:
no rows selected
SQL ORDER BY clause

The ORDER BY keyword is used to sort the result-set in ascending or


descending order.

The ORDER BY keyword sorts the records in ascending order by


default. To sort the records in descending order, use the DESC keyword.

ORDER BY Syntax
SELECT column-list 
FROM table_name
[WHERE condition] 
ORDER BY column1 [, column2, .. columnN] [DESC];

Q1)List the emps in the asc order of their Salaries?


select * from emp order by sal asc;
OR
SELECT * FROM emp
ORDER BY sal;
o/p:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

--------- ---------- --------- --------- --------- --------- --------- ---------

7369 SMITH CLERK 7902 17-DEC-80 800 20

7900 JAMES CLERK 7698 03-DEC-81 950 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20


7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7934 MILLER CLERK 7782 23-JAN-82 1300 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 10

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7839 KING PRESIDENT 17-NOV-81 5000 10

14 rows selected.

Q2)List the emps in the descending order of their Salaries?


SELECT * FROM emp
ORDER BY sal DESC;
o/p:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

--------- ---------- --------- --------- --------- --------- --------- ---------

7839 KING PRESIDENT 17-NOV-81 5000 10

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7566 JONES MANAGER 7839 02-APR-81 2975 20

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30


7782 CLARK MANAGER 7839 09-JUN-81 2450 10

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7934 MILLER CLERK 7782 23-JAN-82 1300 10

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20

7900 JAMES CLERK 7698 03-DEC-81 950 30

7369 SMITH CLERK 7902 17-DEC-80 800 20

14 rows selected.

Q3)List the Empno, Ename, Sal, Daily sal,annual sal of all emps
in the asc order of Annsal.
select empno ,ename ,sal,sal/30,12*sal annsal from emp order
by annsal asc;
Q4) Display all the unique job groups in the descending order?
select distinct job from emp order by job desc;
Q5)List all the emps except ‘PRESIDENT’ & ‘MGR” in asc
order of Salaries.
Select * from emp where job not in
(‘PRESIDENT’,’MANAGER’) order by sal asc;
or
select * from emp where job not like ‘PRESIDENT’ and job not
like ‘MANAGER’ order by sal asc;
or
Select * from emp where job != ‘PRESIDENT’ and job <>
‘MANAGER’ order by sal asc;
Q6)List the enames in alphabetical order along with their jobs
SELECT ename,job FROM emp
ORDER BY ename;

Q7) Find out the most recently hired emps


SELECT * FROM emp
ORDER BY hiredate desc;

Q8)List the empno,ename,sal,deptno of the dept 10 emps in the


ascending order of salary.
select e.empno,e.ename,e.sal,e.deptno from emp where e.deptno
= 10 order by e.sal asc;
Q9) sort the employee table by the salary and name
SELECT sal,ename FROM emp 
ORDER BY sal,ename; 
or
SELECT sal,ename FROM emp 
ORDER BY 1,2; 
o/p:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

--------- ---------- --------- --------- --------- --------- --------- ---------

7369 SMITH CLERK 7902 17-DEC-80 800 20

7900 JAMES CLERK 7698 03-DEC-81 950 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7934 MILLER CLERK 7782 23-JAN-82 1300 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 10

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7839 KING PRESIDENT 17-NOV-81 5000 10

14 rows selected.

You might also like