Open In App

How to Specify Condition in Count() in SQLite?

Last Updated : 16 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

How to put a Condition in count()?

In SQLite, the COUNT() function is used to return the number of rows that match a specified condition. It counts the number of non-null values in a specified column or counts all the rows in a table if no column is specified.

The COUNT() function in SQLite retrieves the number of rows in a specified column or table that meet a given condition. We can specify conditions in the COUNT() function in SQLite using the below method as follows:

  1. Using WHERE Clause
  2. Using CASE Statement
  3. Using NULLIF Function

To understand how to specify conditions in Count() in SQLite we need a table on which we will perform various operations and queries. Here we will consider a table called employees which contains id, name, salary, and department as Columns.

employeetable10

Employees Table

1. Using WHERE Clause

We can include a WHERE clause within the COUNT() function to specify the condition.

Example: Using WHERE clause to count employees in the Sales department

SELECT COUNT(*) AS sales_department_count
FROM employees
WHERE Department = 'Sales';

Output:

USEWHEREclause

Output

Explanation: This query uses the WHERE clause within the COUNT() function to count the number of employees in the Sales department. Only the rows where the ‘Department‘ column equals ‘Sales‘ are included in the count.

2. Using CASE Statement

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:

USECASE

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.

3. Using NULLIF Function

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:

NULLIF

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.

Conclusion

In conclusion, SQLite offers several versatile approaches for incorporating conditions within the COUNT() function to retrieve specific counts from a database. By utilizing the WHERE clause, CASE statement, or NULLIF() function, users can tailor their queries to count rows based on precise criteria. These examples demonstrate SQLite’s flexibility in handling conditional counting tasks, allowing for efficient data analysis and reporting. Whether counting employees in a particular department, evaluating salary thresholds, or identifying non-Sales department employees, SQLite’s functionality empowers users to extract valuable insights from their data with ease.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads