What is the difference among UNION , MINUS and INTERSECT ?
What is the difference among UNION , MINUS and INTERSECT ?
These set operators combine the results of two queries and treat rows as sets. Both queries need the same number of columns with compatible types.
| Operator | Returns | Notes |
|---|---|---|
UNION |
rows in A or B | removes duplicates (UNION ALL keeps them) |
INTERSECT |
rows in both A and B | removes duplicates |
EXCEPT / MINUS |
rows in A not in B | removes duplicates; order matters |
EXCEPT is the ANSI name (PostgreSQL, SQL Server, SQLite, and MySQL 8.0.31+). MINUS is the Oracle name (also MariaDB as an alias). MySQL did not support INTERSECT/EXCEPT before 8.0.31.
-- A = {1, 2, 3}, B = {2, 3, 4}
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.