This is the last of the lessons on functions for the 100 level. I hope you’ve enjoyed them so far, but it’s time to wrap these up!
When beginning to learn SQL, it won’t be long until you have to answer questions that require you to summarize the data. It’s one of the primary reasons SQL is used. You store many records detailing events, then you can summarize that data and report it back to users, so they don’t have to summarize it by hand.
I’m going to show you the 5 most common ways you’ll be asked to summarize data: COUNT, SUM, MAX, MIN,and AVG (Average). There are several more that SQL understands, and once you learn to define your own functions, there will be an
infinite number of ways to summarize that data. I just want you to gain an understanding of the basics, so you can go on to all those other methods.
There are three variations to this function
COUNT(*) COUNT(ALL expression) COUNT(DISTINCT expression)
The first method is the most common. You’ll use it when you want to count how many rows are in a table. If we ran the following query against our products table we’ve referred to throughout the SQL 101 lessons, we get 4, since we only defined four records.
SELECT COUNT(*) AS [count] FROM products
count ----- 4
We would get the same result from the following query.
SELECT COUNT(ALL productName) AS [count] FROM products count ----- 4
If there were duplicates in the products table, and we only wanted a count of unique values, then the following version would let us count only the unique values in the table.
SELECT COUNT(DISTINCT productName) AS [count] FROM products count ----- 4
Since there are no duplicates in our example table, we still get four.
Like COUNT, the sum function accepts ALL or DISTINCT as a modifier, but by default, it will sum all. Sum is the first function that really interprets the data. A COUNT is simple, a SUM actually does a little work for you. Warning,
make sure the column your summing is a numeric type. The sum function will try to convert the column into a numeric type, and if it finds even one value that cannot be converted, you will get an error. You may also want to consider using the ISNULL function with this, since adding NULL to something is still NULL.
SELECT SUM(price) AS [Total Price] FROM products Total Price ----------- 3.49
MAX, MIN, and AVG (average)
SQL is can be a great way to store lots of detail records. Whenever you have data, someone will eventually ask you to do some statistical analysis on that data. MAX, MIN, and average are the most common statistical numbers asked for.
SELECT MAX(price) AS [max] , MIN(price) AS [min] , AVG(price) AS [average] FROM products max min average ---- --- ------- 1.25 .25 .8725
Summarizing data will be a big part of your life as a SQL developer. These functions will serve as your first steps into learning more and more powerful ways to summarize data.
If you have any questions, please, feel free to send them in! I’m here to help you learn as much as you can about SQL.
|Previous: Calculated Fields||Next: GROUP BY|