Clauses Task
Clauses Task
===========================
Syntax:-select columnname.......
group by columnname;
Execution order:-
==============
1.FROM
2.WHERE
3.GROUP BY
4.HAVING
5.SELECT
6.DISTINCT
7.ORDER BY
8.OFFSET
9.FETCH
===== then those all columns must be specified after group by.
expression.
eg1:-
====
SQL>select deptno,job
from emp
group by deptno,job;
DEPTNO JOB
---------- ---------
20 MANAGER
20 ANALYST
10 PRESIDENT
10 CLERK
30 SALESMAN
10 MANAGER
20 CLERK
30 MANAGER
30 CLERK
eg2:-
====
from emp
group by deptno,job;
20 MANAGER 4175
20 ANALYST 8400
10 PRESIDENT 6300
10 CLERK 2600
30 SALESMAN 10900
10 MANAGER 3750
20 CLERK 4300
30 MANAGER 3950
30 CLERK 2050
EG:-
==
group by deptno;
DEPTNO
----------
30
10
20
step1:-group by deptno;
------ ----------------
20 10---------------------> 10
30 10
20 30 ----------------------> 30
30 30
===== |
DEPTNO
----------
30
10
20
---------------------------------------------------
SUM(SAL)
----------
46425
|| ||
|| ||
executed
||
||Solution is
||
solution:-
=========
SQL>select deptno,sum(sal)
from emp
group by deptno;
||
-----------------------------------------------------------------------------------
group by
1 SUBBARAO AP AP 4
2 KIRAN TS TS 2
3 RAJU TS MP 1
4 ADITYA MP MH 1
5 GOWTHAM AP
6 VISWAJITH MH
7 SURYA AP
8 SAI TEJA AP
1 SUBBARAO M AP
2 KAVYA F TS
3 KIRAN M TS
4 GOWTHAM M AP
5 KERTHI F AP
6 SURYA M AP
7 ANUSHKA F AP
8 RAJU M TS
or
2 FROM PERSONS
2 FROM emp
3 GROUP BY deptno;
SQL> SELECT DEPTNO,COUNT(*) AS NUM_EMPS FROM EMP GROUP BY DEPTNO HAVING COU
NT(*)>3;
==== clause.
deptno
----------
10
10
10
20
20
20
30
30
40
50
G COUNT(*)>1;
2 FROM EMP
group by?
2 FROM emp
4 ORDER BY quarter;
2 FROM emp
3 GROUP BY deptno
SQL>select deptno,sum(sal)
from emp
group by deptno
having count(*)>3;
SQL>select to_char(orderdate,'yyyy'),sum(orderamt)
from orders
group by to_char(orderdate,'yyyy');
SQL>select to_char(orderdate,'q'),sum(orderamt)
from orders
group by to_char(orderdate,'q');
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
where | having
-----------------------------------------------------------------------------------
3.group fuctions can not be used in |3.group fuctions can be used in having
4.where gets executed before group by |4.having gets executed after group by
5.it can be used with out group by |5.it can not be used with out group by
------------------------------------------------------------------------------------