Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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)

fromEmployee_Table

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.9k answers

500 comments

693 users

Browse Categories

...