Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

HAVING Clause



The SQL HAVING Clause


The SQL HAVING clause is used in combination with the GROUP BY clause to filter the results of aggregate functions applied to groups of rows. While the WHERE clause filters individual rows before they are grouped, the HAVING clause filters groups of rows after they have been grouped. The basic syntax of the HAVING clause is as follows:

SELECT column1, column2, ..., aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column1, column2, ...
HAVING condition

For example, let's say you have a table called orders with columns customer_id and total_amount. If you want to find customers who have spent more than a certain amount, say $1000, you can use the HAVING clause like this:

SELECT customer_id, SUM(total_amount) AS total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(total_amount) > 1000;

In this example:

The result will contain each customer_id along with the corresponding total amount spent by that customer, but only for customers who have spent more than $1000.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java