Pre-requisites: JOIN, UNION
JOIN in SQL is used to combine data from many tables based on a matched condition between them. The data combined using the JOIN statement results in new columns. Consider the two tables:
Boys
Girls
Example:
sql> SELECT Boys.Name, Boys.Age, Girls.Address,
FROM Boys INNER JOIN Girls
ON Boys.Rollno = Girls.Rollno;
The resultant table is:
| Name |
Age |
Address |
| Ritik |
15 |
Delhi |
| Prakhar |
17 |
Bhopal |
| Sanjay |
16 |
Goa |
UNION in SQL is used to combine the result set of two or more SELECT statements. The data combined using the UNION statement is into results into new distinct rows.
Example:
sql> SELECT Name FROM Boys
WHERE Rollno < 16
UNION
SELECT Name FROM Girls
WHERE Rollno > 9
Output:
| Name |
| Ram |
| Jayant |
| Rimi |
| Seema |
| Mona |
Difference between JOIN and UNION in SQL
| JOIN |
UNION |
|
JOIN combines data from many tables based on a matched condition between them
|
SQL combines the result set of two or more SELECT statements.
|
|
It combines data into new columns.
|
It combines data into new rows
|
|
The number of columns selected from each table may not be the same.
|
The number of columns selected from each table should be the same.
|
|
Datatypes of corresponding columns selected from each table can be different.
|
The data types of corresponding columns selected from each table should be the same.
|
|
It may not return distinct columns.
|
It returns distinct rows.
|
Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing
Data Science & AI as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
15 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...