Count Where in SQL
? COUNT with WHERE Clause in SQL
You can use the COUNT() function together with a WHERE clause to count rows that meet a specific condition.
? Syntax
SELECT COUNT(*)FROM table_nameWHERE condition;? Example
Suppose you have a table employees and want to count how many employees are in the Sales department:
SELECT COUNT(*) AS sales_countFROM employeesWHERE department = 'Sales';? Counting Non-Null Values with Condition
SELECT COUNT(email) AS emails_in_salesFROM employeesWHERE department = 'Sales';Counts non-null
emailvalues only for employees in Sales.
? Using COUNT with Conditional Aggregation
Sometimes you want to count based on a condition but within a single query without filtering rows:
SELECT COUNT(CASE WHEN department = 'Sales' THEN 1 END) AS sales_count, COUNT(CASE WHEN department = 'HR' THEN 1 END) AS hr_countFROM employees;If you want, I can provide examples with more complex conditions or grouping!