INTERSECT
combines the result sets of two or more SELECT
statements, retrieving only the common rows between them. Unlike UNION
, which combines all rows and removes duplicates, INTERSECT
focuses on returning rows that appear in all SELECT
statements.
INTERSECT
is as follows:
value1, value2, ... value_n
: The columns you want to retrieve. You can also use SELECT * FROM
to retrieve all columns.table1, table2
: The tables from which you wish to retrieve records.customers_old
and customers_new
, containing customer data for different periods. You want to find the customers who are present in both tables:
INTERSECT
:
INTERSECT ALL
retrieves all common rows between two or more tables, including duplicates.
This means that if a row appears multiple times in any of the SELECT
statements, it will be included in the final result set multiple times.
INTERSECT ALL
is similar to INTERSECT
:
value1, value2, ... value_n
: The columns you wish to retrieve. You can also retrieve all the values using the SELECT * FROM
query.table1, table2
: The tables from which you want to retrieve records.SELECT
queries must be compatible.INTERSECT ALL
: