How to use CASE Statement In SQL
We can utilize the CASE statement within the COUNT() function to count rows based on specific conditions.
Example: Using CASE statement to count employees with salary above 55000.
SELECT COUNT(CASE WHEN Salary > 55000 THEN 1 END) AS high_salary_count
FROM employees;
Output:
Explantion: Here the CASE statement inside the COUNT() function evaluates each row. If the Salary column value is greater than 55000 then it returns 1, otherwise it returns NULL. The COUNT() function then counts the non-null values returned by the CASE statement and giving the count of employees with a salary above 55000.
How to Specify Condition in Count() in SQLite?
In SQLite, One common question that arises is whether it’s possible to specify a condition in the Count()
function in SQLite. This question is particularly relevant when we want to count only certain rows based on a specific condition. We will explore different approaches to updating multiple rows effectively using SQLite.