How to query the differences between where and having

Source: Internet
Author: User
The difference between where and having in a query: having clauses are similar but different from where clauses. They are all statements that set conditions. In the Query Process, aggregate statements (sum, min, max, avg, count) are executed preferentially than having clauses. the where clause takes precedence over aggregate statements (sum, min, max, avg, count) during query ). Simply put

The difference between where and having in a query: having clauses are similar but different from where clauses. They are all statements that set conditions. In the Query Process, aggregate statements (sum, min, max, avg, count) are executed preferentially than having clauses. the where clause takes precedence over aggregate statements (sum, min, max, avg, count) during query ). Simply put

How to query the differences between where and having:

Having clauses are similar but different from where clauses. They are all statements that set conditions.

In the Query Process, aggregate statements (sum, min, max, avg, count) are executed preferentially than having clauses. the where clause takes precedence over aggregate statements (sum, min, max, avg, count) during query ).

Simply put:

Where clause:

Select sum (num) as RMB from order where id> 10

// Only records with IDs greater than 10 can be queried for aggregation.

Having clause:

Select reportsto as manager, count (*) as reports from employees

Group by reportsto having count (*)> 4

Take the northwind database as an example. having condition expression as an aggregate statement. Certainly, the execution priority of the having clause Query Process is lower than that of the aggregate statement.

In other words, changing having to where will result in an error. Aggregate statements are used to count grouped data.

Having is used to determine the group data again. Having is not used if you do not use these links. Simply use where.

Having is used to make up for the deficiency of where in grouping data judgment. The where statement takes precedence over aggregation statements.

Aggregate functions. This is a special function that must be mentioned first, such as SUM, COUNT, MAX, and AVG. The fundamental difference between these functions and other functions is that they generally work on multiple records.

Select sum (population) FROM tablename

SUM is used in the population field of all returned records. The result is that only one result is returned for this query, that is, all

Total population of the country. BY using the group by clause, SUM and COUNT functions can be used for a GROUP of data.

When you specify group by region, only one row of data belonging to the same region is returned.

That is to say, all fields except region (region) in the table can only return a value after SUM, COUNT, and other aggregate function operations.

HAVING clause allows us to filter data of groups after grouping.

HAVING clause filters group records after aggregation

The WHERE clause filters records before aggregation, that is, before the group by clause and HAVING clause

Let's take a look at the following examples:

1. display the total population and total area of each region

SELECT region, SUM (population), SUM (area)

FROM bbc

Group by region

First, return records are divided into multiple groups BY region, which is the literal meaning of group. After grouping, Aggregate functions are used to calculate different fields (one or more records) in each group.

2. The total population and total area of each region are displayed. Only those regions with an area exceeding 1000000 square meters are displayed.

SELECT region, SUM (population), SUM (area)

FROM bbc

Group by region

Having sum (area)> 1000000

Here, we cannot use where to filter more than 1000000 of the regions, because such a record does not exist in the table.

On the contrary, the HAVING clause allows us to filter the group data.

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.