Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Having Clause in SQL

Having Clause in SQL

HAVING Clause in SQL

The HAVING clause is used to filter groups created by the GROUP BY clause based on a condition.


Why use HAVING?

  • You use WHERE to filter rows before grouping.

  • You use HAVING to filter groups after aggregation.


Syntax:

SELECT column1, AGGREGATE_FUNCTION(column2)FROM table_nameGROUP BY column1HAVING condition;

Example:

Suppose a Sales table:

RegionAmount
East10
East20
West15
West5
East30

Query: Regions with total sales greater than 30

SELECT Region, SUM(Amount) AS TotalSalesFROM SalesGROUP BY RegionHAVING SUM(Amount) > 30;

Result:

RegionTotalSales
East60

Key points:

  • HAVING works only with grouped data.

  • You can use aggregate functions (SUM(), COUNT(), etc.) in HAVING.

  • You can combine WHERE and HAVING in the same query.

Example:

SELECT Region, COUNT(*) AS NumSalesFROM SalesWHERE Amount > 5GROUP BY RegionHAVING COUNT(*) > 1;

If you want, I can provide more examples or help with your specific use case!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql