1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
COUNT
-----
Take the following "Nodes" table, where 'nodes' are user-contributed content:
+--------+-------------------+------------+----------------+-------------------+
|NodeID |ContributionDate |NodeSize |NodePopularity |UserName |
+--------+-------------------+------------+----------------+-------------------+
|1 |12/22/2010 |160 |2 |Smith |
+--------+-------------------+------------+----------------+-------------------+
|2 |08/10/2010 |190 |2 |Johnson |
+--------+-------------------+------------+----------------+-------------------+
|3 |07/13/2010 |500 |5 |Baldwin |
+--------+-------------------+------------+----------------+-------------------+
|4 |07/15/2010 |420 |2 |Smith |
+--------+-------------------+------------+----------------+-------------------+
|5 |12/22/2010 |1000 |4 |Wood |
+--------+-------------------+------------+----------------+-------------------+
|6 |10/2/2010 |820 |4 |Smith |
+--------+-------------------+------------+----------------+-------------------+
The SQL COUNT function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. If we want to count how many orders has made a customer with CustomerName of Smith, we will use the following SQL COUNT expression:
.. code-block:: mysql
SELECT COUNT * FROM Nodes
WHERE UserName = "Smith";
In the above statement, the COUNT keyword returns the number 3, because the user Smith has 3 total nodes.
If you don’t specify a WHERE clause when using the COUNT keyword, your statement will simply return the total number of rows in the table, which would be 6 in this example:
.. code-block:: mysql
SELECT COUNT * FROM Nodes;
|