IT tutorials
 
Technology
 

SQL Server 2012 : Introducing Basic Query Flow - (0 row(s) affected)Columns, Stars, Aliases, and Expressions

4/19/2014 1:48:58 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 title of this section may read like a bad tabloid headline, but in all seriousness it means that the SQL SELECT statement returns columns in the order in which you list them in the SELECT statement. The column may be any expression or any column in the FROM clause.

Following the FROM clause and the WHERE clause, the next logical step in the query is the list of returned expressions.

The Star

The *, commonly called “star,” is a special wildcard that includes all columns in their table order. If the query pulls from multiple tables, the * includes all columns from every table. Alternatively, tablename.* includes only the columns from the named table. Also, if you alias any tables in the FROM clause, you can return a complete column list of a specific example by using table alias.*.

Aliases

The name of the column in the underlying table becomes the name of the column in the result set. Optionally, you can change the column name using a column alias.

Expressions and constants have a blank column heading in the result set unless an alias is provided.

The AS keyword is optional, but just as with a table alias, using it is a good practice that improves the readability of the code and helps prevent errors.

To use an alias that's identical to a SQL Server keyword or that includes a space, enclose the alias in square brackets, single quotes, or double quotes. Although the square brackets are not technically required if the alias is the same as an object name (that is, table or column name), you an explicitly specify that the alias is not a keyword.

The following code demonstrates adding aliases to columns:

USE AdventureWorks;
SELECT
Name AS ProductName,
‘abc',
SellStartDate + 365 OneYearSellStartDate
FROM Production.Product;

Result:

ProductName           OneYearSaleStartDate
-------------------------- ---- ------------------------
Adjustable Race abc 2003-06-01 00:00:00.000
Bearing Ball abc 2003-06-01 00:00:00.000
BB Ball Bearing abc 2003-06-01 00:00:00.000
. . .

The first column's name is changed from Name to ProductName by means of an alias. The second column is an expression without an alias, so it has no column name. A better practice is to name expression columns using an alias, as demonstrated in the third column.

Accidental aliases are a common source of errors. Take a careful look at the next query:

USE AdventureWorks;
SELECT
Name
‘abc',
SellStartDate + 365 OneYearSellStartDate
FROM Production.Product;

Result:

abc            OneYearSaleStartDate
------------------------- -------------------------
Adjustable Race 2003-06-01 00:00:00.000
Bearing Ball 2003-06-01 00:00:00.000
BB Ball Bearing 2003-06-01 00:00:00.000.. .

The second column isn't abc as in the previous query. Instead, because of a missing comma, the ‘abc' in the query became an accidental alias for the first column.


What's Wrong with Select *?
For some developers, using SELECT * is common practice. However, this method of programming can pose a few challenges. If you talk to five SQL Server DBAs, it is likely that you get five different reasons not to use the SELECT *. On the other hand, if you talk to five SQL developers, they might give you five different reasons to use SELECT *. Basically you should avoid using it for several reasons, but if you are ever faced with the challenge of validating, here are a few reasons why:
  • It is a waste of resources. In most cases more data is returned than needed.
  • If you use a SELECT * in your query, how do you build a covering index to satisfy the needs of the query?
  • Adding a column to the table could potentially break the application.
These are only a few, but there are many more and they all are valid. Just a quick Internet search should reveal a list that provides enough ammunition for any DBA.

Qualified Columns

A common problem with queries is that column names are duplicated in multiple tables. Including the column in the select list by column names alone can cause an “ambiguous column name” error. Basically, SQL Server complains that it doesn't know which column you refer to. Even if they contain the same exact data, SQL Server must know which column to select.

CREATE TABLE t1 (col1 INT);
CREATE TABLE t2 (col1 INT);

SELECT col1
FROM t1
CROSS JOIN t2;

Result:

Msg 209, Level 16, State 1, Line 2
Ambiguous column name ‘col1'.

The solution, of course, is to qualify the column name by including the table:

SELECT t1.col1
FROM t1
CROSS JOIN t2;
 
Others
 
- SQL Server 2012 : Introducing Basic Query Flow - WHERE Conditions (part 2) - Using the LIKE Search Condition
- SQL Server 2012 : Introducing Basic Query Flow - WHERE Conditions (part 1) - Comparing with a List
- SQL Server 2012 : Introducing Basic Query Flow - FROM Clause Data Sources
- 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
 
 
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