compare3

Published: (December 2, 2025 at 04:41 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

SQL Query to Compare Tables Across Schemas

SELECT 'ONLY_IN_US_1' AS location, t1.table_name
FROM dba_tables t1
LEFT JOIN dba_tables t2
       ON t1.table_name = t2.table_name
      AND t2.owner = 'GLOBAL_COMET_US_2'
WHERE t1.owner = 'GLOBAL_COMET_US_1'
  AND t2.table_name IS NULL

UNION ALL

SELECT 'ONLY_IN_US_2' AS location, t2.table_name
FROM dba_tables t2
LEFT JOIN dba_tables t1
       ON t2.table_name = t1.table_name
      AND t1.owner = 'GLOBAL_COMET_US_1'
WHERE t2.owner = 'GLOBAL_COMET_US_2'
  AND t1.table_name IS NULL

ORDER BY location, table_name;
Back to Blog

Related posts

Read more »

compare2

sql SELECT CASE WHEN t1.table_name IS NOT NULL AND t2.table_name IS NULL THEN 'ONLY_IN_US_1' WHEN t2.table_name IS NOT NULL AND...

step2

!Query Filterhttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2...

compare5

SQL Script sql -- Declare variables for schema UIDs run once DECLARE @uid1 int, @uid2 int SELECT @uid1 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_1' SEL...

compare4

sql -- First get the user IDs once optional, for readability DECLARE @uid1 int, @uid2 int SELECT @uid1 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_1' SELE...