Insane Coder Insane Coder - 8 days ago 6
SQL Question

Compare row count of two tables in a single query and return boolean

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

Answer

you have to remove :

FROM Table1,Table2

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

Comments