How to use NOT IN Clause In SQL

A subquery is a query that uses the result of another query to get the final result. There are multiple ways through which we can use subquery to get the our output. In the below query, we first select the name of professors that are present in the hod table and then use NOT IN clause to exclude them in the outer query.

SELECT * FROM professor
WHERE name NOT IN 
(
  SELECT name FROM hod
);

Output:

Output

Explanation: This above query selects all records from the “professor” table where the name does not exist in the “hod” table.

How to Select Rows that Don’t Exist in Other Table in PostgreSQL?

In PostgreSQL, there are times when we need to find records in one table that do not exist in another table. This can be useful for various data manipulation tasks and ensuring data integrity.

In this article, we will explore different approaches along with examples to achieve this using PostgreSQL. By the end, you will have a clear understanding of how to efficiently select rows that don’t exist in another table which help to improve your data management skills in PostgreSQL.

Similar Reads

How to Select Rows That Don’t Exist in Other Table?

When working with databases, it is common to encounter situations where we need to compare data across tables and find records that are missing in one table but present in another....

1. Using NOT IN Clause

A subquery is a query that uses the result of another query to get the final result. There are multiple ways through which we can use subquery to get the our output. In the below query, we first select the name of professors that are present in the hod table and then use NOT IN clause to exclude them in the outer query....

2. Using NOT EXISTS Clause

In the following query, we first select only those records in the inner query that are present in both the tables by comparing the name in the WHERE clause. Later we discard these records from the professor table by utilising the NOT EXISTS clause....

3. Using LEFT OUTER JOIN and IS NULL

The left join returns all the records in the left table whether they are matched or not. Using this, we can filter out the records for which the fields from the right table are NULL....

4. Using EXCEPT Operator

The EXCEPT operator is used to retrieve distinct records from one table which are not present in the other table. We can make use of except operator to retrieve the rows which are not present in the other table. The following query does the same:...

Conclusion

Overall, the PostgreSQL offers several efficient methods to select rows that don’t exist in another table, which can be important for data management and ensuring data integrity. By using the NOT IN clause, NOT EXISTS clause, LEFT OUTER JOIN and IS NULL, and the EXCEPT operator, you can effectively compare data across tables and identify missing records. These methods provide valuable tools for database administrators and developers working with PostgreSQL, allowing them to manipulate data efficiently and maintain the integrity of their databases....