Explore Courses Blog Tutorials Interview Questions
0 votes
in SQL by (20.3k points)

Is it possible to specify a condition in Count()? I would like to count only the rows that have, for example, "Manager" in the Position column.

I want to do it in the count statement, not using WHERE; I'm asking about it because I need to count both Managers and Other in the same SELECT (something like Count(Position = Manager), Count(Position = Other)) so WHERE is no use for me in this example.

1 Answer

0 votes
by (40.7k points)

 You can't limit the query itself with a WHERE clause. But you can use the count aggregate as it only counts the non-null values.

So, use this code:

SELECTcount (case Position when 'Manager' then 1 else null end)

FROM Table_Name

You can also use the sum aggregate in a similar way:

Select Sum (case Job_role when 'HR Associate' then 1 else 0 end)


Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers


108k users

Browse Categories