codesnout.com

SQL COUNT

Google Ads

SQL COUNT() Function


SQL Sample Code - SQL COUNT() Function


SELECT COUNT(column_name)
FROM table_name;

See the examples section below for an explanation of the COUNT function.


What does the COUNT() Function do?

The SQL COUNT() function calculates the number of rows of retrieved by a SQL select statement.


Sample 1 – SQL COUNT() Function

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this SQL sample we simply want to know the number COUNT() of records in the tblCompany table.

Use the SELECT statement below:

SELECT COUNT(CompanyName) AS Number_of_records FROM tblCompany;

The result will look like this:

Number_of_records
4

Note: the AS operator is used to name the result column as ‘Number_of_records'’. See the Alias section of this website to learn more about renaming database columns and tables.


Sample 2 – SQL COUNT() Function using GROUP BY.

In this SQL sample we will use the database table ‘tblCompany’.

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
3 Sample Code World 66 SQL St Curry 18000
4 SQL Reference Ltd 34 Reference St Pisa 17000

In this SQL sample we want to know the number of Sales records for each town in the tblCompany table.

Use the SELECT statement below:

SELECT Town, COUNT(Sales) FROM tblCompany
GROUP BY Town;

The result will look like this:

Town COUNT(Sales)
Hamburg 2
Curry 1
Pisa 1

 


Related SQL Sample Code:


About Us | Privacy Policy | Contact Us | ©2010 Thunderousity Information Management Solutions | www.autosnout.com - Car Performance Statistics Website Utilising SQL | SQL Blog |

SQL Home | SQL Intro | SQL SELECT Statement | DISTINCT | WHERE | SQL Wildcards | AND OR | IN | BETWEEN | LIKE | ORDER BY | GROUP BY | TOP | ALIAS | DELETE | INSERT | UPDATE | CREATE DATABASE | CREATE TABLE | ALTER | INNER JOIN | FULL JOIN | LEFT JOIN | RIGHT JOIN | UNION | CONSTRAINTS | NOT NULL| Aggregate Functions | SUM | AVG | COUNT | MAX | MIN | FIRST | LAST | GROUP BY | HAVING | LIMIT | Microsoft Access Data Types | MySQL Data Types | SQL Server Data Types | ASCII HTML Reference Sheet |