Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

113


Chapter 6: Introducing Basic Query Flow


6


When you include the FROM portion of the SELECT statement, it assembles all the data
sources into a result set, which the rest of the SELECT statement uses. You can represent
these data sources in many ways. This chapter represents them only as tables. Within the
FROM clause, you can reference multiple tables by using one of several types of joins.

The WHERE clause acts upon the record set assembled by the FROM clause to fi lter certain
rows based upon conditions. You can specify several conditions in the WHERE clause. This
chapter discusses some of these conditions later.

Aggregate functions perform summation-type operations across the data set. The GROUP BY
clause can group the larger data set into smaller data sets based on the columns specifi ed in
the GROUP BY clause. The aggregate functions are then performed on the new smaller groups
of data. You can restrict the results of the aggregation using the HAVING clause.

Finally, the ORDER BY clause determines the sort order of the result set. You can sort the
data in ascending or descending order based on one or more columns in the list of columns
from the SELECT statement. If you do not specify a sort order, the default is ascending.

A Graphical View of the Query Statement
SQL Server Management Studio includes two basic methods to construct and submit que-
ries: Query Designer and Query Editor. Query Designer offers a graphical method to build
a query, whereas Query Editor is an excellent tool for writing SQL code or ad hoc data
retrieval because there are no graphics to get in the way, and the developer can work as
close to the SQL code as possible. Even further, limiting yourself to the Query Designer may
limit your ability to fully understand the SQL programming language and how to debug any
potential errors you may encounter.

From SQL Server’s point of view, it doesn’t matter where the query originates; each state-
ment is evaluated and processed as a SQL statement.

When selecting data using the Query Designer, you can enter the SQL statements as raw
code in the third pane, as shown in Figure 6-1. The bottom pane displays the results in Grid
mode or Text mode and displays any messages. The Object Browser presents a tree of all the
objects in SQL Server, as well as templates for creating new objects with code.

If you select text in the Query Editor, then only the highlighted text is submitted to SQL Server when you press the
Execute command button or the F5 key. This is an excellent way to test single SQL statements or portions of SQL code.

Although it may vary depending on the user account settings, the default database is probably the master database.
Be sure to change to the appropriate user database using the database selector combo box in the toolbar, or the USE
database command.
The best solution is to change the user’s default database to a user database and avoid master altogether.

c06.indd 113c06.indd 113 7/30/2012 4:15:56 PM7/30/2012 4:15:56 PM


http://www.it-ebooks.info
Free download pdf