AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL Aggregate Functions |
|
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.
The syntax for using functions is, 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 QuestionsDo 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: 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. |
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.