Previous Article
SQL Server Insert into one table, from another table in another database
How to create a Select Statement the proper way
Go Back To List
SQL Server
Coding Article #: 81 - Published On: July 06, 2017 @ 20:40:03 PM - Last Updated on: July 06, 2017
This article has been Favorited 0 times
Join today, and add this to your favorites.
Coding Source - Share on MySpace Coding Source - Share With Facebook Coding Source - Share on Twitter Coding Source - Share on Reddit Coding Source - Share on Digg It Coding Source - Share on Stumble Upon It Coding Source - Share on Delicious
Share With Friends (Updated 6-8-2010)

Supported Files
No Files for this Article.
No Screenshot Available
There are different ways to create a Select Statement, and some are not practical, and some are improper.
In this article, we are going to look at the wrong and the right way, to write your select statement.
Complex statements will be in another article.

Example #1:

    Select * from TableOne where ColID=1

In my professional opinion, this statement is amongst the worse way to write a select statement.
The above statement will return all rows that match the ID.
So, if there are ten columns in the TableOne, and you want to retrieve three columns, then you will waste resources, as this statement will return all columns.

The Wildcard *, has its place in SQL Statements, however. This is not one of them. There are more complex queries that are generated, that need to utilize the Wildcard. But, to get a set of records returned back, this is NOT what you want to use, and its very bad practice and some hosting providers do not allow these types of statements run on their servers.

Example #2:

    Select ColOne, ColFive, ColTen from TableOne where ColID=1

In this statement, we will only get back the records that we are querying and no more.
So, with ten columns, we are only getting back ColOne, ColFive, and ColTen.
So, this means that we will not waste any resources on the remaining columns.

Example #3: (Multiple Tables)

    SELECT TableTwo.ColOne, TableTwo.ColFour, TableTwo.ColID, TableOne.ColZero FROM TableOne INNER JOIN TableTwo ON TableOne.ColID = TableTwo.ColID WHERE TableTwo.ColTwo=?

In this statement, we are calling multiple columns from two tables. We are only wanting three columns from TableTwo, and one column from TableOne.
If TableOne has 10 Columns
If TableTwo has 8 Columns, and we use the Wildcard *, then we would waste resources returning all 18 columns, when we only need four columns, in our query to be retrieved.
Post to Facebook about: How to create a Select Statement the proper way