×

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from one table, and the record should not be available in the other table. In that case, SQL has the concept name SQL Except.

To purify the data from more than one table, we used SQL Except. The SQL Except is the same as the minus operator we do in mathematics. SQL Except first merges the two or more than two SELECT statements in query and returns the data from the first SELECT statement. We aren't available in another SELECT statement result.

SQL EXCEPT Rules

We should understand all the rules and regulations before using the EXCEPT query in SQL:

  • The number and order of columns in the given table must be the same in the entire SELECT query.
  • The column's data type must be the same or compatible.

The syntax for SQL EXCEPT

SELECT * FROM table1 EXCEPT SELECT * FROM table2;

Table1 and Table2 will be the name of tables.

Example:           

Assume we have two tables with the same number of columns and the order of columns.

  • Table 1: T1, Number of Columns: 3, Data: A, B, C, D
  • Table 2: T2, Number of Columns: 3, Data: B, D, F, G

Whenever we execute the EXCEPT query on these two tables, we will get A and C because these two data are not present in table T2, B and D are common in both tables, which discard.

Let's understand SQL EXCEPT concept with examples. Consider the following tables along with the given records.

Table1: Emp

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWHERE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4

Table2: Employee

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VaibhavSharma65000PUNEORACLE1
1002NikhilVani60000NOIDAORACLE1
1003VaibhaviMishra50000JAIPURFMW2
2001RuchikaJain55500CHANDIGARHORACLE1
2002PrachiSharma65500PUNEFMW2
2003BhaveshJain50000MUMBAITESTING4
3001DeepamJauhari55500PUNEJAVA3
3002ANUJAWHERE50500JAIPURFMW2
3003PranotiShende58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4

Table3: Manager

Manageridmanager_namemanager_department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING

Table4: Manager1

Manageridmanager_namemanager_department
1Ishita AgrawalORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Paul OakipTESTING

Example 1: Suppose we want to join the above two tables Emp and Employee in our SELECT query using EXCEPT operator.

SELECT EMPLOYEEID, CONCAT(FIRST_NAME, LAST_NAME) AS NAME, CITY, DEPARTMENT MANAGER1.MANAGERID, MANAGER1.MANAGER_NAME FROM EMPLOYEE INNER JOIN MANAGER ON EMP.MANAGERID = MANAGER.MANAGERID EXCEPT SELECT EMPLOYEEID, CONCAT(FIRST_NAME, LAST_NAME) AS NAME, CITY, DEPARTMENT, MANAGER1.MANAGERID, MANAGER1.MANAGER_NAME FROM EMPLOYEE INNER JOIN MANAGER1 ON EMPLOYEE.MANAGERID = MANAGER1.MANAGERID;

We are using the INNER JOIN clause between Emp and Employee table where we display Employee Id, Name, City, Department, Manager Id, and Manager Name using EXCEPT operator. The above query will display only those unique values between both tables.

The above query gives the following output:

SQL EXCEPT

If we observe the tables data, there are two common data between both tables Emp table and Employee table, i.e., Employee id 3002 and 4001. Employee id 4001 details display except 3002. Because Employee id 3002 Manager name is the same in both tables Manager and Manager1 but Employee id 4001 Manager names are different in both tables, employee id 4002 details are displayed.

Example 2: Suppose we want to join the above two tables Emp and Employee in our SELECT query using EXCEPT operator and sort the result set by their salary in descending order. We will use the ORDER BY clause to sort the result set in the SQL query.

SELECT EMPLOYEEID, CONCAT(FIRST_NAME, LAST_NAME) AS NAME, CITY, SALARY, MANAGER1.MANAGERID, MANAGER1.MANAGER_NAME FROM EMPLOYEE INNER JOIN MANAGER ON EMP.MANAGERID = MANAGER.MANAGERID EXCEPT SELECT EMPLOYEEID, CONCAT(FIRST_NAME, LAST_NAME) AS NAME, CITY, SALARY, MANAGER1.MANAGERID, MANAGER1.MANAGER_NAME FROM EMPLOYEE INNER JOIN MANAGER1 ON EMPLOYEE.MANAGERID = MANAGER1.MANAGERID ORDER BY SALARY;

The above query shows the following output:

SQL EXCEPT

Example 3: Suppose we want to join the above two tables Emp and Employee in our SELECT query using EXCEPT operator where employee salary greater than 55000 from Emp table and employee city include ‘Pune’, ‘Mumbai’, ‘Jaipur’ from Employee table.

SELECT * FROM EMP WHERE SALARY > 55000 EXCEPT SELECT * FROM EMPLOYEE WHERE CITY IN ('Pune', 'Mumbai', 'Jaipur');   

The above query first SELECT statement fetches all the details of those employees whose salary is greater than 55000 from the Emp table. The second SELECT statement fetches all the details of those employees whose cities include Pune, Mumbai, Jaipur from the Employee table. Then, EXCEPT operator will be executed between the Emp table and Employee table.

This gives the following output:

SQL EXCEPT

Related Topics

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.