Skip to Main Content
  • Questions
  • How to find missing entries from a table

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Anavadya.

Asked: October 31, 2022 - 7:35 am UTC

Last updated: November 01, 2022 - 4:14 pm UTC

Version: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Viewed 10K+ times! This question is

You Asked

Hi,
Is there any way using SQL I can find the missing rows in the table that I tried to return using a "WHERE...IN()"?

Example query:
SELECT * FROM employees WHERE employee_id IN(100,101,102,103,104,105,106,107,108,109,110);


Suppose the above query returned 8 rows. How can I locate the 3 employee IDs that are absent or missing from the database?
I can look at the output and figure it out, but this is challenging when a few missing rows have to be found from 100+ records.
Looking forward to a better solution.

Thanks in advance



and Chris said...

You'll need a data source that contains all the possible values. Then return all those where there NOT EXISTS a match in the target table.

For example:

delete hr.employees
where  employee_id in ( 104, 106 );

with rws as (
  select 99 + level n from dual
  connect by level <= 10
)
  select * from rws
  where  not exists (
    select null
    from   hr.employees
    where  n = employee_id
  );
/* 
         N
----------
       104
       106
*/ 
rollback;

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

NOT IN list

A reader, November 01, 2022 - 7:48 pm UTC

select l.column_value missing_empno
from sys.odcinumberlist(7698,7777,7782) l
left join emp e on l.column_value = e.empno
where e.empno is null;

MISSING_EMPNO
-------------
         7777

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.