IT tutorials
 
Technology
 

SQL Server 2012 : Introducing Basic Query Flow - FROM Clause Data Sources

4/19/2014 1:46:23 AM
- Windows 10 Product Activation Keys Free 2019 (All Versions)
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

The first 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 maximum 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 flexible 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 . 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.
  • Table-valued user-defined functions return rows and columns.
  • 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 distributed functions.
  • Full-text search can return data sets with information about which rows contain certain words.
  • Pivot creates a crosstab within the FROM clause .
  • XML data sources using XQuery.
  • Row constructors build hard-coded rows using the values() clause.
  • 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.

Table Aliases

You can assign a data source a table alias within the FROM clause. When the data source has an alias, you must refer to it by this new name. In some cases the data source must have an alias. The following code accesses the Person table but refers to it within the query as table P:

-- From Table [AS] Table Alias
USE AdventureWorks;
SELECT
P.LastName,P.FirstName
FROM Person.Person AS P;

In some cases the data source must have an alias. For example, when writing a query that has a subquery as the source, you must specify an alias. The following code accesses a subquery that has an alias of SQ.

--From Subquery [AS] Alias
USE AdventureWorks
SELECT
SQL.LastName, SQL.FirstName
FROM
(
SELECT LastName, FirstName from Person.Person
) AS SQL;

Best Practice
Using the keyword AS to assign an alias to a column or data source is optional and is commonly ignored. However, this practice leads to errors in the query, as seen regularly in SQL Server newsgroups. As a rule, always include the AS keyword.

Note
In SQL, the USE command specifies the current database. It's the code version of selecting a database from the toolbar in Management Studio.

[Table Name]

If the name of a database object, such as a table or column name, conflicts with a SQL reserved keyword, you can let SQL know that it's the name of an object by placing it inside square brackets. The square brackets are specific to SQL Server and not part of the ANSI SQL standard. For example, if you have a database (DB1) that contains a table named [Order] you would be required to place the table name inside square brackets. The following code illustrates this:

USE DB1;
SELECT OrderID, OrderDate
FROM [Order];

Although it's an incredibly poor practice to include spaces within the names of database objects, it is possible nevertheless. If this is the case, square brackets are required when specifying the database object. Again, assuming you work in database DB1 and it contains a table name Order Details, you would query the table using the following code:

USE DB1;
SELECT OrderID, ProductID, Quantity
FROM [Order Details];
Caution
The collation of the database determines its character set and sort order, but it can also affect object names within SQL statements. If the collation is case-sensitive, then the object names must be case-sensitive as well. For example, with a case-sensitive collation, a table created with the name Person is different from person or PERSON. As a result, when referencing these objects in queries, you must adhere to any case-sensitivity specified for that object name.

Fully Qualified Names

The full and proper name for a table is not just the table name but a fully qualified name, sometimes informally referred to as the four-part name:

Server.Database.Schema.Table

If the table is in the current database, then the server and database name are not required, so when SQL Server developers talk about a qualified table name, they usually mean a two-part table name:

Schema.Table


Best Practice
Using the two-part name, (that is, the schema and object name) is sufficient and the best practice. Including the server and database name would restrict moving code from one server to another (for example, from development to production). The following code shows an example of using a two-part name:
USE DB1;
SELECT * FROM dbo.[Order]

In addition to writing cleaner code, using the qualified name has two specific benefits:

  • The same table may exist in multiple schemas. If this is the case, then the schema selected is based on the user's default schema. Qualifying the name avoids accidentally using the wrong table.
  • Qualified table names are required for the Query Engine to reuse the query execution plan, which is important for performance.
 
Others
 
- SQL Server 2012 : Understanding Query Flow (part 2) - Logical Flow of the Query Statement, Physical Flow of the Query Statement
- SQL Server 2012 : Understanding Query Flow (part 1) - Syntactical Flow of the Query Statement, A Graphical View of the Query Statement
- Windows Server 2012 : Deploying and configuring virtual machines (part 5) - Configuring virtual machine management
- Windows Server 2012 : Deploying and configuring virtual machines (part 4) - Adding and configuring virtual network adapters
- Windows Server 2012 : Deploying and configuring virtual machines (part 3) - Configuring virtual machines - Adding virtual disks
- Windows Server 2012 : Deploying and configuring virtual machines (part 2) - Creating virtual machines
- Windows Server 2012 : Deploying and configuring virtual machines (part 1) - Planning virtual machine deployment
- Sharepoint 2013 : Manage Tags and Notes
- Sharepoint 2013 : Follow Colleagues to See What They Are Posting
- Sharepoint 2013 : Use Microblogging to Let Others Know What You Are Doing or Thinking
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS