Where Clause in SQL with Excellent Example
Where Clause in SQL with Excellent Example
Table 1:- XX_EMP A
select * from XX_EMP A
EMPNO
|
ENAME
|
JOB
|
MGR
|
HIREDATE
|
SAL
|
COMM
|
DEPTNO
|
7369
|
Sunil
|
CLERK
|
7902
|
17-Dec-80
|
800
|
|
20
|
7499
|
Anil
|
SALESMAN
|
7698
|
20-Feb-81
|
1600
|
300
|
30
|
7521
|
Sanjay
|
SALESMAN
|
7698
|
22-Feb-81
|
1250
|
500
|
30
|
7566
|
Jai Prakas
|
MANAGER
|
7839
|
02-Apr-81
|
2975
|
|
20
|
7654
|
Manoj
|
SALESMAN
|
7698
|
28-Sep-81
|
1250
|
1400
|
30
|
7698
|
Bal Kishan
|
MANAGER
|
7839
|
01-May-81
|
2850
|
|
30
|
7782
|
Anmol
|
MANAGER
|
7839
|
09-Jun-81
|
2450
|
|
10
|
7788
|
Sushil
|
ANALYST
|
7566
|
09-Dec-82
|
3000
|
|
20
|
7839
|
Kishan
|
PRESIDENT
|
|
17-Nov-81
|
5000
|
|
10
|
7844
|
Tarun
|
SALESMAN
|
7698
|
08-Sep-81
|
1500
|
0
|
30
|
7876
|
Amit
|
CLERK
|
7788
|
12-Jan-83
|
1100
|
|
20
|
7900
|
Jai
|
CLERK
|
7698
|
03-Dec-81
|
950
|
|
30
|
7902
|
Faiz
|
ANALYST
|
7566
|
03-Dec-81
|
3000
|
|
20
|
7934
|
Vinod
|
CLERK
|
7782
|
23-Jan-82
|
1300
|
|
10
|
Table 2:- XX_DEPT B
select * from XX_DEPT B
DEPTNO
|
DNAME
|
LOC
|
10
|
ACCOUNTING
|
NEW YORK
|
20
|
RESEARCH
|
DALLAS
|
30
|
SALES
|
CHICAGO
|
40
|
OPERATIONS
|
BOSTON
|
--====================================================
-- WHERE Clause
in SQL 1. put any Condition
--====================================================
select * from XX_EMP A
where JOB = 'SALESMAN'
select * from XX_EMP A
where UPPER(JOB) = 'SALESMAN'
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and A.DEPTNO
= 20
--====================================================
-- How to Use
(LIKE)
--====================================================
select
* from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and LOWER(A.ENAME) LIKE '%a%'
.....a.....
select
* from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and LOWER(A.ENAME) LIKE '%a_'
--====================================================
-- How to Use
(NOT LIKE)
--====================================================
select
* from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and LOWER(A.ENAME) NOT LIKE '%a%'
select
* from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and LOWER(A.ENAME) NOT LIKE '%a_'
--====================================================
-- How to Use
(IN)
--====================================================
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and
ENAME IN ('Vinod', 'Anmol', 'Faiz' )
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and A.DEPTNO
IN ('30')
--====================================================
-- How to Use
(NOT IN)
--====================================================
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and
ENAME NOT IN ('Vinod', 'Anmol', 'Faiz' )
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and A.DEPTNO
NOT IN ('30')
--====================================================
-- How to Use
(NULL)
--====================================================
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
and MGR IS NULL
--====================================================
-- How to Use
(NOT IN) ( IS NULL )
--====================================================
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
AND MGR IS NOT NULL
--====================================================
-- How to Use
(BETWEEN)
--====================================================
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
AND A.DEPTNO
BETWEEN '20' AND '35'
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
AND
HIREDATE BETWEEN '20-Feb-81' AND '08-Sep-81'
--====================================================
-- How to Use
(Greater Then ) > ,
< , = , <>
--====================================================
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
AND SAL = 1100
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
--AND SAL
<> 1100
AND SAL
!= 1100
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
AND SAL > 1100
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
AND SAL < 1100
--====================================================
-- How to Use
(OR / AND )
--====================================================
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
AND JOB = 'SALESMAN'
AND SAL = 1250
select * from XX_EMP A, XX_DEPT B
where A.DEPTNO
= B.DEPTNO
AND (JOB = 'SALESMAN' OR SAL = 1250 )
Comments
Post a Comment