Wednesday, March 18, 2009

The SQL SELECT Statement

The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SQL SELECT Syntax
SELECT column_name(s) FROM table_name
and
SELECT * FROM table_name
Note: SQL is not case sensitive. SELECT is the same as select.

An SQL SELECT Example
We use the following SELECT statement:


DECLARE @Persons TABLE (LogRef int, FirstName varchar(30),LastName varchar(30),Address varchar(50),City varchar(50))
INSERT INTO @Persons
SELECT 1, 'Serkan','SONMEZ','Altiparmak Mh','Bursa'
UNION SELECT 2, 'Egemen','TANIRER','Muradiye Mh.','Trabzon'
UNION SELECT 3, 'Ozkan','YAKUT','Ataevler Mh.','Ankara'
UNION SELECT 4, 'Rahmi','TABAN','Kadıkoy','Istanbul'
UNION SELECT 5, 'Didem','KIZILKAYA','Sarıyer','Istanbul'

SELECT FirstName,LastName FROM @Persons

Results:





SELECT * Example

We use the following SELECT statement:

SELECT * FROM @Persons



No comments: