Union and Union All Concept in SQL


Difference between Union and Union All 


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+


--====================================================
UNION
--====================================================

select * from XX_STUDENT_TL
where RESULT = 'P'
UNION
select * from XX_STUDENT_TL
WHERE GRADE = 'A+'

STUDENT_NAME
MARKS
PHYSICAL_MARKS
RESULT
GRADE
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+


--====================================================
UNION ALL
--====================================================

select * from XX_STUDENT_TL
where RESULT = 'P'
UNION ALL
select * from XX_STUDENT_TL
WHERE GRADE = 'A+'
order by 1

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

--====================================================
Mandatory Rules for Union and Union ALL
--====================================================

select STUDENT_NAME,RESULT, MARKS,GRADE  from XX_STUDENT_TL
where RESULT = 'P'
UNION
select GRADE ,GRADE,MARKS,RESULT   from XX_STUDENT_TL
WHERE GRADE = 'A+'

STUDENT_NAME
RESULT
MARKS
GRADE
A+
A+
20
F
A+
A+
50
P
A+
A+
80
P
A+
A+
90
P
Jai Prakash
P
99
A++
Pinky
P
90
A+
Ravi
P
50
A+
Sanjay
P
80
A+

select STUDENT_NAME,RESULT, MARKS,GRADE  from XX_STUDENT_TL
where RESULT = 'P'
UNION
select  STUDENT_NAME,RESULT, MARKS,GRADE from XX_STUDENT_TL
WHERE GRADE = 'A+'

STUDENT_NAME
RESULT
MARKS
GRADE
Jai Prakash
P
99
A++
Manoj
F
20
A+
Pinky
P
90
A+
Ravi
P
50
A+
Sanjay
P
80
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