The difference between HAVING and WHERE clauses is that HAVING is used to filter aggregate results (that is, grouped data), while WHERE is used to filter rows in the base table or view. Additionally, HAVING requires the data to be grouped, while WHERE does not.
The difference between HAVING and WHERE clauses in Oracle
Introduction
HAVING and WHERE are two clauses used in Oracle to filter data results. Although they all have similar purposes, they have different application scenarios and functions.
Application scenarios
Grammar
WHERE clause:
SELECT column(s) FROM table WHERE condition(s)
HAVING clause:
SELECT column(s) FROM table GROUP BY column(s) HAVING condition(s)
Function comparison
WHERE clause | HAVING clause | |
---|---|---|
Filter rows | Filter aggregate results | |
Basic table or view | Grouped data | |
Before data retrieval | After data aggregation | |
Not required | Required | |
Supported | Not supported (only aggregate functions are supported) |
Example
WHERE clause example:
SELECT * FROM customers WHERE country = 'USA';
HAVING clause example:
SELECT country, COUNT(*) AS customer_count FROM customers GROUP BY country HAVING customer_count > 1000;
Conclusion
HAVING and WHERE clauses are used to filter data in Oracle, but their functions are different and their application scenarios are also different. The WHERE clause is used to filter the underlying data, while the HAVING clause is used to filter the aggregated data.The above is the detailed content of The difference between having and where in oracle. For more information, please follow other related articles on the PHP Chinese website!