Select statement inside Case count condition

Select statement inside Case count condition

While working with database many time requirement comes to get data using select query based on certain condition and one of these condition is to compare and get data joining two table and put count condition inside case statement. Here I will show how to use count condition inside case statement by using sub query:

Let’s say you have two table: Employee and Employer and you need to match employer id on employer table to get count and put value in result:

SELECT (CASE when (SELECT DISTINCT COUNT(*) FROM Employer EM
WHERE EMP.Id=EM.Id) > 0 THEN ‘true’ ELSE ‘false’ END) AS Employee Id
FROM Employee EMP

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *