SQL > SQL Functions

Since we have started dealing with numbers, the next natural question to ask is if it is possible to do math on those numbers, such as summing them up or taking their average. The answer is yes! SQL has several arithematic functions, and they are:

Key Takeaway: Aggregate functions summarize data across multiple rows. They are almost always used with GROUP BY to compute metrics per group, but can also be applied to the entire table when no grouping is specified.
SQL FunctionDescription
 AVG Average of the column.
 COUNT Number of records.
 MAX Maximum of the column.
 MIN Minimum of the column.
 SUM Sum of the column.
 ROUND Round a number to s specified precision.

The syntax for using functions is,

SELECT "function type" ("column_name")
FROM "table_name";

Examples of how these functions are used are presented individually in the next few pages.

In addition to using functions, it is also possible to use SQL to perform simple tasks such as addition (+) and subtraction (-). For character-type data, there are also several string functions available, such as concatenation, trim, and substring functions. Different RDBMS vendors have different string functions implementations, and it is best to consult the references for your RDBMS to see how these functions are used.

Frequently Asked Questions

Do aggregate functions ignore NULL values?

Yes, with one exception. SUM, AVG, MAX, MIN, and COUNT(column) all ignore NULL values. COUNT(*) is the exception — it counts every row regardless of NULLs.

What is the difference between AVG and SUM/COUNT?

AVG is equivalent to SUM divided by COUNT (excluding NULLs). Using AVG is cleaner, but SUM/COUNT gives you flexibility — for example, you can use COUNT(*) to include NULLs in the denominator.

Can I use multiple aggregate functions in one query?

Yes. For example: SELECT Store_Name, SUM(Sales), AVG(Sales), COUNT(*) FROM Store_Information GROUP BY Store_Name calculates three aggregates at once.

Can aggregate functions be used in the WHERE clause?

No. Use HAVING instead. WHERE filters individual rows before aggregation; HAVING filters grouped results after aggregation. WHERE SUM(Sales) > 1000 is invalid — use HAVING SUM(Sales) > 1000.

Next: SQL Average

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact