+ 1
What's the difference between UNION and JOIN?
7 ответов
+ 6
(1/3) With a join, you're taking the intersection of two tables by linking them together with a common data point, then returning columns from those two tables. With a union statement, you're taking the result of two scripts, which could theoretically be disparate, and smushing the results together into one result.
(2/3) JOIN example:
You have two tables, tblStudent (holds basic info about students incl StudentID and Name) and tbljoinStudentCourse (holds the courses taken by students incl StudentID and Course). These two tables have the column StudentID in common. You could join the tables to pull in student names and course(s) that they're taking:
SELECT Name, Course
FROM tblStudent s
JOIN tbljoinStudentCourse sc ON s.StudentID = sc.StudentID
(3/3) UNION example:
Say that you want to return the names and addresses of everyone from a school, including both students and teachers. You store student info in tblStudent and teacher info in tblTeacher. These two tables don't intersect, but they both store the info that you want for each person. You can write a query to return the names and addresses of the students and another for the names and addresses of teachers, then use UNION to show everything in a single result:
SELECT Name, Address
FROM tblStudent
UNION
SELECT Name, Address
FROM tblTeacher
+ 2
What many SoloLearn users are doing is searching "join vs union" on the Internet and posting that as part of their answer. I am going to respect authors or bloggers response and I am going to provide you two links that is going to take you to the concepts and examples, step-by-step in what is a "join" and what is an "union". At the end you will have your own understanding and conclusion what is a join vs union.
http://www.w3schools.com/sql/sql_join.asp
http://www.w3schools.com/sql/sql_union.asp
0
UNION is condition less combinations, where in JOIN we can define conditions using ON
0
Dude, I will explain you. UNION combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union. By using JOINs, you can retrieve data from two or more tables based on logical relationshipsbetween the tables.
- 1
It, union, removes any duplicates. Whereas join just joins the tables.
- 1
As I understood, Join combines data from more than one tables. UNION connects some statesmans even from one table in one set result
- 2
(1/3) With a join, you're taking the intersection of two tables by linking them together with a common data point, then returning columns from those two tables. With a union statement, you're taking the result of two scripts, which could theoretically be disparate, and smushing the results together into one result.