INTERSECT and EXCEPT of the new features of MySQL 8.0

In recent years, MySQL has continuously worked to be compatible with the SQL standard. For example, window functions, common table expressions, check constraints, etc. in MySQL 8.0.

The latest release of MySQL 8.0.31 continues to enhance the SQL statement, providing two set operators that have been missing for a long time: INTERSECT and EXCEPT.

Intersection operator (INTERSECT)

The INTERSECT operator is used to return the common part of the two query results, that is, the data that appears in both the first query result and the second query result, and the final result is deduplicated.

We create an annual excellent employee table (excellent_emp) to demonstrate set operations:

CREATE TABLE excellent_emp(
    year   INT NOT NULL, 
    emp_id INTEGER NOT NULL,
    CONSTRAINT pk_excellent_emp PRIMARY KEY (YEAR, emp_id)
);

INSERT INTO excellent_emp VALUES (2018, 9);
INSERT INTO excellent_emp VALUES (2018, 11);
INSERT INTO excellent_emp VALUES (2019, 9);
INSERT INTO excellent_emp VALUES (2019, 20);

The following example finds employee IDs that were top performers in both 2018 and 2019:

SELECT emp_id
  FROM excellent_emp
 WHERE year = 2018
INTERSECT
SELECT emp_id
  FROM excellent_emp
 WHERE year = 2019;

emp_id|
------|
     9|

Among them, INTERSECT represents the intersection operation. The first query statement returns 9 and 11, the second query statement returns 9 and 20, and the final result returns the common 9. The field name returned by the aggregate operation is determined by the first statement, where both statements have the same field name (emp_id).

For MySQL 5.7 and earlier, the above example can be rewritten as an equivalent join query:

SELECT t1.emp_id
  FROM excellent_emp t1
  JOIN excellent_emp t2
    ON (t1.emp_id = t2.emp_id
        AND t1.year = 2018
        AND t2.year = 2019);

emp_id|
------|
     9|

Intersection operations can be rewritten as equivalent equivalent inner join queries.

The full syntax of the INTERSECT operator is as follows:

SELECT ...
INTERSECT [ALL | DISTINCT] SELECT ...
[INTERSECT [ALL | DISTINCT] SELECT ...]

The ALL option means to keep the duplicate records in the query result set, and the DISTINCT option means to remove the duplicate records in the query result set. The default option is DISTINCT.

In addition, the INTERSECT operator has a higher priority than UNION and EXCEPT, so the following two writing methods are equivalent:

TABLE r EXCEPT TABLE s INTERSECT TABLE t;

TABLE r EXCEPT (TABLE s INTERSECT TABLE t);

For more information about the INTERSECT operator, you can refer to the official documentation
.

Difference operator (EXCEPT)

The EXCEPT operator is used to return records that appear in the first query result but not in the second query result, and the final result is deduplicated.

Only 3 in the result of the first query does not appear in the result of the second query, so only 3 is retained in the result of the difference operation.

The following statement finds employees who were rated excellent in 2019 but not excellent in 2018:

SELECT emp_id
  FROM excellent_emp
 WHERE year = 2019
EXCEPT
SELECT emp_id
  FROM excellent_emp
 WHERE year = 2018;

emp_id|
------|
    20|

The query results show that only employee No. 20 is a newly promoted excellent employee in 2019.

For MySQL 5.7 and earlier versions, the above example can be rewritten as an equivalent left outer join query:

SELECT t1.emp_id
  FROM excellent_emp t1
  LEFT JOIN excellent_emp t2 ON (t1.emp_id = t2.emp_id AND t2.year = 2018)
 WHERE t1.year = 2019
   AND t2.emp_id IS NULL;

emp_id|
------|
    20|

Among them, the left outer join returns all the excellent employees; then use the WHERE condition to find out the excellent employees in 2019 but not in 2018.

The full syntax of the EXCEPT operator is as follows:

SELECT ...
EXCEPT [ALL | DISTINCT] SELECT ...
[EXCEPT [ALL | DISTINCT] SELECT ...]

The ALL option means to keep the duplicate records in the query result set, and the DISTINCT option means to remove the duplicate records in the query result set. The default option is DISTINCT.

For more information about the INTERSECT operator, you can refer to the official documentation
.

 MySQL 8.0 Video Tutorial Portal: The 2023 new version of Mysql database operation and maintenance management DBA project foundation + actual combat, from installation to addition, deletion, checking and modifying one-stop

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/131845460