|
Custom Search
| |
|
SQL Set Operators - a Visual Guide to UNION, UNION ALL, MIMUS/EXCEPT, INTERSECT Set operators, such as UNION, UNION ALL, EXCEPT or MINUS, INTERSECT, are commonly used in SQL queries. They combine the results of 2 or more queries into a single result set. The pre-requisite to use SET OPERATORS is that the number of columns must be the same between queries, and the data type of each column must be compatible. That is, the datasets must have identical table/column structures. MySQL does not support EXCEPT or MINUS, INTERSECT, only supports UNION and UNION ALL, see practice here, but we'll show you how to achieve the same effect of EXCEPT/MINUS and INTERSECT in MySQL by using JOINs. 1. UNION ALL UNION ALL combines all rows from the results of 2 or more queries, including all duplicates. UNION ALL simply vertically appends the result of the first query to the result of the second query. The only checking the database engine does is to make sure the data structures are the same and data types are compatible.
select * from Table1
2. UNION UNION combines the distinct rows from the results of 2 or more queries. UNION appends 1 dataset to another vertically. In addition, if duplicate rows exist in both datasets, it will only include the distinct rows into the result set.
select * from Table1
3. MINUS/EXCEPT MINUS/EXCEPT only returns distinct rows that only exists in the result of the first query. Oracle supports MINUS and SQL Server uses EXCEPT to do the same thing. Both MINUS and EXCEPT have the same effect. MySQL does not support either, but the same effect can be achieved by using outer join.
select * from Table1
select * from Table1
4. INTERSECT INTERSECT returns distinct rows that exist in the results of both queries. MySQL does not support INTERSECT operator, but the same effect can be achieved by using inner join.
select * from Table1
Other tutorials in this category 1. Using Inner Joins to Combine Data from Two Tables |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |