{"id":1006,"date":"2018-10-21T15:58:10","date_gmt":"2018-10-21T08:58:10","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=1006"},"modified":"2024-04-03T14:51:21","modified_gmt":"2024-04-03T07:51:21","slug":"sql-server-aggregate-functions","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/","title":{"rendered":"SQL Server Aggregate Functions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the SQL Server aggregate functions and how to use them to calculate aggregates.<\/p>\n\n\n\n<p>An aggregate function operates on a set of values and returns a single value. In practice, you often use aggregate functions with the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-group-by\/\">GROUP BY<\/a><\/code> clause and <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-having\/\">HAVING<\/a><\/code> clause to aggregate values within groups.<\/p>\n\n\n\n<p>The following table shows the most commonly used SQL Server aggregate functions:<\/p>\n\n\n\n<input id=\"search\" class=\"light-table-filter\" type=\"text\" placeholder=\"Type a function name to search...\" data-table=\"functions\"><\/input><\/p>\n\n\n\n<figure class=\"wp-block-table responsive functions\"><table><thead><tr><th>Aggregate function<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-avg\/\">AVG<\/a><\/td><td>Calculate the average of non-NULL values in a set of values.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-checksum_agg\/\">CHECKSUM_AGG<\/a><\/td><td>Calculate a checksum value based on a group of rows.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-count\/\">COUNT<\/a><\/td><td>Return the number of rows in a group that satisfy a condition.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-count-star\/\">COUNT(*)<\/a><\/td><td>Return the number of rows from a table, which meets a certain condition.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-count-distinct\/\">COUNT(DISTINCT)<\/a><\/td><td>Return the number of unique values in a column that meets a certain condition.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-count-if\/\">COUNT IF<\/a><\/td><td>Show you how to use the <code>COUNT<\/code> function with the <code>IIF<\/code> function to form a <code>COUNT IF<\/code> function that returns the total number of values based on a condition.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-count\/\">COUNT_BIG<\/a><\/td><td>The <code>COUNT_BIG()<\/code> function returns the number of rows (with BIGINT data type) in a group, including rows with NULL values.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-max\/\">MAX<\/a><\/td><td>Return the highest value (maximum) in a set of non-NULL values.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-min\/\">MIN<\/a><\/td><td>Return the lowest value (minimum) in a set of non-NULL values.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-stdev\/\">STDEV<\/a><\/td><td>Calculate the sample standard deviation of a set of values.<\/td><\/tr><tr><td>STDEVP<\/td><td>Return the population standard deviation of a set of values.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-sum\/\">SUM<\/a><\/td><td>Return the summation of all non-NULL values in a set.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-sum-if\/\">SUM IF<\/a><\/td><td>Use the <code>SUM<\/code> function with the <code>IIF<\/code> function to form a <code>SUM IF<\/code> function that returns the total of values based on a condition.<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-string-functions\/sql-server-string_agg-function\/\" target=\"_blank\" rel=\"noreferrer noopener\">STRING_AGG<\/a>\u00a0<\/td><td>Concatenate strings by a specified separator<\/td><\/tr><tr><td><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-var\/\">VAR<\/a><\/td><td>Return the sample variance of a set of values.<\/td><\/tr><tr><td>VARP<\/td><td>Return the population variance of a set of values.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-aggregate-function-syntax'>SQL Server aggregate function syntax <a href=\"#sql-server-aggregate-function-syntax\" class=\"anchor\" id=\"sql-server-aggregate-function-syntax\" title=\"Anchor for SQL Server aggregate function syntax\">#<\/a><\/h2>\n\n\n\n<p>Here&#8217;s is the general syntax of an aggregate function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">aggregate_function_name( &#91;DISTINCT | ALL] expression)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the name of an aggregate function that you want to use such as <code>AVG<\/code>, <code>SUM<\/code>, and <code>MAX<\/code>.<\/li>\n\n\n\n<li>Second, use <code>DISTINCT<\/code> to apply aggregate distinct values in a set; or use the ALL option to apply the aggregate function to all values including duplicates. <\/li>\n\n\n\n<li>Third, specify the <code>expression<\/code> which can be a column of a table or an expression that consists of multiple columns with arithmetic operators.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-aggregate-function-examples'>SQL Server aggregate function examples <a href=\"#sql-server-aggregate-function-examples\" class=\"anchor\" id=\"sql-server-aggregate-function-examples\" title=\"Anchor for SQL Server aggregate function examples\">#<\/a><\/h2>\n\n\n\n<p>We will use the <code>products<\/code> table from the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a> for the demonstration.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"206\" height=\"169\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products.png\" alt=\"products\" class=\"wp-image-147\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='the-avg-function-example'>The AVG function example <a href=\"#the-avg-function-example\" class=\"anchor\" id=\"the-avg-function-example\" title=\"Anchor for The AVG function example\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-avg\/\">AVG()<\/a><\/code> function to return the average list price of all products in the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    <span class=\"hljs-keyword\">AVG<\/span>(list_price) avg_product_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows the output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">avg_product_price<\/span>\n<span class=\"hljs-selector-tag\">-----------------<\/span>\n1520<span class=\"hljs-selector-class\">.591401<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because the list price is in USD, it should have at most two decimal places. Therefore, you need to round the result to a number with two decimal places. <\/p>\n\n\n\n<p>To achieve this, you use the <code>ROUND<\/code> and <code>CAST<\/code> functions as demonstrated in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    <span class=\"hljs-keyword\">CAST<\/span>(<span class=\"hljs-keyword\">ROUND<\/span>(<span class=\"hljs-keyword\">AVG<\/span>(list_price),<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>))\n    avg_product_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">avg_product_price<\/span>\n<span class=\"hljs-selector-tag\">-----------------<\/span>\n1520<span class=\"hljs-selector-class\">.59<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this query: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code>ROUND<\/code> function returns the rounded average list price. <\/li>\n\n\n\n<li>Then the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-system-functions\/sql-server-cast-function\/\">CAST<\/a><\/code> function converts the result to a decimal number with two decimal places.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='the-count-function-example'>The COUNT function example <a href=\"#the-count-function-example\" class=\"anchor\" id=\"the-count-function-example\" title=\"Anchor for The COUNT function example\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-count\/\">COUNT()<\/a><\/code> function to return the number of products whose price is greater than 500:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    <span class=\"hljs-keyword\">COUNT<\/span>(*) product_count\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products\n<span class=\"hljs-keyword\">WHERE<\/span>\n    list_price &gt; <span class=\"hljs-number\">500<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">product_count\n-------------\n213<\/code><\/span><\/pre>\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a><\/code> clause gets products whose list price is greater than 500.<\/li>\n\n\n\n<li>Second, the <code>COUNT<\/code> function returns the number of products with list prices greater than 500.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='the-max-function-example'>The MAX function example <a href=\"#the-max-function-example\" class=\"anchor\" id=\"the-max-function-example\" title=\"Anchor for The MAX function example\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-max\/\">MAX()<\/a> function to return the highest list price of all products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    <span class=\"hljs-keyword\">MAX<\/span>(list_price) max_list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">max_list_price<\/span>\n<span class=\"hljs-selector-tag\">--------------<\/span>\n11999<span class=\"hljs-selector-class\">.99<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='the-min-function-example'>The MIN function example <a href=\"#the-min-function-example\" class=\"anchor\" id=\"the-min-function-example\" title=\"Anchor for The MIN function example\">#<\/a><\/h3>\n\n\n\n<p>Similarly, the following statement uses the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/sql-server-min\/\">MIN()<\/a> function to return the lowest list price of all products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    <span class=\"hljs-keyword\">MIN<\/span>(list_price) min_list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products;     <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">min_list_price<\/span>\n<span class=\"hljs-selector-tag\">--------------<\/span>\n89<span class=\"hljs-selector-class\">.99<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='the-sum-function-example'>The SUM function example <a href=\"#the-sum-function-example\" class=\"anchor\" id=\"the-sum-function-example\" title=\"Anchor for The SUM function example\">#<\/a><\/h3>\n\n\n\n<p>To demonstrate the <code>SUM()<\/code> function, we will use the <code>stocks<\/code> table from the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"194\" height=\"119\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/stocks.png\" alt=\"\" class=\"wp-image-151\"\/><\/figure>\n\n\n\n<p>The following statement uses the <code>SUM()<\/code> function to calculate the total stock by product id in all warehouses:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    product_id, \n    <span class=\"hljs-keyword\">SUM<\/span>(quantity) stock_count\n<span class=\"hljs-keyword\">FROM<\/span> \n    production.stocks\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    product_id\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    stock_count <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following picture shows the partial output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"144\" height=\"208\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Aggregate-Functions-SUM-function-example.png\" alt=\"SQL Server Aggregate Functions - SUM function example\" class=\"wp-image-1013\"\/><\/figure>\n\n\n\n<p>How the statement works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-group-by\/\">GROUP BY<\/a><\/code> clause summarized the rows by product id into groups.<\/li>\n\n\n\n<li>Second, the <code>SUM()<\/code> function calculated the sum of quantity for each group.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='the-stdev-function-example'>The STDEV function example <a href=\"#the-stdev-function-example\" class=\"anchor\" id=\"the-stdev-function-example\" title=\"Anchor for The STDEV function example\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>STDEV()<\/code> function to calculate the statistical standard deviation of all list prices:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    <span class=\"hljs-keyword\">CAST<\/span>(<span class=\"hljs-keyword\">ROUND<\/span>(<span class=\"hljs-keyword\">STDEV<\/span>(list_price),<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">as<\/span> <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>)) stdev_list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">stdev_list_price<\/span>\n<span class=\"hljs-selector-tag\">----------------<\/span>\n1612<span class=\"hljs-selector-class\">.15<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Aggregate functions operate on rows and return a single row.<\/li>\n\n\n\n<li>Use aggregate functions with the <code>GROUP BY<\/code> clause to aggregate values within groups.<\/li>\n\n\n\n<li>Use the <code>AVG()<\/code> function to calculate the average of values.<\/li>\n\n\n\n<li>Use the <code>SUM()<\/code> function to calculate the total of values.<\/li>\n\n\n\n<li>Use the <code>COUNT()<\/code> function to count the number of values in a column.<\/li>\n\n\n\n<li>Use the <code>MIN()<\/code> function to get the minimum value in a set.<\/li>\n\n\n\n<li>Use the <code>MAX()<\/code> function to get the maximum value in a set.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial introduces you to the SQL Server aggregate functions and shows you how to use them to calculate aggregates.<\/p>\n","protected":false},"author":1,"featured_media":2733,"parent":0,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1006","page","type-page","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server Aggregate Functions<\/title>\n<meta name=\"description\" content=\"This tutorial introduces you to the SQL Server aggregate functions and shows you how to use them to calculate aggregates.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server Aggregate Functions\" \/>\n<meta property=\"og:description\" content=\"This tutorial introduces you to the SQL Server aggregate functions and shows you how to use them to calculate aggregates.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-03T07:51:21+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-aggregate-functions\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-aggregate-functions\\\/\",\"name\":\"SQL Server Aggregate Functions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-aggregate-functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-aggregate-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-aggregate-functions.svg\",\"datePublished\":\"2018-10-21T08:58:10+00:00\",\"dateModified\":\"2024-04-03T07:51:21+00:00\",\"description\":\"This tutorial introduces you to the SQL Server aggregate functions and shows you how to use them to calculate aggregates.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-aggregate-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-aggregate-functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-aggregate-functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-aggregate-functions.svg\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-aggregate-functions.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-aggregate-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Aggregate Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server Aggregate Functions","description":"This tutorial introduces you to the SQL Server aggregate functions and shows you how to use them to calculate aggregates.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server Aggregate Functions","og_description":"This tutorial introduces you to the SQL Server aggregate functions and shows you how to use them to calculate aggregates.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2024-04-03T07:51:21+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/","name":"SQL Server Aggregate Functions","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-aggregate-functions.svg","datePublished":"2018-10-21T08:58:10+00:00","dateModified":"2024-04-03T07:51:21+00:00","description":"This tutorial introduces you to the SQL Server aggregate functions and shows you how to use them to calculate aggregates.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-aggregate-functions.svg","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-aggregate-functions.svg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-aggregate-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Aggregate Functions"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/1006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=1006"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/1006\/revisions"}],"predecessor-version":[{"id":3675,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/1006\/revisions\/3675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media\/2733"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}