PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the INTERSECT
operator is used to combine the result sets of two or more SELECT
statements and return only the rows that are common to all result sets. Each SELECT
statement within the INTERSECT
must have the same number of columns, and the corresponding columns must have compatible data types.
The returned result set from an INTERSECT
operation is distinct; that is, duplicate rows are eliminated.
Here's the basic syntax:
SELECT column1, column2, ... FROM table1 WHERE condition1 INTERSECT SELECT column1, column2, ... FROM table2 WHERE condition2;
Let's consider an example with two tables: orders_2022
and orders_2023
, each containing a list of order IDs for the respective years. If you want to find out the order IDs that are common to both years, you would use the INTERSECT
operator as follows:
SELECT order_id FROM orders_2022 INTERSECT SELECT order_id FROM orders_2023;
This would give you a list of order_id
values that exist in both orders_2022
and orders_2023
.
INTERSECT
returns only distinct rows, so even if a value appears multiple times in both result sets, it will appear only once in the final result.
If you want the result set to be sorted, you can add an ORDER BY
clause at the end. However, remember that the ORDER BY
clause will only apply to the entire combined result, not to individual SELECT
statements.
If you're interested in getting all distinct rows from both result sets (including non-matching rows), consider using the UNION
operator. If you want to get rows from the first result set that are not present in the second one, you can use the EXCEPT
operator.
Always ensure that the SELECT
statements used with INTERSECT
return results with matching column data types, as trying to combine incompatible data types can result in errors.
How to use INTERSECT operator in PostgreSQL:
INTERSECT
operator is used to retrieve the common records between two queries.SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2;
PostgreSQL INTERSECT operator example:
INTERSECT
to find common values.SELECT name FROM employees INTERSECT SELECT name FROM managers;
Combining INTERSECT with other set operators in PostgreSQL:
INTERSECT
with other set operators like UNION
and EXCEPT
for more complex queries.SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2 UNION SELECT column_name FROM table3;
INTERSECT vs. INNER JOIN in PostgreSQL:
INTERSECT
for set intersection with INNER JOIN
for common records.-- Using INTERSECT SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2; -- Using INNER JOIN SELECT table1.column_name FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
Handling NULL values with INTERSECT in PostgreSQL:
IS NOT NULL
condition with INTERSECT
.SELECT column_name FROM table1 WHERE column_name IS NOT NULL INTERSECT SELECT column_name FROM table2 WHERE column_name IS NOT NULL;
Using INTERSECT with multiple columns in PostgreSQL:
INTERSECT
to work with multiple columns.SELECT column1, column2 FROM table1 INTERSECT SELECT column1, column2 FROM table2;
INTERSECT and ORDER BY in PostgreSQL:
INTERSECT
using ORDER BY
.SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2 ORDER BY column_name;
INTERSECT with subqueries in PostgreSQL:
INTERSECT
for more dynamic intersections.SELECT column_name FROM table1 INTERSECT (SELECT column_name FROM table2 WHERE condition);
Using INTERSECT to find common values in multiple tables in PostgreSQL:
INTERSECT
.SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2 INTERSECT SELECT column_name FROM table3;
INTERSECT with aggregate functions in PostgreSQL:
INTERSECT
for aggregated common values.SELECT AVG(column_name) FROM table1 INTERSECT SELECT AVG(column_name) FROM table2;
INTERSECT vs. DISTINCT in PostgreSQL:
INTERSECT
and DISTINCT
.SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2; -- Equivalent using DISTINCT SELECT DISTINCT column_name FROM table1 WHERE column_name IN (SELECT column_name FROM table2);
Nested INTERSECT in PostgreSQL:
INTERSECT
operators for more complex set intersections.SELECT column_name FROM table1 INTERSECT (SELECT column_name FROM table2 INTERSECT SELECT column_name FROM table3);
INTERSECT and UNION ALL in PostgreSQL:
INTERSECT
and UNION ALL
in the same query for varied set operations.SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2 UNION ALL SELECT column_name FROM table3;
INTERSECT and EXCEPT operators in PostgreSQL:
INTERSECT
and EXCEPT
for more sophisticated set operations.SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2 EXCEPT SELECT column_name FROM table3;
Using INTERSECT for set comparison in PostgreSQL:
INTERSECT
.SELECT column_name FROM set1 INTERSECT SELECT column_name FROM set2;
INTERSECT with JOIN in PostgreSQL:
INTERSECT
with JOIN
for more intricate set intersections.SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2 JOIN table3 ON table2.column_name = table3.column_name;