Database Reference
In-Depth Information
How It Works
Let's look at the clauses individually. The
SELECT
list specifies which columns you want to use.
select AddressID, AddressLine1, City
The
FROM
clause specifies that you want to use the
Address
table.
from Person.Address
The
WHERE
clause specifies that you want all the records for Redmond.
where City = 'Redmond'
The
ORDER BY
clause specifies the order in which the rows are sorted. The rows will be sorted by
AddressID in ascending order.
order by AddressID asc
GROUP BY Clause
The
GROUP BY
clause is used to organize output rows into groups. The
SELECT
list can include aggregate
functions and produce summary values for each group. Often you'll want to generate reports from the
database with summary figures for a particular column or set of columns. For example, you may want to
find out the total quantity of addresses that belong to a particular city from the
Person.Address
table.
Try It: Using the GROUP BY Clause
The Person.Address table contains the address details. You want to know the total of how many
addresses belong to a particular city. For example, if you look at the query and number of records in
Figure 5-6, you will notice that there are a total of 121 address entries for Redmond.
Open a New Query window in SQL Server Management Studio Express. Enter the following query,
and click Execute. You should see the results shown in Figure 5-7.
Select City, Count(City) As 'Total Count'
from Person.Address
Group By City
Order By City Asc