Differences between Having and Where in SQL and notes: sqlhaving

Source: Internet
Author: User

Differences between Having and Where in SQL and notes: sqlhaving

Differences:

The where clause removes rows that do not meet the where condition before grouping query results. That is, data is filtered before grouping. The where condition cannot contain a clustering function, use the where condition to filter out specific rows.

The having clause is used to filter groups that meet the conditions. That is, data is filtered after the group. The conditions often contain clustering functions and the having condition is used to filter out specific groups, you can also use multiple grouping standards for grouping.

Note:

1. The HAVING clause must be located before order by after group.

2. The WHERE statement is before the group by statement, and the SQL statement is calculated before the GROUP.

3. The HAVING statement is after the group by statement, and the SQL statement calculates the HAVING statement after the GROUP.

Example:

Select class, sum (Quantity) as quantity sum from A group by class having sum (Quantity)> 18

Example: joint use of Having and Where

Select category, SUM (Quantity) from A where quantity> 8 group by category having SUM (Quantity)> 10

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.