> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/database/sql-databases/sql-fundamentals/sql-operators/except.md).

# EXCEPT

## Description

The `EXCEPT` operator in SQL is used to return all rows from the first `SELECT` statement that are not present in the second `SELECT` statement. It effectively subtracts the result set of the second query from the result set of the first query. It is often used to find differences between two sets of data.

{% hint style="info" %}
Oracle supports both EXCEPT and its functionally equivalent counterpart, MINUS.

**Why EXCEPT is Preferred:**

* Wider adoption across different SQL database systems.
* Alignment with the ANSI SQL standard for portability.

**Choosing EXCEPT or EXCEPT ALL:**

* Use `EXCEPT` (default) for distinct results, excluding duplicates.
* Use `EXCEPT ALL` (optional) if we specifically need all rows from the difference, including duplicates present in the first result set.
  {% endhint %}

## Characteristics of the `EXCEPT` Operator

1. **Eliminates Duplicates**: The `EXCEPT` operator automatically removes duplicate rows from the result set.
2. **Column Match**: The number of columns and the data types of the columns in the `SELECT` statements must match.
3. **Order of Queries Matters**: The order of the `SELECT` statements is important because it determines which rows are subtracted from which.

## Syntax

```sql
SELECT column1, column2, ...
FROM table1
EXCEPT
SELECT column1, column2, ...
FROM table2;
```

## Example

Consider two tables, `employees` and `managers`, with the following data:

**`employees` table:**

| emp\_id | name    | dept\_id |
| ------- | ------- | -------- |
| 1       | Alice   | 101      |
| 2       | Bob     | 102      |
| 3       | Charlie | 103      |
| 4       | Dave    | 104      |

**`managers` table:**

| mgr\_id | name    | dept\_id |
| ------- | ------- | -------- |
| 2       | Bob     | 102      |
| 3       | Charlie | 103      |
| 5       | Eve     | 105      |

To find employees who are not managers, we can use the `EXCEPT` operator as follows:

```sql
SELECT name, dept_id
FROM employees
EXCEPT
SELECT name, dept_id
FROM managers;
```

#### Result:

| name  | dept\_id |
| ----- | -------- |
| Alice | 101      |
| Dave  | 104      |

In this example, the `EXCEPT` clause returns the rows where the `name` and `dept_id` are present in the `employees` table but not in the `managers` table.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/database/sql-databases/sql-fundamentals/sql-operators/except.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
