Professional Documents
Culture Documents
Experiment Num 4 Record
Experiment Num 4 Record
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:
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.
o/p:
DEPTNO TOTAL_SALARIES
------ --------------
10 8750
20 10875
30 9400
Q2)Display the details of job wise average salaries.
o/p:
JOB AVERAGE_SALARIES
--------- ----------------
ANALYST 3000
CLERK 1037.5
MANAGER 2758.3333
PRESIDENT 5000
SALESMAN 1400
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:
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.
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.
o/p:
DEPTNO TOTAL_SALARIES
------- --------------
20 10875
30 9400
Q5)List the minimum salary of every department more than 5000.
o/p:
no rows selected
SQL ORDER BY clause
ORDER BY Syntax
SELECT column-list
FROM table_name
[WHERE condition]
ORDER BY column1 [, column2, .. columnN] [DESC];
14 rows selected.
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;
14 rows selected.