HAVING clause is used to filter grouped data. The syntax is: SELECT aggregate_functions(column_name) FROM table_name GROUP BY column_name HAVING condition;. It can filter on aggregate functions, grouping columns, constants or operators. For example, the following query filters out groups with at least 3 items in the order and a total amount greater than $100: SELECT COUNT(DISTINCT order_id) AS order_count FROM orders GROUP BY cus
The HAVING clause in SQL
#The role of the HAVING clause:
The HAVING clause is used to filter grouped data . It is similar to the WHERE clause, but is used to filter aggregated results rather than raw data.
The syntax of the HAVING clause:
SELECT aggregate_functions(column_name) FROM table_name GROUP BY column_name HAVING condition;
Conditions:
The conditions in the HAVING clause can include the following:
Usage of HAVING clause:
HAVING clause can be used in a variety of scenarios, for example:
Example:
Query orders that meet the following conditions:
SELECT COUNT(DISTINCT order_id) AS order_count FROM orders GROUP BY customer_id HAVING SUM(total_amount) > 100 AND COUNT(DISTINCT product_id) >= 3;Note:
- The HAVING clause can only be used after the GROUP BY clause.
- HAVING clause can only filter aggregate results, not original data.
The above is the detailed content of How to use having in sql. For more information, please follow other related articles on the PHP Chinese website!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-05-10 05:00:26
-
2024-05-10 04:57:21
-
2024-05-10 04:54:17
-
2024-05-10 04:51:19
-
2024-05-10 04:45:28
-
2024-05-10 04:39:16
-
2024-05-10 04:33:20
-
2024-05-10 04:30:23
-
2024-05-10 04:27:19
-
2024-05-10 04:24:18
Latest Issues
Leverage VueJS components in PHP
I want to mix basic HTML generated by Php and VueJS components without having to use VueJS...
From 2023-11-11 00:01:44
0
2
288
Popular Recommendations
Popular Tutorials
More>
-
-
-
JAVA Beginner's Video Tutorial
2360600
-
-
Latest Downloads
More>
-
-
-
-
-
-
-
-
-
About us
Disclaimer
Sitemap
-
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!