Q:

Check whether a column returns a set of values in sql

I have a table having a column which contain multiple occurrence of numeric values 1,2 and 3 only.
SELECT * from table1 where column_name=1

SELECT * from table1 where column_name=2

SELECT * from table1 where column_name=3

Is it possible that I can make these queries to a single query because calling 3 sql select query statement for a single requirement from a single table look awkward .
I need to do something if the table has atleast single row for each condition.
I have used the following query:
SELECT * 
from table1 
where column_name IN (1,2,3). 

It return the data even if column with any of the value doesn't exist in the table.I need to return the table if the table has at least one row for each where clause.
I have also tried the following query:
SELECT *     
FROM table1 
where (column_name =1 
AND column_name =2 AND column_name =3)
//this returns null even if a single column has value 1,2 and 3

A:

You can use the following query:
SELECT MAX(CASE WHEN column_name = 1 THEN 1 END) +
       MAX(CASE WHEN column_name = 2 THEN 1 END) +
       MAX(CASE WHEN column_name = 3 THEN 1 END)
FROM table1 

to check if the specified condition is satisified. 
The above will return 3 if all three values (1,2,3) are contained in column_name column of table1 (in separate records of course), and NULL otherwise.
You can now use the above query in a CTE like this:
;WITH CTE AS (
   SELECT MAX(CASE WHEN column_name = 1 THEN 1  END) +
          MAX(CASE WHEN column_name = 2 THEN 1  END) +
          MAX(CASE WHEN column_name = 3 THEN 1  END) AS cnt
   FROM table1
)
SELECT t.*
FROM #table1 AS t
CROSS JOIN CTE 
WHERE cnt = 3

to return the table, if the table satisfies the requirement, i.e. if the table contains:

at least one row with column_name = 1 and
at least one row with column_name = 2 and
at least one row with column_name = 3 

