How to use UNION in SQL?

Right now, are going to utilize UNION provision/administrator which join aftereffects of two or might be in excess of two SELECT articulations.

This to some degree advance in SQL on the grounds that at whatever point we have to combine the consequence of two diverse SQL SELECT questions results we utilize UNION keyword.

Presently in the example giving underneath, we got an example clarifying only this idea of association. Suppose we have two distinctive SQL questions:

	Select * from A
	Select * from B

So at whatever point we this sort of condition we some way or another need to have a solitary outcome including the consequences of both of these inquiries. Truly this can likewise be don’t with a solitary SELECT inquiry like:

	SELECT * from A, B

In any case, imagine a scenario in which our SQL inquiry is exceptionally mind-boggling and we would prefer not to make it increasingly intricate. At when we have two complex questions and we need to join them, we should simply to utilize the association catchphrase like this:

 SELECT * from A UNION select * from B

Dummy Data:

How to use UNION in SQL?

Syntax 1: (UNION):

SELECT c1,c2 FROM table1,table2  [WHERE condition]  UNION
SELECT c1,c2 FROM table1,table2  [WHERE condition]

Syntax 2: (UNION ALL):

SELECT c1,c2 FROM table1, table2 [WHERE condition]  UNION ALL
SELECT c1,c2 FROM table1, table2 [WHERE condition]

Example 1:

SELECT  ID, NAME, SALARY, COMPANY_NAME FROM 'JustTechReview'.'employee' 
LEFT JOIN 'JustTechReview'.'company' ON 
'JustTechReview'.'employee'.ID = 'JustTechReview'.'company'.COMPANY_ID
UNION
SELECT  ID, NAME, SALARY, COMPANY_NAME FROM 'JustTechReview'.'employee' 
RIGHT JOIN 'JustTechReview'.'company' ON 
'JustTechReview'.'employee'.ID = 'JustTechReview'.'company'.COMPANY_ID

Example 2:

SELECT  ID, NAME, SALARY, COMPANY_NAME FROM 'JustTechReview'.'employee' 
LEFT JOIN 'JustTechReview'.'company' ON 
'JustTechReview'.'employee'.ID = 'JustTechReview'.'company'.COMPANY_ID
UNION ALL
SELECT  ID, NAME, SALARY, COMPANY_NAME FROM 'JustTechReview'.'employee' 
RIGHT JOIN 'JustTechReview'.'company' ON 
'JustTechReview'.'employee'.ID = 'JustTechReview'.'company'.COMPANY_ID

Leave a Comment

error: Alert: Content is protected!!