Skip to content Skip to sidebar Skip to footer

Check If Tables Are Identical Using Sql In Oracle

I was asked this question during an interview for a Junior Oracle Developer position, the interviewer admitted it was a tough one: Write a query/queries to check if the table 'empl

Solution 1:

If the tables have the same columns, you can use this; this will return no rows if the rows in both tables are identical:

(
select*from test_data_01
minus
select*from test_data_02
)
union
(
select*from test_data_02
minus
select*from test_data_01
);

Solution 2:

Identical regarding what? Metadata or the actual table data too?

Anyway, use MINUS.

select * from table_1
MINUS
select * from table_2

So, if the two tables are really identical, i.e. the metadata and the actual data, it would return no rows. Else, it would prove that the data is different.

If, you receive an error, it would mean the metadata itself is different.

Update If the data is not same, and that one of the table has duplicates.

Just select the unique records from one of the table, and simply apply MINUS against the other table.

Solution 3:

One possible solution, which caters for duplicates, is to create a subquery which does a UNION on the two tables, and includes the number of duplicates contained within each table by grouping on all the columns. The outer query can then group on all the columns, including the row count column. If the table match, there should be no rows returned:

createtable employees (name varchar2(100));
createtable employees_hist (name varchar2(100));

insertinto employees values ('Jack Crack');
insertinto employees values ('Jack Crack');
insertinto employees values ('Jill Hill');
insertinto employees_hist values ('Jack Crack');
insertinto employees_hist values ('Jill Hill');
insertinto employees_hist values ('Jill Hill');


with both_tables as
(select name, count(*) as row_count
 from employees
 groupby name
unionallselect name, count(*) as row_count
 from employees_hist
 groupby name)
select name, row_count from both_tables
groupby name, row_count havingcount(*) <>2;

gives you:

Name        Row_count
Jack Crack  1
Jack Crack  2
Jill Hill   1
Jill Hill   2

This tells you that both names appear once in one table and twice in the other, and therefore the tables don't match.

Solution 4:

select name, count(*) n from EMPLOYEES groupby name
minus
select name, count(*) n from EMPLOYEES_HIST groupby name
unionall ( 
select name, count(*) n from EMPLOYEES_HIST groupby name
minus
select name, count(*) n from EMPLOYEES groupby name)

Solution 5:

You could merge the two tables and then subtract one of the tables from the result. If the result of the subtraction is an empty table then you know that the the tables must be the same since merge had no effect (every row and column were effectively the same)

How do I merge two tables with different column number while removing duplicates?

That link provides a good way to merge the two tables without duplicates without knowing what the columns are.

Post a Comment for "Check If Tables Are Identical Using Sql In Oracle"