maria-db
  1. maria-db-intersect-operator

Intersect Operator - (MariaDB Operators)

The INTERSECT operator is a set operator in MariaDB that is used to combine the result sets of two or more SELECT statements to produce a single result set that contains only the common rows between the result sets.

Syntax

The syntax for the INTERSECT operator is as follows:

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

Here, the SELECT statements can select columns from one or more tables, and the INTERSECT operator combines the results of these statements to produce a single result set that contains only the common rows between the result sets.

Example

Here is an example of using the INTERSECT operator to combine the result sets of two SELECT statements:

SELECT customer_id, order_id
FROM orders
WHERE order_date > '2022-01-01'
INTERSECT
SELECT customer_id, order_id
FROM orders
WHERE order_date < '2023-01-01';

Output

Executing the query will return a result set that contains the customer_id and order_id for orders made between January 1, 2022, and December 31, 2022:

customer_id | order_id
------------|---------
123         | 456
789         | 012

Explanation

In the above example, we are using the INTERSECT operator to combine the result sets of two SELECT statements that select orders made between different date ranges. The INTERSECT operator returns only the common rows between the result sets, in this case, the orders that were made between January 1, 2022, and December 31, 2022.

Use

The INTERSECT operator is useful for finding common rows between two or more tables or for combining the results of two or more queries to produce a single result set.

Important Points

  • The INTERSECT operator is a set operator in MariaDB that is used to combine the result sets of two or more SELECT statements to produce a single result set that contains only the common rows between the result sets.
  • The SELECT statements can select columns from one or more tables.
  • The INTERSECT operator returns only the common rows between the result sets.
  • The INTERSECT operator is useful for finding common rows between two or more tables or for combining the results of two or more queries to produce a single result set.

Summary

In summary, the INTERSECT operator is a set operator in MariaDB that is used to combine the result sets of two or more SELECT statements to produce a single result set that contains only the common rows between the result sets. The operator can be used to find common rows between two or more tables or combine the results of queries.

Published on: