SQL Video - Union, Intersect, Minus


  SQL > Video Tutorial > Set Operations - Union, Intersect, Minus

This video discusses how to combine two sets of results together in SQL. The following SQL keywords are covered: UNION, UNION ALL, INTERSECT, MINUS, and EXCEPT.



Embed this video using the code below



Video Transcription

In this tutorial, we look into how to combine two sets of results in SQL. There are 3 ways to do this. The first one is similar to an "AND" operation, whereby only rows that are common in both sets are selected. The second one is similar to an "OR" operation, where all rows that appear in either set are selected. The third one is taking only the rows in Set 1 that do not appear in Set 2.

Each of the three ways have corresponding commands in SQL. The first one is achieved via the "INTERSECT" command. The second is achieved via the "UNION" or "UNION ALL" command. The third one is achieved via the "MINUS" or "EXCEPT" command. The difference between UNION and UNION ALL is that UNION only shows distinct values, and UNION ALL shows all rows. So, if a value appears 4 times in Set 1 and Set 2 combined, the UNION command would return that value once, while the UNION ALL command would return that value 4 times. The MINUS and EXCEPT commands are the same. Some databases use MINUS while other databases use EXCEPT.

Let's take a look at an example and see how they are different. Assuming Set 1 has 4 rows: John, Larry, Mary, and Sophie, and Set 2 has 3 rows: John, Angela, and Linda. When we use the INTERSECT command, the result is John. When we use the UNION command, the result is John, Larry, Mary, Sophie, Angela, and Linda. When we use the UNION ALL command, the result is John, Larry, Mary, Sophie, John, Angela, and Linda. When we use the MINUS or EXCEPT command, the result is Larry, Mary, and Sophie.




Copyright © 2013 1keydata.com   All Rights Reserved.     Privacy Policy


SQL SELECT
SQL DISTINCT
SQL WHERE
SQL AND OR
SQL IN
SQL BETWEEN
SQL Wildcard
SQL LIKE
SQL ORDER BY
SQL Functions
SQL Average
SQL COUNT
SQL MAX
SQL MIN
SQL SUM
SQL GROUP BY
SQL HAVING
SQL ALIAS
SQL AS
SQL JOIN
SQL INNER JOIN
SQL OUTER JOIN
SQL LEFT OUTER JOIN
SQL CROSS JOIN
SQL SELECT UNIQUE
SQL ROUND
SQL CAST
SQL CONVERT
SQL CONCATENATE
SQL SUBSTRING
SQL INSTR
SQL TRIM
SQL LENGTH
SQL REPLACE
SQL DATEADD
SQL DATEDIFF
SQL DATEPART
SQL GETDATE
SQL SYSDATE

SQL CREATE TABLE
SQL Data Types
SQL CONSTRAINT
SQL NOT NULL
SQL DEFAULT
SQL UNIQUE
SQL CHECK
PRIMARY KEY
FOREIGN KEY
SQL View
SQL CREATE VIEW
SQL Index
SQL CREATE INDEX
SQL ALTER TABLE
SQL DROP TABLE
SQL TRUNCATE TABLE
SQL USE
SQL CREATE DATABASE
SQL DROP DATABASE
SQL INSERT INTO
SQL INSERT INTO SELECT
SQL UPDATE
SQL DELETE FROM

SQL Video Tutorial
SQL Jobs

Site Map
Resources





View this site in:
Traditional Chinese Version SQL 語法教學
Italian Version Tutorial de SQL
German Version SQL-Tutorial
French Version Tutoriel SQL
Spanish Version Tutorial de SQL
Dutch Version SQL Tutorial
Portuguese Version Tutorial de SQL
Japanese Version SQL 入門
Korean Version SQL 자습서
Simplified Chinese Version SQL语句教程