Home     SQL Basic     Contact Us     Site Map    

SQL BASIC

SQL SELECT

SQL WHERE

SQL DISTINCT

SQL AND

SQL OR

SQL NOT

SQL ORDER BY

SQL IN

SQL BETWEEN

SQL LIKE

SQL ALIAS

SQL AGGREGATE

>SQL COUNT

>SQL SUM

>SQL MAX

>SQL MIN

>SQL AVG

SQL GROUP BY

SQL HAVING

SQL INSERT

SQL UPDATE

SQL DELETE

SQL SELECT INTO

SQL CREATE DATABASE

SQL CREATE TABLE

SQL DROP TABLE

SQL DROP DATABASE

SQL CREATE INDEX

SQL PRIMARY KEY

SQL FOREIGN KEY

SQL ALTER TABLE

SQL TRUNCATE TABLE

SQL JOIN

SQL INNER JOIN

SQL OUTER JOIN

SQL CROSS JOIN

SQL UNION

SQL UNION ALL

SQL INTERSECT



SQL COUNT

The first aggregate function I am going to cover is the COUNT() function.

The SQL COUNT aggregate function is used to count the number of rows in database table, which is directed to count on it.

The SQL COUNT syntax is simple and looks like this:

SELECT COUNT ( [COLUMN NAME] ) FROM [TABLE NAME]

If you want to count the number of rows returned by the query, the easiest way to do is use the COUNT(*) expression.


EXAMPLE :

We start with a simple example which is return number of row in the table.

Table GameScores

PlayerNameDepartmentScores
JasonIT3000
IreneIT1500
JaneMarketing1000
DavidMarketing2500
PaulHR2000
JamesHR2000

SQL statement :

SELECT COUNT(*) FROM GameScores

Result:

COUNT(*)
6


EXAMPLE (WITH DISTINCT) :

When the DISTINCT is specified before the column name, all the duplicate values are removed first and then the addition is carried out. Let's say we want to select the number of departments for contestant in the table .

SQL statement :

SELECT COUNT( DISTINCT Department) FROM GameScores

Result:

COUNT( DISTINCT Department)
3