SQL Diff query

An efficient query to compare content of tables with same schemas

Using SQL aggregation to compute the difference between two tables

select sum(in_old), sum(in_new),
       <group_by_columns>
from (
  select 1 as in_old, 0 as in_new, 
         <group_by_columns>
  from old_table
  union all
  select 0 as in_old, 1 as in_new,
         <group_by_columns>
  from new_table
)
group by <group_by_columns>
having sum(in_old) <> sum(in_new)
order by <your_choice>;
Written on April 20, 2021, Last update on April 21, 2021
sql diff