SELECT Statement

The old famous or should I say unfamous SELECT statement is one of the most popular statement in the SQL world and used in almost all database systems. Like all modren RDBMS vendors Microsoft have introduced its own version of select statment in T-SQL (Transact SQL).

The SELECT statement have clauses to choose, but FROM is the only required clause. These clauses have a variety of selection of options, parameters, etc. The databases will be useless with out SELECT statement, the data stored in database is retrieved using SELECT. A basic SELECT statement consist of two parts, first part where you specify columns and second part you specify the object (tables, views, etc).

SELECT in simple form

1. SELECT Column1, Column2 FROM TableName
2. SELECT * FROM TableName

The first query will retrieve only two columns (column1 and column2) from the table, the second query will retrieve all the columns from the table.

WHERE clause

The WHERE clause in SQL statement is used to filter the results, the WHERE clause use operators to filter the results and get the information you are requesting. The WHERE clause is also widely used with UPDATE, DELETE statements. There are two type of operators in WHERE one are comparision operators and other are logical operators. You can also combine more than one operator in single SELECT statement.

WHERE clause operators

Operator Explaination
= Compare two values, if they are equal (A = A)
<> Compare two expressions if they are NOT equal (A <> B)
> If value on left side is greater then value on right side.( 2 > 1)
< If value on left side is less then value on right side (2 < 3)
>= If value on left side is greater then or equal to value on right side.
<= If value on left side is less then or equal to value on right side.

Operator Explaination
AND Combines two expressions, the statements on both side must be true.
OR Combines two expressions, but one of them can be true.
NOT Used to eliminate values in where clause.
LIKE Pattern matching, " LIKE '%A%' the percentage sign is used as wild card.
BETWEEN Checks if value is between given range.

1. SELECT First_Name, Last_Name FROM Customers WHERE Last_Name = 'John'

2. SELECT First_Name, Last_Name FROM Customers WHERE Last_Name = 'John' AND Age = 23

The first query will show all the records in "Customers" table with Last_Name John, the second query will also show all the customers with Last_Name with John but will only show customers with age of 23. Like the second query we can use combination of more than one operator in single SQL statement.




Add Comments

Name: *
Email: *
URL:
Comments: *
 


Advertisements

MySQL HOSTING
MySQL hosting provider, clustering and replication supported. DBA Services Included.

SQL SERVER HOSTING
Reliable, powerful SQL Server 2008 hosting with ASP.NET on Windows 2008 Servers.

SQL Server DBA

I am a SQL Server DBA with almost 9 years of experience in database technologies.

 I am again in process of redesigning this website using ASP.NET 2.0 and AJAX. The main purpose of new SQL DBA website is to offer more features and make it easy for me to update contents on regular basis.