Sql Intersect And Except Complete Guide

 Last Update:2025-06-23T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    6 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of SQL INTERSECT and EXCEPT

SQL INTERSECT and EXCEPT: Detailed Explanation and Important Information

Introduction

SQL INTERSECT

INTERSECT is used to return records that exist in both SELECT statements.

Syntax:

SELECT column1, column2, ...
FROM table1
INTERSECT
SELECT column1, column2, ...
FROM table2;

Key Points:

  1. Common Rows: Only the rows which are common between the result sets of the two SELECT statements are returned.
  2. Uniqueness: Duplicate rows in the result set are automatically eliminated. If you want to include duplicates, you can use INTERSECT ALL (though not all SQL databases support this).
  3. Matching Columns: The number and order of columns must match between the two SELECT statements, and the data types must be compatible.

Example:

SELECT employee_id, department_id
FROM employees
INTERSECT
SELECT employee_id, department_id
FROM previous_employees;

In this example, the query will return all employee_id and department_id combinations that are present in both the employees and previous_employees tables.

SQL EXCEPT (OR MINUS)

EXCEPT (also known as MINUS in some SQL dialects like Oracle) is used to return records that exist in the first SELECT statement but do not exist in the second SELECT statement.

Syntax:

SELECT column1, column2, ...
FROM table1
EXCEPT
SELECT column1, column2, ...
FROM table2;

Key Points:

  1. Unique Rows in First Result Set: Only the rows that are unique to the first SELECT statement are returned.
  2. Uniqueness: Similar to INTERSECT, duplicate rows in the result set are eliminated.
  3. Column Matching: The same rules apply for column matching as with INTERSECT.

Example:

SELECT customer_id
FROM orders
EXCEPT
SELECT customer_id
FROM returned_orders;

In this example, the query will return all customer_id values from the orders table that do not have corresponding entries in the returned_orders table.

Important Differences

  1. INTERSECT vs EXCEPT:

    • INTERSECT: Used to find common rows.
    • EXCEPT: Used to find rows that are unique to the first SELECT statement.
  2. Performance Considerations:

    • Both INTERSECT and EXCEPT involve sorting and comparing data, which can be resource-intensive on large datasets. Indexing and data normalization can help mitigate performance issues.
  3. Data Type Compatibility:

    • Ensure that the data types of corresponding columns in both SELECT statements are compatible to avoid unexpected results or errors.
  4. Set Operators vs Joins:

    • While set operators can be powerful, complex queries might be more efficiently handled with JOINs or subqueries.

Use Cases

  1. Finding Overlaps:

    • Determine what data is shared across different tables.
  2. Identifying Unique Data:

    • Determine what data is unique to a specific table or dataset.
  3. Data Auditing:

    • Compare historical data with current data to identify changes.

Conclusion

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement SQL INTERSECT and EXCEPT

SQL INTERSECT

The INTERSECT operator is used to return only the distinct rows that are common between two SELECT statements.

Example Scenario:

Let's consider a simple database for a school that has two tables: students_in_math and students_in_science. We want to find out which students are enrolled in both math and science.

Table Creation and Data Insertion:

-- Create table for students in math
CREATE TABLE students_in_math (
    student_id INT,
    student_name VARCHAR(100)
);

-- Insert data into students_in_math
INSERT INTO students_in_math VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie'),
(4, 'David');

-- Create table for students in science
CREATE TABLE students_in_science (
    student_id INT,
    student_name VARCHAR(100)
);

-- Insert data into students_in_science
INSERT INTO students_in_science VALUES
(2, 'Bob'),
(3, 'Charlie'),
(5, 'Eve'),
(6, 'Frank');

Step-by-Step Process to Find Intersection:

-- Step 1: Select all student IDs and names from students_in_math
SELECT student_id, student_name FROM students_in_math;

-- Step 2: Select all student IDs and names from students_in_science
SELECT student_id, student_name FROM students_in_science;

-- Step 3: Use INTERSECT to find common rows between the two SELECT statements
SELECT student_id, student_name FROM students_in_math
INTERSECT
SELECT student_id, student_name FROM students_in_science;

Expected Result:

student_id | student_name
-----------|-------------
2          | Bob
3          | Charlie

SQL EXCEPT

The EXCEPT operator is used to return distinct rows from the first SELECT statement that are not present in the second SELECT statement.

Example Scenario:

Using the same tables as above, let's find out which students are enrolled in math but not in science.

Step-by-Step Process to Find Difference:

Top 10 Interview Questions & Answers on SQL INTERSECT and EXCEPT

1. What does the SQL INTERSECT operator do?

Answer: The INTERSECT operator returns only the rows that are common between the result sets of two or more SELECT statements. It’s useful when you need to compare two sets and extract records that appear in both.

-- Example of INTERSECT
SELECT column1, column2
FROM TableA
INTERSECT
SELECT column1, column2
FROM TableB;

2. How does SQL INTERSECT handle duplicates?

Answer: INTERSECT removes duplicate rows from the output. If a row appears more than once in the result sets being compared, it will only be included once in the final result.

Note: Some databases like SQL Server provide an INTERSECT ALL version which allows duplicates.

3. What is the difference between INTERSECT and INNER JOIN?

Answer: INTERSECT finds common rows across two result sets, whereas INNER JOIN retrieves rows from one table where there are matching values in another table.

  • INTERSECT: Compares rows based on equality across all selected columns.
  • INNER JOIN: Joins rows from multiple tables based on a condition, typically using specific keys.
-- Example of INTERSECT vs INNER JOIN
SELECT emp_id FROM Employees_A WHERE dept = 'Sales';
INTERSECT
SELECT emp_id FROM Employees_B WHERE dept = 'Sales';

-- Equivalent INNER JOIN query
SELECT A.emp_id 
FROM Employees_A AS A
JOIN Employees_B AS B ON A.emp_id = B.emp_id
WHERE A.dept = 'Sales' AND B.dept = 'Sales';

4. When should I use SQL EXCEPT?

Answer: Use EXCEPT (or its synonym MINUS in Oracle) when you want to return all rows from the left-hand query (SELECT statement before EXCEPT) that do not exist in the right-hand query (SELECT statement after EXCEPT). Ideal for identifying differences between two datasets.

-- Example of EXCEPT
SELECT emp_id FROM Employees_A
EXCEPT
SELECT emp_id FROM Employees_B;

5. Can EXCEPT handle duplicates? If yes, how?

Answer: By default, EXCEPT removes duplicate rows from its result. However, some databases such as PostgreSQL offer an EXCEPT ALL operator which preserves duplicates in the final result.

-- Example of EXCEPT vs EXCEPT ALL in PostgreSQL
SELECT column1, column2 FROM TableA
EXCEPT
SELECT column1, column2 FROM TableB; -- Duplicates are removed

SELECT column1, column2 FROM TableA
EXCEPT ALL
SELECT column1, column2 FROM TableB; -- Duplicates are preserved

6. Are INTERSECT and EXCEPT ANSI SQL standards?

Answer: Yes, both INTERSECT and EXCEPT are part of the ANSI SQL standard. This means they're supported by most major relational database management systems (RDBMSs), though syntax might vary slightly for EXCEPT ALL.

7. Can I use INTERSECT and EXCEPT with subqueries?

Answer: Absolutely. You can use INTERSECT and EXCEPT with subqueries, allowing for complex data comparisons nested within larger query structures.

-- Example with SUBQUERIES
(SELECT emp_name FROM Employees WHERE dept_id = 1)
INTERSECT
(SELECT emp_name FROM Employees WHERE location_id = 1);

8. How are the results of INTERSECT and EXCEPT ordered?

Answer: The order of results is usually arbitrary and not guaranteed unless you apply an ORDER BY clause explicitly to the entire INTERSECT or EXCEPT statement.

-- Ordered INTERSECT example
(SELECT emp_name FROM Employees WHERE dept_id = 1)
INTERSECT
(SELECT emp_name FROM Employees WHERE location_id = 1)
ORDER BY emp_name;

9. Why do INTERSECT and EXCEPT require compatible SELECT lists?

Answer: INTERSECT and EXCEPT require the number and types of columns in both SELECT statements to match because these operations compare rows across the datasets. Each corresponding pair of columns must be comparable, meaning their data types must match implicitly or explicitly.

-- Correct usage
SELECT first_name, last_name FROM Employees_A
INTERSECT
SELECT first_name, last_name FROM Employees_B;

-- Error due to mismatched columns
SELECT first_name FROM Employees_A
INTERSECT
SELECT last_name FROM Employees_B; -- Column count/types mismatch

10. Are there alternatives to INTERSECT and EXCEPT?

Answer: Yes, while INTERSECT and EXCEPT are straightforward, similar functionalities can often be achieved through JOIN, NOT EXISTS, or NOT IN clauses.

  • Alternatives for INTERSECT:

    SELECT A.column1, A.column2 
    FROM TableA AS A
    JOIN TableB AS B ON A.column1 = B.column1 AND A.column2 = B.column2;
    
  • Alternatives for EXCEPT:

    SELECT A.column1, A.column2 
    FROM TableA AS A
    LEFT JOIN TableB AS B ON A.column1 = B.column1 AND A.column2 = B.column2
    WHERE B.column1 IS NULL;
    

    Alternatively using NOT IN:

You May Like This Related .NET Topic

Login to post a comment.