SQL having | Having clause command in sql - sql - sql tutorial - learn sql




  • The HAVING clause is used to filter the result set based on the result of an aggregate function.
  • It is typically located near or at the end of the SQL statement.
  • HAVING is often coupled with the presence of the GROUP BY clause, although it is possible to have a HAVING clause without the GROUP BY clause.

Syntax:

SELECT ["column_name1"], "function type" ("column_name2")
FROM "table_name"
[GROUP BY "column_name1"]
HAVING (arithmetic function condition);
  • The brackets around "column_name1" and GROUP BY "column_name1" means that they are optional.
  • Note: We may select zero, one, or more columns in addition to the aggregate function. If we do select any column outside of the aggregate function, there is no need for the GROUP BY clause.

Example 1:

  • We use the following table for our example.
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Table Store_Information

Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
  • To see only the stores with sales over $1,500, we would type,
SELECT Store_Name, SUM(Sales)
FROM Store_Information
GROUP BY Store_Name
HAVING SUM(Sales) > 1500;
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

Store_Name SUM(Sales)
Los Angeles 1800
  • Total sales for both San Diego and Boston are below $1,500, so the "HAVING SUM(Sales) > 1500" clause filters out these two stores.

Example 2:

  • Consider the CUSTOMERS table having the following records.
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
  • Following is an example, which would display a record for a similar age count that would be more than or equal to 2.
SQL > SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 2;
  • This would produce the following result
ID NAME AGE ADDRESS SALARY
2 Khilan 25 Delhi 1500.00

SQL Having Example 1

SQL Having Example 1

SQL Having Example 2

SQL Having Example 2

This tutorial provides more the basic needs and informations on sql tutorial , pl sql tutorial , mysql tutorial , sql server , sqlcode , sql queries , sql , sql formatter , sql join , w3schools sql , oracle tutorial , mysql , pl sql , learn sql , sql tutorial for beginners , sql server tutorial , sql query tutorial , oracle sql tutorial , t sql tutorial , ms sql tutorial , database tutorial , sql tutorial point , oracle pl sql tutorial , oracle database tutorial , oracle tutorial for beginners , ms sql server tutorial , sql tutorial pdf

Related Searches to Having clause in sql | Having clause command in sql