10
10
SELECT "function type" ("column_name")
13
The following are examples of aggregate functions:
15
**AVG**: Return the average value of the argument. (Does not work with temporal values unless first converted to numeric values.)
13
The following are examples of aggregate functions:
15
:ref:`avg`: Return the average value of the argument. (Does not work with temporal values unless first converted to numeric values.)
18
18
(DISTINCT): Return the count of a number of different values
20
:doc:`count`: Return a count of the number of rows returned
20
:ref:`count`: Return a count of the number of rows returned
22
**GROUP_CONCAT**: Return a concatenated string
24
**MAX**: Return the maximum value
26
**MIN**: Return the minimum value
28
**STD**: Return the population standard deviation
30
**STDDEV_POP**: Return the population standard deviation
32
**STDDEV_SAMP**: Return the sample standard deviation
34
**STDDEV**: Return the population standard deviation
36
**SUM**: Return the sum. (Does not work with temporal values unless first converted to numeric values.)
38
**VAR_POP**: Return the population standard variance
40
**VAR_SAMP**: Return the sample variance
42
**VARIANCE**: Return the population standard variance
22
:ref:`group_concat`: Return a concatenated string
24
:ref:`max`: Return the maximum or minim values
32
The AVG function returns the average value for the specified column in a table. To find the average session time for users and GROUP BY last_name:
36
SELECT last_name, AVG(session_length)
37
-> FROM session_details GROUP BY last_name;
39
AVG() returns NULL if there are no matching rows.
47
Take the following "Nodes" table, where 'nodes' are user-contributed content:
49
+--------+-------------------+------------+----------------+-------------------+
50
|NodeID |ContributionDate |NodeSize |NodePopularity |UserName |
51
+========+===================+============+================+===================+
52
|1 |12/22/2010 |160 |2 |Smith |
53
+--------+-------------------+------------+----------------+-------------------+
54
|2 |08/10/2010 |190 |2 |Johnson |
55
+--------+-------------------+------------+----------------+-------------------+
56
|3 |07/13/2010 |500 |5 |Baldwin |
57
+--------+-------------------+------------+----------------+-------------------+
58
|4 |07/15/2010 |420 |2 |Smith |
59
+--------+-------------------+------------+----------------+-------------------+
60
|5 |12/22/2010 |1000 |4 |Wood |
61
+--------+-------------------+------------+----------------+-------------------+
62
|6 |10/2/2010 |820 |4 |Smith |
63
+--------+-------------------+------------+----------------+-------------------+
65
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 the number of nodes made by user Smith, we will use the following SQL COUNT expression:
69
SELECT COUNT * FROM Nodes
70
WHERE UserName = "Smith";
72
In the above statement, the COUNT keyword returns the number 3, because the user Smith has 3 total nodes.
74
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:
78
SELECT COUNT * FROM Nodes;
86
GROUP_CONCAT returns a string result with the concatenated non-NULL values from a group.
88
For example, without GROUP_CONCAT, this query:
92
SELECT id,client_id FROM services WHERE id = 3;
106
But using GROUP_CONCAT in an alternate query:
108
.. code-block:: mysql
110
SELECT id,GROUP_CONCAT(client_id) FROM services WHERE id = 3 GROUP BY id;
114
+----+-------------------------+
115
| id | GROUP_CONCAT(client_id) |
116
+====+=========================+
118
+----+-------------------------+
126
MAX returns the maximum value in a group. In cases where MAX is passed a string argument, it will return the maximum string value.
128
MIN returns the minimum value of a group. Like MAX, MIN returns the minimum string value string value.
130
MAX and MIN return NULL if there are no matching rows.
132
.. code-block:: mysql
134
SELECT product_id, MIN(price), MAX(price)
138
** Question: For MAX(), does Drizzle compare ENUM and SET columns by their string value rather than by the string's relative position in the set? **