26
Jul

MySQL HAVING


Summary: In this tutorial, you will learn how to use MySQL HAVING clause to specify a filter condition for a group of records or an aggregate.

Introducing MySQL HAVING clause

The MySQL HAVING clause is an optional part of and used only with the SQL SELECT statement. The MySQL HAVING clause specifies a filter condition for a group of record or an aggregate. The MySQL HAVING is often used with MySQL GROUP BY clause. When using with MYSQL GROUP BY clause, you can apply filter condition of the HAVING clause only to the columns appear in the GROUP BY clause. If the MySQL GROUP BY clause is omitted, the MySQL HAVING clause will behave like a WHERE clause. Notes that the MySQL HAVING clause applies to groups as a whole while the WHERE clause applies to individual rows.

Examples of MySQL HAVING clause

Let’s take a look at an example of using MySQL HAVING clause to have a better understanding.

We have orderDetails table in our sample database. We can use the MySQL GROUP BY clause to get all orders, number of items sold and total values in each order as follows:

MySQL HAVING - Sample Table

1 SELECT ordernumber,
2 sum(quantityOrdered) AS itemsCount,
3 sum(priceeach) AS total
4 FROM orderdetails
5 GROUP BY ordernumber

MySQL HAVING Example

Now you can ask what order has total value greater than $1000. In this case, you need to use the MySQL HAVING clause on aggregate to answer that question.

1 SELECT ordernumber,
2 sum(quantityOrdered) AS itemsCount,
3 sum(priceeach) AS total
4 FROM orderdetails
5 GROUP BY ordernumber
6 HAVING total > 1000

MySQL HAVING Example 2

We use column alias for the aggregate sum(priceeach) as total so in the HAVING clause we just have to specify that column alias total instead of typing the aggregate sum(priceeach) again.

You can use a complex condition in the MySQL HAVING clause such as OR, AND operators. For example if you want to know what order has total value greater than $1000 and has more than 600 items in it. You can use the following query to find out:

1 SELECT ordernumber,
2 sum(quantityOrdered) AS itemsCount,
3 sum(priceeach) AS total
4 FROM orderdetails
5 GROUP BY ordernumber
6 HAVING total > 1000 AND itemsCount > 600

MySQL HAVING Example 3

The MySQL HAVING clause is useful only with the MySQL GROUP BY clause for building output of high-level reports. For example, you can use the MySQL HAVING clause to answer questions like how many order has total values more than 1000 this month, this quarter and this year?…

In this tutorial, you have learned how to use the MySQL HAVING clause together with the MySQL GROUP BY to specify filter condition on a group or aggregate.

Related posts:

  1. Selecting Data with SQL IN
  2. MySQL GROUP BY
  3. MySQL INNER JOIN
  4. MySQL LEFT JOIN
  5. Using MySQL SELECT Statement to Query Data

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/