Learning About SQL Aggregate Functions
See more tutorials in Database. This post has Comments Off on Learning About SQL Aggregate Functions.
This Tutorial will teach you about all of the basic Aggregate functions in SQL. These functions can be used to perform mathematical equations to SQL columns and allow you to pull this Data out of the table.
Step 1. List of the Aggregate Functions
1. AVG() – Returns the average value
2. COUNT() – Returns the number of rows
3. FIRST() – Returns the first value
4. LAST() – Returns the last value
5. MAX() – Returns the largest value
6. MIN() – Returns the smallest value
7. SUM() – Returns the sum of all rows
All of these functions can be used in correspondence with the GROUP BY Function.
Step 2. Basic Uses of the Aggregate Functions
The following SQL Queries are examples of how these Functions can be used.
1. In a SELECT Statement Grabbing the Sum of a Column
SELECT SUM(column_name) FROM table_name
This command will add up all of the numbers in a column and return them
2.Using the AS statement to name the returned value
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
when trying to use any of the numbers created it is always best to use the AS function. The AS function names the number that is returned so that you can uses it when you like.
3. In a Where Clause Grabbing the Above average Customers
SELECT Customer FROM Orders
WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)
This Function will grab the
are greater than the Average number of OrderPrices. Notice the second SELECT Command is perfectly legal, this allows you to chain SELECT Commands to narrow down your results.
In this Tutorial we learned all about Aggregate Functions and how they can be used to simplify Mathematical equations on Table columns. We also learned about their possible placements in SQL Queries and where they can be useful.