Home > Database > SQL > body text

Common aggregate functions in sql

下次还敢
Release: 2024-05-07 05:48:15
Original
902 people have browsed it

SQL common aggregate functions include: COUNT() Calculate the number of rows SUM() Sum AVG() Find the average MIN() Find the minimum value MAX() Find the maximum value

Common aggregate functions in sql

Commonly used aggregate functions in SQL

The aggregate functions in SQL (Structured Query Language) are used to operate on a set of values ​​and combine them Aggregate into one or more values. The most common aggregate functions include:

  • COUNT(): Counts the total number of rows in a set of rows, regardless of whether there are values ​​in those rows.
  • SUM(): Add a set of values.
  • AVG(): Calculate the average of a set of values.
  • MIN(): Returns the minimum value in a group.
  • MAX(): Returns the maximum value in a group.

Other commonly used aggregate functions:

  • GROUP_CONCAT(): Concatenate a set of strings into one string , separated by the specified delimiter.
  • FIRST(): Returns the value of the first row in a group.
  • LAST(): Returns the value of the last row in a group.
  • STDDEV():Calculate the standard deviation of a set of values.
  • VARIANCE(): Calculate the variance of a set of values.

Use aggregate functions:

Aggregation functions are usually used in the GROUP BY clause of the SELECT statement, which groups query results by specified columns. For example, the following query calculates the average sales for each group:

<code>SELECT department_name, AVG(sales)
FROM sales
GROUP BY department_name;</code>
Copy after login

Aggregate functions can also be used in the HAVING clause, which filters groups. For example, the following query returns only departments with average sales greater than 1000:

<code>SELECT department_name, AVG(sales)
FROM sales
GROUP BY department_name
HAVING AVG(sales) > 1000;</code>
Copy after login

The above is the detailed content of Common aggregate functions in sql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template