Databases Reference
In-Depth Information
4
Chapter
Restricting, Sorting, and
Grouping Data
In This Chapter
Using the
WHERE
clause to
◆
restrict results
Using the
ORDER BY
clause to
◆
sort data
Using the
GROUP BY
clause
◆
Using group functions to aggregate data
◆
Unless your database tables are very small, or your data reporting needs
are very limited, you will want to restrict the rows returned from your
queries. In cases where you want to see the results of the queries in a par-
ticular order, you will want to sort the results. Grouping the data—for
example, grouping sales figures by month, salary totals by department,
and so forth—can be done in conjunction with restricting and sorting the
data in a SQL statement.
Scott's widget company has been growing by leaps and bounds over
the past few months, and it has expanded to international locations.
While Scott has enjoyed being the data analyst and DBA, he has turned
over these roles to Janice. The employee-related database tables have
been redesigned and turned over to the HR department. All of our exam-
ples from this point on will use the HR schema, which contains the fol-
lowing tables:
Using the
HAVING
clause to restrict
◆
the groups returned
COUNTRIES
,
DEPARTMENTS
,
EMPLOYEES
,
JOBS
,
JOB_
HISTORY
,
LOCATIONS
, and
REGIONS
. The names of these tables should be
self-explanatory.