Sqlhaving usage and example tutorial

Source: Internet
Author: User
The usage of sqlhaving is optional in conjunction with the HAVING clause in the instance tutorial. It is similar to the Where clause. However, after determining which records are restricted by the clause, it will show that they have been grouped. It is usually at the end of an SQL statement. The SQL statement of a HAVING clause may not include the groupby clause. The syntax is as follows: SELECTcolumn1,... column_n, aggregate_f SQL having usage and example tutorial

The HAVING clause is optional and can be used together with the group by clause. It is similar to the Where clause. However, after determining which records are restricted by the clause, it will show that they have been grouped. It is usually at the end of an SQL statement. The SQL statement of a HAVING clause may not include the group by clause.

Syntax:

SELECT column1,... column_n, aggregate_function (expression)
FROM table_name
[WHERE condition]
[Group by column1,... column_n]
HAVING condition
Instance:

Select Item, Sum (Price) as TotalSum
From Antiques
Group by Item
Having Sum (Price)> 57

The query returns different items and the so-called 'field list totalsum' counts the sum of antique results to supply each item. The HAVING clause will only return the amount of more than 57 USD and the results of the project.

Example #2

Select SellerID, Count (*) as Number_of_Sellers
From Antiques
Where BuyerID = 21
Group by SellerID
Having Count (*)> 1

The example shows the seller's ID list and their quantity, but only when more than one BuyerID is equal to 15 sellers.

Example #3

SELECT Item, COUNT (Item) AS Total, MAX (Price) AS MaxPrice
FROM Antiques
Group by Item
Having count (Item)> 1 and max (Price) <500

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.