The difference between having and where in SQL and attention _ database other

Source: Internet
Author: User

Difference Introduction:

The action of the WHERE clause is to remove rows that do not conform to the where condition before grouping the query results, that is, to filter the data before the group, where the cluster function cannot be included, and the Where condition to filter out a particular row.

The HAVING clause is used to filter the groups that meet the criteria, that is, to filter the data after grouping, often including the clustering function, using the having condition to filter out specific groups, or multiple grouping criteria for grouping.

Attention:

1. The HAVING clause must precede the order by after group by.

2. The WHERE statement precedes the group BY statement, and SQL calculates the where statement before grouping.

3. The having statement is after the group by statement, and SQL calculates the having statement after grouping.

Example:

Select category, sum (quantity) as quantity and from A Group by category 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

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.