How to use NULLIF Function In SQL
We can use the NULLIF function in conjunction with the COUNT() function to count rows where a certain condition is met.
Example: Using NULLIF function to count employees with non-Sales departments.
SELECT COUNT(NULLIF(Department = 'Sales', 1)) AS non_sales_department_count
FROM employees;
Output:
Explanation: In the above query, the NULLIF() function returns NULL if the condition Department = ‘Sales’ is true and 1 otherwise. This effectively counts the rows where the ‘Department‘ column is not equal to ‘Sales‘, giving us the count of employees with non-Sales departments.
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.