|
|
Custom Search
| |
|
| |
|
Using GROUP BY with HAVING Clause
In last tutorial, we have had a close look about how to use GROUP BY clause. In this tutorial, we are going to look at how to conditionally show data after they are grouped. We often need to selectively display data after they have been aggregated by using GROUP BY clause. Often, people confuse it with the usage of WHERE clause. WHERE clause does not work for GROUP BY because WHERE clause controls what data are affected before GROUP BY taking effect. For example, WHERE sets criteria for a SELECT statement - after the qualified rows are retrieved, GROUP BY is applied to these rows. Then HAVING clause is applied to the grouped data to further restrict what grouped rows should be displayed. To set criteria on what rows should be returned after GROUP BY is applied, we need to use HAVING clause. When using HAVING clause, consider the following facts and guidelines:
Practice #1: Using HAVING clause to selectively show aggregated data. 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 - 4 rows returned: Practice #2: Using HAVING clause to selectively show aggregated data. 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 - 13 rows 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 |