Aggregate Function
In DBMS, an aggregate function is a function where the values of the single column and multiple row values are processed together to form a single summation value. The aggregate functions are SUM(), COUNT(), MAX(), MIN(), and so on. All aggregate functions are used for the different operations.
For example, you can use the COUNT () function to get the number of employees from the employees table, or the number of departments from the departments table, and so on.
Example: To understand the SQLite COUNT Function in a more depth manner we need a table on which we will perform the operations. So will create a table called staff table which consists of emp_id, name, salary and dept as Columns. If you don’t know How to Create a Table to refer this.
let us consider the staff table and the fields in the table are emp_id, name, salary and dept with the 5 records inserted in it and you can find that the dept field for the emp_id 5 is Null because there is no data insered in it.
Example 1: Now we will try to count the no of records in the staff table.
Query:
select count( ) from staff;
Output:
Explanation: As, there are 5 records in the staff table and it is showing the count as 5 in the above output.
Count( ) Function Counts the NULL Values or Not?
Now let us count the dept column and check whether they counts the exact dept count or not. The syntax is select keyword followed by count and in it we specify the column name that to be counted and from followed by the table name from which the column is to be counted.
Query:
select count(dept ) from staff;
Output:
Explanation: In the above Query, we can see that it does not count the NULL values as the dept count is 5 but the output is 4, it is because as we have passed the column_name within the count function it does not count the NULL values.
Note: But, it counts the NULL values if you do not pass the column name inside the count function as it is shown below:
Query:
select count() dept
from staff;
Output:
Explanation: this is how it counts the NULL values too as the output is five, even the no of rows inserted are also five and the count is equal.
SQLite COUNT
SQLite is a serverless database engine, it is written in C programming language and is easy to use and store the data. It is one of the most used database engines. It is widely used for the development of Embedded applications. In this article we will learn about the SQLite count, how it works and what are all the other functions that are used with SQLite to get the desired output.