|
Custom Search
| |
|
Using GROUP BY with HAVING Clause in MySQL 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. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 4 rows returned: Practice #2: Using HAVING clause to selectively show aggregated data. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 13 rows returned: Other tutorials in this category 1. Using Aggregate Functions and Group By Clause in MySQL |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. 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 | Feedback | Terms of Use | Privacy Policy |