Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

117


Chapter 6: Introducing Basic Query Flow


6


The rest of this chapter walks through the logical order of the basic query.

FROM Clause Data Sources


The fi rst logical component of a typical SQL SELECT statement is the FROM clause. In a simple
SQL SELECT statement, the FROM clause contains a single table. However, the FROM clause
can also combine data from multiple sources and multiple types of data sources. The maxi-
mum number of tables that may be accessed within a single SQL SELECT statement is 256.

The FROM clause is the foundation of the rest of the SQL statement. For a table column to
be in the output, accessed in the WHERE conditions, or in the ORDER BY, it must be in the
FROM clause.

Possible Data Sources
SQL is extremely fl exible and can accept data from 10 distinctly different types of data
sources within the FROM clause:

■ (^) Local SQL Server tables.
■ Subqueries serving as derived tables, also called subselects or in-line views (see
Chapter 9). Common table expressions (CTEs) are functionally similar to subqueries
but may be referenced multiple times within the query.
■ (^) You can reference views or stored SELECT statements within the FROM clause as if
they were tables. Chapter 11, “Projecting Data through Views,” discusses views.
■ (^) Table-valued user-defined functions return rows and columns. See Chapter 18,
“Building User- Defi ned Functions,” for more information.
■ (^) Distributed data sources pull in data from other SQL Server databases, other
SQL Servers, other database platforms (for example, Microsoft Access, Oracle, and
Foxpro), or applications (for example, Excel) using openquery() and other distrib-
uted functions, as detailed in Chapter 15, “Executing Distributed Queries.”
■ (^) Full-text search can return data sets with information about which rows contain
certain words.
■ (^) Pivot creates a crosstab within the FROM clause and is covered in Chapter 10.
■ XML data sources using XQuery, as discussed in Chapter 14, “Using XML Data.”
■ (^) Row constructors build hard-coded rows using the values() clause, as covered in
Chapter 9.
■ (^) Inserted and deleted virtual tables from an insert, update, or delete can be passed
to an outer query in the form of a subquery using the output clause.
c06.indd 117c06.indd 117 7/30/2012 4:15:59 PM7/30/2012 4:15:59 PM
http://www.it-ebooks.info

Free download pdf