SQL group by usage

Source: Internet
Author: User

SQL group by usage

Group by, Count, minimum, maximum, or used to generate a total. Their output values are the same summary, listing the final appearance in each column in a report. You can also use these functions to output the total value of the score. The value in the entire column is split by the special clause group. It is combined into a record that all records share the same value in a field or field combination. The group by clause can group the results of one or more columns.

SELECT column_name_1,..., column_name_n, aggregate_function (expression)
FROM table_name
[WHERE condition]
Group by column_name1,..., column_name_n

The where clause is optional. If the keyword is present in the SQL statement, it is mentioned in the GROUP BY clause.

Example #1

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

This query returns different items and the so-called 'field list totalsum' counts the sum of antique results to supply each item.

Example #2

Select Item, Max (Price) as MaxPrice
From Antiques
Group by Item

This returns the name of each project and its highest price.

Example #3

Select SellerID, Count (*) as Number_of_Sellers
From Antiques
Where BuyerID = 15
Group by SellerID

Related Article

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.