5) Clauses
5) Clauses
5) Clauses
========
- a clause is a statement which is used to add to sql query for providing
some additional facilities are "filtering rows,sorting values,grouping data,to find
sub total and grand total " based on column / columns automatically.
- oracle is supporting the following clauses are,
> WHERE
> ORDER BY
> GROUP BY
> HAVING
> ROLLUP
> CUBE
syntax:
=======
< SQL query> + <Clause statement>;
WHERE :
========
- filtering rows before grouping data in a table.
- it can apply on "select,update,delete" commands only.
syntax:
=======
where <filtering condition>
Ex:
SELECT * FROM EMP WHERE EMPNO=7788;
UPDATE EMP SET SAL=45000 WHERE ENAME='SMITH';
DELETE FROM EMP WHERE SAL=5000;
ORDER BY:
=========
- to arrange a specific column values either in ascending / descending
order.by default order by clause will arrange the values in ascending order only
if we want to arrange the values in descending order then use a keyword is "desc".
- it can apply on "select command" only.
syntax:
=======
select * from <table name> order by <column name1> <asc/desc>,..................;
Ex:
SQL> SELECT * FROM EMP ORDER BY SAL;
SQL> SELECT * FROM EMP ORDER BY SAL DESC;
Ex:
waq to display employees who are working under deptno is 30 and arrange those
employees salaries in descending order?
SQL> SELECT * FROM EMP WHERE DEPTNO=30 ORDER BY SAL DESC;
Ex:
waq to arrange the employee department numbers in ascending order and their
salaries in descending for each deptno wise?
SQL> SELECT * FROM EMP ORDER BY DEPTNO,SAL DESC;
NOTE:
=====
- order by clause can apply on not only columns even though we can apply
on position of column in select query.
Ex:
SQL> SELECT * FROM EMP ORDER BY SAL DESC;
(or)
SQL> SELECT * FROM EMP ORDER BY 6 DESC;
GROUP BY:
==========
- this is clause is used for grouping data from a table.
- when we use "group by" we must use "grouping / aggregative functions"
are COUNT(),SUM(),AVG(),MIN() and MAX().
- it can apply on "select command" only.
syntax:
======
select <column name1>,<column name2>,......,<grouping function name1>,..........
from <table name> group by <column name1>,<column name2>,........);
Ex:
SQL> SELECT * FROM STUDENTS;
Ex:
waq to find out no.of students in a class room?
SQL> SELECT COUNT(*) FROM STUDENTS;
COUNT(*)
------------------
5
Ex:
waq to find out no.of students from each gender wise?
SQL> SELECT GENDER,COUNT(*) AS NO_OF_STUDENTS FROM STUDENTS GROUP BY GENDER;
G NO_OF_STUDENTS
--- --------------
M 3
F 2
Ex:
waq to find out no.of employees are working in each job wise?
SQL> SELECT JOB,COUNT(*) NO_OF_EMPLOYEES FROM EMP GROUP BY JOB;
Ex:
waq to find out no.of employees are working in each job along with their deptno
wise?
SQL> SELECT JOB,DEPTNO,COUNT(*) FROM EMP
2 GROUP BY JOB,DEPTNO ORDER BY JOB,DEPTNO;
Ex:
waq to display sum of salaries of each deptno?
SQL> SELECT DEPTNO,SUM(SAL) FROM EMP GROUP BY DEPTNO
ORDER BY DEPTNO;
Ex:
SQL> SELECT DEPTNO,COUNT(*) NO_OF_EMPLOYEES,
2 SUM(SAL) TOTAL_AMOUNT,
3 AVG(SAL) AVG_SALARY,
4 MIN(SAL) MIN_SALARY,
5 MAX(SAL) MAX_SALARY
6 FROM EMP GROUP BY DEPTNO ORDER BY DEPTNO;
HAVING:
========
- filtering rows after grouping data in a table.
- it should be used after "group by" clause only.
syntax:
======
select <column name1>,<column name2>,......,<grouping function name1>,..........
from <table name> group by <column name1>,<column name2>,............
having <filtering condition>;
Ex:
waq to display jobs in which job the no.of employees are more than 3 ?
SQL> SELECT JOB,COUNT(*) FROM EMP GROUP BY JOB
2 HAVING COUNT(*)>3;
JOB COUNT(*)
--------- ----------
CLERK 4
SALESMAN 4
Ex:
waq to display deptno's in which deptno the sum of salary is less than to 10000?
SQL> SELECT DEPTNO,SUM(SAL) FROM EMP GROUP BY DEPTNO
2 HAVING SUM(SAL)<10000 ORDER BY DEPTNO;
Ex:
waq to display gender if the no.of students are more than 2?
SQL> SELECT GENDER,COUNT(*) FROM STUDENTS GROUP BY GENDER
2 HAVING COUNT(*)>2;
G COUNT(*)
--- ----------
M 3
GROUPING_ID():
==============
- it is pre-defined function in oracle.
- it is used to generate id numbers for sub total rows and grand total row.
Ex:
grouping_id(deptno,job)
> 1st grouping column sub total rows id is --- 1
> 2nd grouping column sub total rows id is --- 2
> grand total row id is ----------------------------------> 3
Ex:
SQL> SELECT DEPTNO,JOB,COUNT(*),GROUPING_ID(DEPTNO,JOB) FROM EMP
GROUP BY CUBE(DEPTNO,JOB) ORDER BY DEPTNO,JOB;