In and Exists in SQL

In and Exists in SQL


select * from XX_STUDENT_TL

STUDENT_NAME
MARKS
PHYSICAL_MARKS
RESULT
GRADE
Amit
33
80
F
A
Bharat
20
30
F

Jai Prakash
99
100
P
A++
Manoj
20
98
F
A+
Pinky
90
30
P
A+
Rahul
45
55


Ravi
50
90
P
A+
Sanjay
80
40
P
A+

select * from  XX_RESULT_MASTER_TL

RESULT
MEANING
F
FAIL
P
PASS


select * from  XX_GRADE_MASTER_TL

GRADE
MEANING
A
GOOD
A+
VERY GOOD
A++
EXCELLENT
B
AVERAGE
C
POOR

--====================================================
Example:-  Simple "IN"  Synatx Use
--====================================================


select * from XX_STUDENT_TL
where RESULT in ('P')

STUDENT_NAME
MARKS
PHYSICAL_MARKS
RESULT
GRADE
Jai Prakash
99
100
P
A++
Pinky
90
30
P
A+
Ravi
50
90
P
A+
Sanjay
80
40
P
A+

select * from XX_STUDENT_TL
where RESULT in ('P','F')

STUDENT_NAME
MARKS
PHYSICAL_MARKS
RESULT
GRADE
Amit
33
80
F
A
Bharat
20
30
F

Jai Prakash
99
100
P
A++
Manoj
20
98
F
A+
Pinky
90
30
P
A+
Ravi
50
90
P
A+
Sanjay
80
40
P
A+


--====================================================
Example:- "IN" Use with Table 
--====================================================


select * from XX_STUDENT_TL
where RESULT in (select RESULT from  XX_RESULT_MASTER_TL)

STUDENT_NAME
MARKS
PHYSICAL_MARKS
RESULT
GRADE
Manoj
20
98
F
A+
Bharat
20
30
F

Amit
33
80
F
A
Sanjay
80
40
P
A+
Ravi
50
90
P
A+
Pinky
90
30
P
A+
Jai Prakash
99
100
P
A++


select * from XX_STUDENT_TL
where ( RESULT,GRADE ) in (select RESULT, 'A+' GRADE from  XX_RESULT_MASTER_TL where RESULT = 'P')

STUDENT_NAME
MARKS
PHYSICAL_MARKS
RESULT
GRADE
Pinky
90
30
P
A+
Ravi
50
90
P
A+
Sanjay
80
40
P
A+


--====================================================
Example:- EXISTS
--====================================================

select * from XX_STUDENT_TL

select * from  XX_RESULT_MASTER_TL

select * from  XX_GRADE_MASTER_TL

Example1:-

select * from XX_STUDENT_TL A
where EXISTS (select * from XX_RESULT_MASTER_TL Z where Z.RESULT = A.RESULT )

STUDENT_NAME
MARKS
PHYSICAL_MARKS
RESULT
GRADE
Manoj
20
98
F
A+
Bharat
20
30
F

Amit
33
80
F
A
Sanjay
80
40
P
A+
Ravi
50
90
P
A+
Pinky
90
30
P
A+
Jai Prakash
99
100
P
A++


Example2:-

edit XX_STUDENT_TL

select * from XX_STUDENT_TL A
where EXISTS (select * from XX_GRADE_MASTER_TL Z where Z.GRADE = A.GRADE )

STUDENT_NAME
MARKS
PHYSICAL_MARKS
RESULT
GRADE
Amit
33
80
F
A
Sanjay
80
40
P
A+
Ravi
50
90
P
A+
Pinky
90
30
P
A+
Manoj
20
98
F
A+
Jai Prakash
99
100
P
A++




Comments

Popular posts from this blog

E-Text Report In Fusion | Types of E-Text reports

Supplier API's

How to pass default Parameter in cursor