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

PostgreSQL - INTERSECT Operator

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;

Example:

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.

Notes:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  1. How to use INTERSECT operator in PostgreSQL:

    • The INTERSECT operator is used to retrieve the common records between two queries.
    SELECT column_name FROM table1
    INTERSECT
    SELECT column_name FROM table2;
    
  2. PostgreSQL INTERSECT operator example:

    • A basic example of using INTERSECT to find common values.
    SELECT name FROM employees
    INTERSECT
    SELECT name FROM managers;
    
  3. Combining INTERSECT with other set operators in PostgreSQL:

    • Combine 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;
    
  4. INTERSECT vs. INNER JOIN in PostgreSQL:

    • Compare using 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;
    
  5. Handling NULL values with INTERSECT in PostgreSQL:

    • Handle NULL values using the 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;
    
  6. Using INTERSECT with multiple columns in PostgreSQL:

    • Extend INTERSECT to work with multiple columns.
    SELECT column1, column2 FROM table1
    INTERSECT
    SELECT column1, column2 FROM table2;
    
  7. INTERSECT and ORDER BY in PostgreSQL:

    • Order the result of INTERSECT using ORDER BY.
    SELECT column_name FROM table1
    INTERSECT
    SELECT column_name FROM table2
    ORDER BY column_name;
    
  8. INTERSECT with subqueries in PostgreSQL:

    • Use subqueries with INTERSECT for more dynamic intersections.
    SELECT column_name FROM table1
    INTERSECT
    (SELECT column_name FROM table2 WHERE condition);
    
  9. Using INTERSECT to find common values in multiple tables in PostgreSQL:

    • Find common values among multiple tables using INTERSECT.
    SELECT column_name FROM table1
    INTERSECT
    SELECT column_name FROM table2
    INTERSECT
    SELECT column_name FROM table3;
    
  10. INTERSECT with aggregate functions in PostgreSQL:

    • Apply aggregate functions with INTERSECT for aggregated common values.
    SELECT AVG(column_name) FROM table1
    INTERSECT
    SELECT AVG(column_name) FROM table2;
    
  11. INTERSECT vs. DISTINCT in PostgreSQL:

    • Understand the difference between 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);
    
  12. Nested INTERSECT in PostgreSQL:

    • Nest INTERSECT operators for more complex set intersections.
    SELECT column_name FROM table1
    INTERSECT
    (SELECT column_name FROM table2
    INTERSECT
    SELECT column_name FROM table3);
    
  13. INTERSECT and UNION ALL in PostgreSQL:

    • Use both 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;
    
  14. INTERSECT and EXCEPT operators in PostgreSQL:

    • Combine 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;
    
  15. Using INTERSECT for set comparison in PostgreSQL:

    • Compare sets of data using INTERSECT.
    SELECT column_name FROM set1
    INTERSECT
    SELECT column_name FROM set2;
    
  16. INTERSECT with JOIN in PostgreSQL:

    • Combine 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;