sqlite
  1. sqlite-aggregate-functions

SQLite Aggregate Functions

SQLite provides a variety of aggregate functions, which can be used to perform calculations on a set of values and return a single result. These functions are useful in scenarios where you need to calculate summaries, averages or totals from a large set of data in a SQLite table.

Syntax

The syntax for using aggregate functions in SQLite is as follows:

SELECT aggregate_function(column_name) 
FROM table_name

The most commonly used aggregate functions are:

  • SUM(): Calculates the sum of values in a given column.
  • AVG(): Calculates the average of values in a given column.
  • COUNT(): Returns the number of rows in a table or number of non-null values in a column.
  • MAX(): Returns the highest value in a given column.
  • MIN(): Returns the lowest value in a given column.

Example

Suppose we have a table called sales with columns for product name and sales figures for each product by month. We can use a SUM function to calculate the total sales figures for each product over a year-long period.

SELECT product_name, SUM(sales_jan + sales_feb + sales_mar + sales_apr + sales_may + sales_jun + sales_jul + sales_aug + sales_sep + sales_oct + sales_nov + sales_dec) AS total_sales
FROM sales
GROUP BY product_name;

Output

The output of the above query would be a table with the product name and the total sales figure for each product over a year-long period.

| product_name | total_sales |
|--------------|-------------|
| product1     | 100000      |
| product2     | 200000      |
| product3     | 150000      |

Explanation

In the example above, we have a table called sales with columns for product name, and monthly sales figures for each product. We use the SUM function to add up all sales figures for each product over a year-long period.

The GROUP BY keyword is used to group the sales figures by product name. This allows us to calculate the total sales figures for each product separately.

Use

Aggregate functions in SQLite are useful for calculating summaries, totals, and averages from large sets of data. They can be used to analyze data to gain insights into your business, find trends, and make informed decisions.

Important Points

  • When using aggregate functions, you should always use the GROUP BY keyword to get a meaningful result.
  • You can use aggregate functions with multiple columns in the SELECT statement. In such cases, you should always use the GROUP BY keyword on all non-aggregate columns.
  • NULL values are ignored by most aggregate functions except for COUNT(), which will count NULL values.
  • The result of an aggregate function is automatically assigned an alias which can be used in the SELECT statement.

Summary

In this tutorial, we learned about using aggregate functions in SQLite to calculate summaries, totals, and averages from large sets of data. We saw examples of using SUM and GROUP BY statements to compute the total sales figures for each product in a table. Aggregate functions are an essential tool in data analysis and can be used in conjunction with other SQL statements to extract insights and make informed decisions.

Published on: