Home SQL count ()
E-mail Print PDF

COUNT ()

Syntax SQL COUNT (column_name)

SELECT COUNT(column_name) FROM table_name

Syntax SQL COUNT (*)

In the COUNT (*) - returns all records in the table below:

SELECT COUNT(*) FROM table_name

Syntax SQL COUNT (DISTINCT column_name)

COUNT (DISTINCT COLUMN_NAME) - returns only the different entries:

SELECT COUNT(DISTINCT column_name) FROM table_name

Note - Note: COUNT (DISTINCT) is working in ORACLE and Microsoft SQL Server, but not in Microsoft Access.

Example SQL COUNT (column_name)

O_IdOrderDateOrderPriceCustomer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen

Now we want to count the number of orders from the client "Nilsen".

1
2
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Nilsen'

Query Result:

CustomerNilsen
2

Example SQL COUNT (*)

If you do not prescribe a WHERE clause E.

1
SELECT COUNT(*) AS NumberOfOrders FROM Orders

The result will be:

NumberOfOrders
6

Example SQL COUNT (DISTINCT column_name)

1
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders

Query Result:

NumberOfCustomers
3

Comments

 
+7 #1 Иван 2010-12-23 21:36 No descriptions hvataet fokusov type

count(case when people.sex='м' then 1 else null end)

And sometimes a useful thing. By the way, here: ddev.ru /.../... written, apparently, more generally, however, is only one example.
Quote
 
 
-2 #2 артем 2011-05-23 14:54 Ivan: you can always write a where clause Quote
 
 
+2 #3 Ред 2011-06-23 22:40 not always write the conditions where is the best course (and correct).
For example, if you have one just a lot of output County.
Quote
 
 
0 #4 Матвей 2011-08-14 22:06 And if you need a sample of all customers with the number of orders? Quote
 
 
+1 #5 Матвей 2011-08-14 22:51 for example
elect Customer, COUNT(O_Id) as CountOrders
from Orders
group by Customer
order by CountOrders desc
Quote
 
 
-2 #6 jon 2011-12-27 20:14 Quote
 
 
-3 #7 Алексей 2012-01-03 14:54



Quote
 
 
-3 #8 Алексей 2012-01-03 15:00
Quote
 

Authorization

Nice Ajax Poll

Which one of my extensions is the best?

Statistics

Translate

русскийitalianoDeutschEnglishLATVIANукраїнськаfrançaispolski
1

Advertisement