Working with Aggregates Flashcards
What’s an aggregate function?
These are functions that perform a calculation on a set of values and return a single value. Examples include SUM, COUNT, AVG (average), MIN (minimum), and MAX (maximum).
In databases, when you have a lot of data and you want to summarize it or get some statistics (like totals, averages, counts, etc.) for groups of related data, what would you use?
Aggregates and Grouping
List everything you need to know about Aggregates and Grouping
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
GROUP BY comes after the WHERE clause (if there is one) and before the ORDER BY clause
Items in the SELECT statement that aren’t aggregated must be included in the GROUP BY clause
Multiple aggregate functions can be applied to a single query
Different columns can be aggregated in the same query
If you use an ORDER BY clause the column(s) must be included in the GROUP BY clause
What does the count() function do?
The COUNT function counts the total occurrences of the specified column in a record set
What happens when you use an asterisk with the count function?
The function counts the total number of rows
List what you know about the SUM() function.
The SUM function adds together the numbers in a grouping
You insert the DISTINCT keyword if you want to only add
distinct values
SUM will add both positive and negative values
List what you know about the AVG() function
The AVG function returns the average of
values in a group
Null values are ignored
List what you know about the Min() and Max() function
The MIN function returns the minimum value in a column
The MAX function returns the maximum
value in a column
Both functions can work on string as well as numeric values
When can you aggregate data without the GROUP BY function?
The GROUP BY clause is not used if you are grouping
against an entire result set or table
If there are no non-aggregated columns then you don’t need the GROUP BY clause
List what you know about Aggregates and NULL
List what you know about the aggregate functions will
exclude NULL values from their calculations
If you want to include NULL values then you will need to enclose the column in an ISNULL scalar function
List what you