SQL COUNT () function

Source: Internet
Author: User
Tags microsoft sql server

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.