MySQL SQLLesson 34Free preview
MySQL UNION
This lesson is part of MySQL · All levels
Summary
The UNION operator combines the results of two or more SELECT queries into one vertical set result, provided that the number of columns and types are aligned. Intuitively, the Union throws out duplicate lines, while the Union ALL keeps them all so that they usually go faster. UNION is very useful in bringing together similar data from several tables or several filter conditions.
Key points
- The UNION composes the query results vertically, not side by side like the JOIN
- The number and order of columns on each SELECT must be equal
- Union erases duplicates; Union ALL maintains them and faster
- To sort the combinations, add ORDER BY at the end of the UNION
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.