modeller modeller - 12 days ago 4
SQL Question

How to find whether one column is a stricter selection criteria than another column?

I have a table with two columns

col_A
and
col_B
. I want to verify my hypothesis that
col_A
is a stricter selection criteria than
col_A and col_B
. i.e.
select * from table where col_A = something_A
will return the same result as `select * from table where col_A = something_A and col_B = something_B'.

May I know how to do this?

Answer

You can count how many times each selection criterion matches:

SELECT
  SUM(CASE WHEN col_A = 'something A' THEN 1 END) AS MatchesA,
  SUM(CASE WHEN col_B = 'something B' THEN 1 END) AS MatchesB
FROM table

The CASE returns 1 if the condition passes or NULL if it doesn't. The 1 is summed; the null is ignored.

Comments