Welcome to Lesson 13 of the TSQL Tutorial. In this lesson we will cover select group by statements. I hope you enjoy the lesson.
Select Statements
In the first lesson we showed how to use the select statement. In this lesson we
will build on the select statement by grouping items together. All items in the
select statement must either be contained in a group by clause or in an aggregate
function (i.e. count, sum, etc...)
Usage:
SELECT column(s)
FROM table
GROUP BY field
Example:
SELECT count(EmployeeID), Title
FROM employees
GROUP BY Title
Results:
|
|
Title
|
|
6
|
Sales Representative
|
|
1
|
Vice President, Sales
|
|
1
|
Sales Manager
|
|
1
|
Inside Sales Coordinator
|
Aggregate functions in SQL Server
|
Function |
Description |
|
AVG(column) |
Returns the average value of a column |
|
BINARY_CHECKSUM |
|
|
CHECKSUM |
|
|
CHECKSUM_AGG |
|
|
COUNT(column) |
Returns the number of rows (without a NULL value) of a column |
|
COUNT(*) |
Returns the number of selected rows |
|
COUNT(DISTINCT column) |
Returns the number of distinct results |
|
FIRST(column) |
Returns the value of the first record in a specified field (not supported in SQLServer2K) |
|
LAST(column) |
Returns the value of the last record in a specified field (not supported in SQLServer2K) |
|
MAX(column) |
Returns the highest value of a column |
|
MIN(column) |
Returns the lowest value of a column |
|
STDEV(column) |
|
|
STDEVP(column) |
|
|
SUM(column) |
Returns the total sum of a column |
|
VAR(column) |
|
|
VARP(column) |
|