How to compare count of two tables in SQL

I want to compare row count of two tables and then return 0 or 1 depending on whether its same or not.

I am thinking of something like this but can't move ahead and need some help.

SELECT 
       CASE WHEN (select count(*) from table1)=(select count(*) from table2)
       THEN 1
       ELSE 0
       END AS RowCountResult
FROM Table1,Table2

I am getting multiple rows instead of a single row with 0 or 1

asked Dec 11, 2014 at 8:33

How to compare count of two tables in SQL

you have to remove :

FROM Table1,Table2

Otherwise it will consider the result of the Case-When for each row of this FROM clause.

answered Dec 11, 2014 at 8:41

How to compare count of two tables in SQL

BanovBanov

2873 silver badges15 bronze badges

0

Or Simply remove the CASE WHEN CLAUSE and write:

SELECT (SELECT count(*) from table1)=(SELECT count(*) from table2) AS RowCountResult;

as a boolean result will be returned.

answered Jan 19, 2018 at 4:33

0

If you are using Oracle db use FROM dual instead of FROM Table1,Table2 Otherwise parser will throw following error: ORA-00923: FROM keyword not found where expected.

answered Nov 25, 2016 at 15:46

Say you have requirement to compare two tables. You have two tables in same database or server that you wish to compare, and check if any changes in the column values or see if any row is missing in either of tables.

Below are some of the methods you can use to compare two tables in SQL.

Compare Two Tables using UNION ALL

UNION allows you to compare data from two similar tables or data sets. It also handles the NULL values to other NULL values which JOIN or WHERE clause doesn’t handle. It allows quickly checking what are the data missing or changed in either table.

Select * from ( 
Select Id_pk, col1, col2...,coln from table1, ‘Old_table’ 
Union all 
Select Id_pk, col1, col2...,coln from table2, 'New_tbale' 
) cmpr order by Id_pk;

The above query returns the all rows from both tables as old and new. You can quickly verify the differences between two tables.

Related reading: Steps to Optimize SQL Query Performance

Compare Two Table using MINUS

You can compare the two similar tables or data sets using MINUS operator. It returns all rows in table 1 that do not exist or changed in the other table.

Select Id_pk, col1, col2...,coln from table1 
MINUS
Select Id_pk, col1, col2...,coln from table2;

You can quickly check how many records are having mismatch between two tables.

The only drawback with using UNION and MINUS is that the tables must have the same number of columns and the data types must match.

Compare Two Table using JOIN

This is the easiest but user has to do some additional work to get the correct result. In this approach you can join the two tables on the primary key of the two tables and use case statement to check whether particular column is matching between two tables.

Select case when A.col1 = B.col1 then ‘Match’ else ‘Mismatch’ end as col1_cmpr, 
case when A.col2 = B.col2 then ‘Match’ else ‘Mismatch’ end as col2_cmpr, ….. 
from table1 A
Join table2 B
on (A.id_pk = B.id_pk) ;

The only drawback of using JOIN is, it cannot compare the NULL values, and you should use the NVL on the column that may have null values in it.

Compare Two Table using NOT EXISTS

The other faster method is to use the NOT EXISTS in WHERE clause of the query. This method is faster and performs well on the large volume of data.

Select id_pk, col1, col2,col,…
From table1 A
Where NOT EXISTS
( select 1 from table2 B
Where A.id_pk = B.id_pk
and A.col1 = B.col1
and A.col2 = B.col2
and…
);

In this approach also you have to use the NVL on the columns which contains NULL in it.

Compare Cells From Two Tables – Cell by Cell Validation

You can use the CASE statement to compare records from both tables and check if the cells are matching from both tables.

For example, following example demonstrates the cell by cell validation.

