Home  >  Article  >  Database  >  What does minus mean in sql

What does minus mean in sql

下次还敢
下次还敢Original
2024-05-07 06:06:161223browse

The MINUS operator subtracts one result set (table1) from another result set (table2), returning rows that appear in table1 but not in table2. It works as follows: Each row of the two result sets is compared. Adds rows that exist in table1 but not in table2 to the new result set.

What does minus mean in sql

The meaning of MINUS in SQL

The MINUS operator is used in SQL to subtract the value of one result set from another. A result set. It returns a new result set containing rows that appear in the first result set but not in the second result set.

Usage:

<code>SELECT * FROM table1 MINUS SELECT * FROM table2;</code>

How it works:

The MINUS operator works as follows:

  1. Compare each row of the two result sets.
  2. If a row exists in the first result set but not in the second result set, the row will be added to the new result set.

Example:

Suppose we have two tables:

  • Table 1: contains students ID and Name
  • Table 2: Contains IDs of students who attended the class

We want to find students who did not attend the class. We can use the MINUS operator to do this:

<code>SELECT student_id, name
FROM table1
MINUS
SELECT student_id
FROM table2;</code>

This query will return a result set containing those students who appear in Table 1 but not in Table 2.

Note:

  • Duplicate rows may appear in the result set of the MINUS operator.
  • The MINUS operator is similar to the EXCEPT operator, but the EXCEPT operator only returns unique rows.

The above is the detailed content of What does minus mean in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn