Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

111


CHAPTER


6


Introducing Basic Query Flow


IN THIS CHAPTER


Using Understanding Query Flow

Using FROM Clause Data Source

Using WHERE conditions

Using Columns, Starts, Aliases, and Expressions

Ordering the Result Set

Using SELECT Distinct

Using TOP()

S


tructured Query Language (SQL), or more specifi cally Transact SQL (T-SQL) for SQL Server, is
the romance language of data. Extracting or querying the single correct answer from giga-
bytes of relational data can seem overwhelming. This is until you master the logical fl ow of
the query.

One of the fi rst points to understand is that SQL is a declarative language. This means that the SQL
query logically describes the question to the SQL Query Optimizer, which then determines the best
method to physically execute the query. This method of execution is the query execution plan.
As covered in the next eight chapters, you can often construct the query in many ways, but you
can optimize each method to the same query execution plan. However, in some cases, queries that
produce the same result set may use different execution plans. As a result, you can express the
SQL query in the way that is most comprehensible and the easiest to maintain. In some cases, one
method may be more effi cient and faster than the other.

SQL queries aren’t limited to the SELECT command. The four Data Manipulation Language (DML)
commands, SELECT, INSERT, UPDATE, and DELETE, are sometimes taught as four separate and
distinct commands. However, think of queries as a single structural method of manipulating data;
in other words, use the four commands as four verbs, using each with the full power and fl exibility
of the SQL.

In addition to SQL queries’ elegance and fl exibility, coining and executing SQL queries are not limited
to one graphical user interface. Many SQL developers who came up through the ranks from Microsoft

c06.indd 111c06.indd 111 7/30/2012 4:15:55 PM7/30/2012 4:15:55 PM


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