SELECT CASE
         WHEN Nvl(a.id :: VARCHAR, 'NULL' :: VARCHAR) = Nvl (b.id :: VARCHAR,'NULL' :: VARCHAR) THEN
         'Matched'
         ELSE Nvl(a.id :: VARCHAR, 'NULL' :: VARCHAR)
              ||','
              || Nvl(b.id :: VARCHAR, 'NULL' :: VARCHAR)
       END id,
       CASE
         WHEN Nvl(a.name :: VARCHAR, 'NULL' :: VARCHAR) =
              Nvl (b.name :: VARCHAR,'NULL' :: VARCHAR) THEN
         'Matched'
         ELSE Nvl(a.name :: VARCHAR, 'NULL' :: VARCHAR)
              ||','
              || Nvl (b.name :: VARCHAR, 'NULL' :: VARCHAR)
       END name,
       CASE
         WHEN Nvl(a.addr :: VARCHAR, 'NULL' :: VARCHAR) =
              Nvl (b.addr :: VARCHAR,'NULL' :: VARCHAR) THEN
         'Matched'
         ELSE Nvl(a.addr :: VARCHAR, 'NULL' :: VARCHAR)
              ||','
              || Nvl (b.addr :: VARCHAR, 'NULL' :: VARCHAR)
       END addr
FROM   test_tab1 a
       full outer join test_tab2 b
                    ON Nvl(a.id :: VARCHAR, 'NULL' :: VARCHAR) =
                       Nvl (b.id :: VARCHAR, 'NULL' :: VARCHAR) ;

+---------+----------+------------+
| ID      | NAME     | ADDR       |
|---------+----------+------------|
| Matched | Matched  | Matched    |
| Matched | Matched  | Matched    |
| Matched | Matched  | adr3,adr-  |
| Matched | ddd,dd   | Matched    |
| Matched | eee,NULL | adr5,NULL  |
| NULL,6  | NULL,zzz | NULL,zaddr |
| 7,NULL  | yyy,NULL | yaddr,NULL |
+---------+----------+------------+

You can use LEFT OUTER JOIN or INNER JOIN if you know the table count is matching.

Get Matched and Unmatched Count from Two Tables

You can use full outer join to get matched and unmatched records or count from two tables which has common columns in it.

SELECT
Sum(CASE WHEN t1.file_name IS NOT NULL AND t2.file_n IS NOT NULL THEN 1
ELSE 0 END) AS matched_count,
Sum( CASE WHEN t1.file_name IS NOT NULL AND t2.file_n IS NOT NULL THEN 0
ELSE 1 END) AS un_matched_count
FROM assessment_file_query_mapping AS t1
FULL OUTER JOIN
(
SELECT *
FROM (SELECT DISTINCT regexp_split_to_table(files, e',') AS file_n
FROM job_info) AS a
WHERE length(trim(file_n)) <>0 ) AS t2
ON trim(t1.file_name) = trim(file_n);

We are checking the matched and unmatched file count from two tables. Example uses regular expression to split the comma separated records into table and then join on common file name column.

Related Posts:

  • Netezza Select Random Rows and Example
  • Netezza Pivot  Rows to Column with Example
  • Netezza Split Delimited Fields into Table Records and Examples
  • Netezza Update Join Syntax and Examples
  • Count Records from all Tables in Database and Example
  • SQL and Hive GROUP BY Alternative-Example

How do I count the number of two tables in SQL?

To achieve this for multiple tables, use the UNION ALL. select sum(variableName. aliasName) from ( select count(*) as yourAliasName from yourTableName1 UNION ALL select count(*) as yourAliasName from yourTableName2 ) yourVariableName; Let us implement the above syntax.

How can I compare two table values in SQL?

Using relational operators: SELECT * FROM TableA UNION SELECT * FROM TableB EXCEPT SELECT * FROM TableA INTERSECT SELECT * FROM TableB; Change EXCEPT to MINUS for Oracle..
For Oracle, you need to use parentheses around the UNION, parentheses around the INTERSECT, and (as noted) replace EXCEPT with MINUS. ... .
Nice answer..

How do I check if two tables have the same data in SQL?

Step 1 - Test for Duplicate Rows on TABLEA. If SELECT DISTINCT * FROM TABLEA. ... .
Step 2 - Test for Duplicate Rows on TABLEB. If SELECT DISTINCT * FROM TABLEB. ... .
Step 3 - INNER JOIN TABLEA to TABLEB on every column..

How can I compare two database tables?

Comparing Database Data.
On the SQL menu, point to Data Compare, and then click New Data Comparison. ... .
Identify the source and target databases. ... .
Select the check boxes for the tables and views that you want to compare..