SQL Progressive (a) HAVING clause

Source: Internet
Author: User

HAVING clause

The following first gives the syntax of the HAVING clause:

SELECT Column1, SUM (Column2)

From "List-of-tables"

GROUP by "Column-list"

having "condition";

This HAVING clause allows you to specify criteria for each group, in other words, you can select rows based on the criteria you specify. If you want to use the HAVING clause, it should be followed by the GROUP BY clause.

Here is an example to explain the HAVING clause. Suppose that our employee table contains the employees ' name, Departmen, salary, and age. If you want to choose an average wage for each employee in each department, you can use the following SQL statement:

SELECT Dept, AVG (Salary)

From employee

GROUP by dept;

Of course, you can add a HAVING clause if you want to calculate and display only the average wage of salary greater than 20000:

SELECT Dept, AVG (Salary)

From employee

GROUP by Dept

Having avg (salary) > 20000;



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.