5) Clauses

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

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;

SQL> SELECT * FROM EMP ORDER BY ENAME;


SQL> SELECT * FROM EMP ORDER BY ENAME DESC;

SQL> SELECT * FROM EMP ORDER BY HIREDATE;


SQL> SELECT * FROM EMP ORDER BY HIREDATE 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;

SQL> SELECT EMPNO,ENAME,SAL FROM EMP ORDER BY 3 DESC;


SQL> SELECT ENAME,SAL FROM EMP ORDER BY 2 DESC;
SQL> SELECT SAL FROM EMP ORDER BY 1 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;

STID SNAME SFEE GENDER


---------- ---------- --------- ------------
1 SMITH 4500 M
2 ALLEN 5500 F
3 WARD 3500 F
4 ADAMS 8500 M
5 SCOTT 8500 M

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

ROLLUP & CUBE:


===============
- these special clauses in DB which are used to find sub total and
grand total based on column / columns automatically.
- these clauses can be implemented along with "group by" clause only.
ROLLUP : to find out sub & grand total based on a single column.
CUBE : to find out sub & grand total based on multiple columns.

syntax for rollup:


===============
select <column name1>,<column name2>,......,<grouping function name1>,..........
from <table name> group by rollup(<column name1>,<column name2>,......);
| |
operational column supporting columns.

Ex.on Rollup with a single column:


=============================
SQL> SELECT DEPTNO,COUNT(*) FROM EMP GROUP BY ROLLUP(DEPTNO);

Ex.on Rollup with multiple columns:


==============================
SQL> SELECT DEPTNO,JOB,COUNT(*) FROM EMP
2 GROUP BY ROLLUP(DEPTNO,JOB);
Here,
leadng column is DEPTNO

SQL> SELECT JOB,DEPTNO,COUNT(*) FROM EMP


2 GROUP BY ROLLUP(JOB,DEPTNO);
Here,
leading column is JOB

syntax for cube:


===============
select <column name1>,<column name2>,......,<grouping function name1>,..........
from <table name> group by cube(<column name1>,<column name2>,......);
|
all columns are operational columns

Ex.on Cube with a single column:


=============================
SQL> SELECT DEPTNO,COUNT(*) FROM EMP
2 GROUP BY CUBE(DEPTNO)ORDER BY DEPTNO;

Ex.on Cube with multiple columns:


=============================
SQL> SELECT DEPTNO,JOB,COUNT(*) FROM EMP
2 GROUP BY CUBE(DEPTNO,JOB) ORDER BY DEPTNO,JOB;

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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy