A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/sql/sql-intersect-clause/ below:

SQL | INTERSECT Clause - GeeksforGeeks

SQL | INTERSECT Clause

Last Updated : 31 Jan, 2025

In SQL, the INTERSECT clause is used to retrieve the common records between two SELECT queries. It returns only the rows that are present in both result sets. This makes INTERSECT an essential clause when we need to find overlapping data between two or more queries.

In this article, we will explain the SQL INTERSECT clause, its syntax, key characteristics, and examples. We will also explore its usage with conditions like BETWEENand LIKE, along with performance considerations and alternatives.

What is SQL INTERSECT?

The INTERSECT clause in SQL is used to combine two SELECT statements but the dataset returned by the INTERSECT statement will be the intersection of the data sets of the two SELECT statements. In simple words, the INTERSECT statement will return only those rows that will be common to both of the SELECT statements.

The INTERSECT operator is a set operation in SQL, similar to UNION and EXCEPT. While UNION combines results from two queries and removes duplicates, INTERSECT returns only the records that exist in both queries, ensuring uniqueness.

  Key Characteristics of SQL INTERSECT:

Syntax:

SELECT column1 , column2 ....
FROM table1
WHERE condition
INTERSECT
SELECT column1 , column2 ....
FROM table2
WHERE condition

Examples of SQL INTERSECT

Let’s consider two tables: the Customers table, which holds customer details, and the Orders table, which contains information about customer purchases. By applying the INTERSECToperator, we can retrieve customers who exist in both tables, meaning those who have made purchases.

Customers Table

Customers Table

Orders Table

Orders Table Example 1: Basic INTERSECT Query

In this example, we retrieve customers who exist in both the Customers and Orders tables. The INTERSECT operator ensures that only those customers who have placed an order appear in the result.

Query:

SELECT CustomerID
FROM Customers
INTERSECT
SELECT CustomerID
FROM Orders;

Output:

Explanation:

Example 2: Using INTERSECT with BETWEEN Operator

In this example, we apply the INTERSECT operator along with the BETWEEN condition to filter records based on a specified range. The query retrieves customers whose CustomerID falls between 3 and 8 and who have placed an order. The result contains only the common CustomerID values that meet both conditions.

Query:

SELECT CustomerID
FROM Customers
WHERE CustomerID BETWEEN 3 AND 8
INTERSECT
SELECT CustomerID
FROM Orders;

Output:

Explanation:

Example 3: Using INTERSECT with LIKE Operator

In this example, we use the INTERSECT operator along with the LIKE operator to find common customers whose FirstName starts with the letter 'J' in both the Customers and Orders tables.

Query:

SELECT CustomerID
FROM Customers
WHERE FirstName LIKE 'J%'
INTERSECT
SELECT CustomerID
FROM Orders;

Output:

Explanation:

Important Notes About SQL INTERSECT Conclusion

TheINTERSECT clause is a powerful SQL operator that allows users to find overlapping data between queries efficiently. By understanding its syntax and practical use cases, we can efficiently analyze overlapping data in a structured manner. Whether we are filtering customer data, analyzing orders, or handling complex datasets, INTERSECT ensures that you retrieve only the common records between two result sets. Use INTERSECT along with conditions (WHERE, BETWEEN, LIKE) to refine results and extract meaningful insights from our database.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4