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.