|
|
Custom Search
| |
|
| |
|
Using Aggregate Functions and Group By Clause in MySQL
Aggregate Functions are used to calculate summarized data such as an average or a sum. MySQL provides the common aggregate functions plus some MySQL specific ones. In this tutorial, we'll only cover the most often used aggregate functions. Note that all aggregate functions, with the exception of COUNT(*) function, ignore NULL values in the columns. When summarize values on a column, aggregate Functions can be used for the entire table or for groups of rows in the table. When applied to groups of rows, use GROUP BY clause with the SELECT statement.
Practice #1-1: Using AVG function in orders table for the entire table. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 1 row returned: Practice #1-2: Using AVG function in orders table for one customer. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 1 row returned: Practice #1-3: Using AVG function in orders table for each customer. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 89 rows returned: Practice #2-1: Using COUNT function. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 9 rows returned: Practice #2-2: Using COUNT function on a column where NULL value exists. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 8 rows returned: Practice #3-1: Using COUNT(DISTINCT) function. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 1 row returned: Practice #3-2: Using COUNT(DISTINCT) function with NULL values. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query 1 result set - 1 row returned:
Query 2 result set - 3 rows returned: Practice #4-1: Using COUNT(*) function. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 9 rows returned: Practice #4-2: Using COUNT(*) function to count NULL values. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 1 row returned: Practice #5: Using MAX() function. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 1 row returned: Practice #6: Using MIN() function. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 1 row returned: Practice #7: Using SUM() function. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 1 row returned: Practice #8-1: Using GROUP_CONCAT() function. GROUP_CONCAT returns a concatenated string of non-NULL values for each group of rows in a table. Although GROUP_CONCAT is not an ANSI and an ISO standard function (which means it's an MySQL extension), it's very useful in the way that it saves you time to use another language such as PHP to loop and build a concatenated string by yourself. The full syntax of GROUP_CONCAT function is as follows: GROUP_CONCAT([DISTINCT] In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 8 rows returned: Practice #8-2: Using GROUP_CONCAT() function for distinct values. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. To execute a query, move your cursor anywhere inside the query and then press F9. Note that the SQL needs to end with semi-colon. /*
Query result set - 8 row returned:
|
|
Copyright © 2010 GeeksEngine.com. All Rights Reserved. This website is hosted by LunarPages. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
| Home | Kung Fu Timer | Feedback | Terms of Use | Privacy Policy |