SqlCOUNT () function
The count () function returns the number of rows that match the specified criteria.
SQL COUNT (column_name) syntax
The count (column_name) function returns the number of values for the specified column (NULL does not count in):
SELECT COUNT from table_name;
SQL COUNT (*) syntax
The count (*) function returns the number of records in the table:
SELECT COUNT (* from table_name;
SQL COUNT (DISTINCT column_name) syntax
The COUNT (DISTINCT column_name) function returns the number of different values for the specified column:
SELECT COUNT (DISTINCT from table_name;
notes: COUNT (DISTINCT) applies to ORACLE and Microsoft SQL Server, but not to Microsoft Access.
Demo Database
In this tutorial, we will use the Runoob sample database.
The following is the data selected from the "Access_log" table:
+-----+---------+-------+------------+|Aid|site_id| Count |Date|+-----+---------+-------+------------+| 1 | 1 | $ | .- to-Ten || 2 | 3 | - | .- to- - || 3 | 1 | the | .- to- - || 4 | 2 | Ten | .- to- - || 5 | 5 | 205 | .- to- - || 6 | 4 | - | .- to- the || 7 | 3 | - | .- to- the || 8 | 5 | 545 | .- to- - || 9 | 3 | 201 | .- to- - |+-----+---------+-------+------------+
SQL COUNT (column_name) instance
The following SQL statement calculates the total number of accesses to the "site_id" =3 in the "Access_log" table:
Instance
SQL COUNT (*) instance
The following SQL statement calculates the total number of records in the "Access_log" table:
Instance
SELECT COUNT (* as from Access_log;
Execute the above SQL output as follows:
SQL COUNT (DISTINCT column_name) instance
The following SQL statement calculates the number of records for different site_id in the "Access_log" table:
Instance
SELECT COUNT (DISTINCT as from Access_log;
Execute the above SQL output as follows:
-- Query the number of bars for all records Select Count (* from access_log; -- Query the number of records that are not empty in the Alexa column in the websites table Select Count from websites; -- Query the number of records that are not duplicated in the country column in the websites table Select Count (distinct from websites;
SQL COUNT () function