The usage of the HAVING clause in SQL

Source: Internet
Author: User

Having

One problem that users may want to solve in the process of using SQL language is to limit the output of the results of sum or other aggregate functions. For example, we may only want to see information about stores that have a total sales of more than 1500 dollars in the Store_information datasheet, and then we need to use the HAVING clause. The syntax format is:

Select "Column_name1", SUM ("column_name2")

From "table_name"

GROUP by "column_name1"

Having (arithematic function condition)

(GROUP by clause optional)

Therefore, we can use the following command to achieve the purpose of the above query:

SELECT store_name, SUM (sales)

From Store_information

GROUP by Store_name

Having SUM (sales) > 1500

The query results appear as follows:

Store_name SUM (Sales)

Los Angeles $1800

Note

Use a HAVING clause instead of the WHERE clause in the SQL language when setting the query condition of the aggregate function. Usually, the HAVING clause is placed at the end of the SQL command



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.