The basic or the first statement in T-SQL is the 'SELECT' statement. It is
used to retrieve records from the database. It consists of 2 parts, the
columns that you are selecting as well as from which table(s).
The SELECT Statement
The 'SELECT" statement helps you select data from one or more tables. When you
want to retrieve all the columns of a table, you can use the '*' clause in your
'SELECT' statement.
SQL SELECT Syntax
Select *
FROM table_name
The SELECT Statement Example
The
'Employees' tables contains the following columns and their respective data type as shown
below.
EmployeeID |
smallint |
Not Null |
EmployeeName |
varchar(50) |
Not Null |
DateOfBirth |
smalldatetime |
Not Null |
PhoneNo |
nchar(10) |
Allows Null |
City |
varchar(50) |
Allows Null |
Salary |
decimal(5, 2) |
Allows Null |
Select * from
Employees
The
"Select * from Employees" statement displays all the columns and data from the
'EMPLOYEES'
table as shown
below.
1 |
Richard Hughes |
4/23/1945 12:00:00 AM |
343-123-2002 |
New Orleans |
950.00 |
2 |
Taryn Sinclair |
3/22/1980 12:00:00 AM |
232-344-8755 |
San Francisco |
800.00 |
3 |
Ted Horowitz |
3/31/1960 12:00:00 AM |
435-657-9068 |
New York |
700.00 |
4 |
Jonathan Douglas |
7/7/1971 12:00:00 AM |
323-454-7656 |
Salt Lake City |
600.00 |
5 |
Miranda Leigh |
7/19/1983 12:00:00 AM |
634-465-8046 |
New York |
500.00 |
6 |
Jana Rae |
9/23/1976 12:00:00 AM |
434-434-1254 |
Houston |
400.00 |
7 |
Lita Rosanna |
9/14/1982 12:00:00 AM |
565-782-1223 |
Long Island |
650.00 |
8 |
Colin Flooks |
12/29/1988 12:00:00 AM |
954-652-2111 |
Salt Lake City |
600.00 |
9 |
Anthony Frank |
2/22/1988 12:00:00 AM |
323-243-1249 |
San Francisco |
750.00 |
10 |
Stephanie Lynn |
7/30/1979 12:00:00 AM |
543-765-4653 |
Salt Lake City |
450.00 |
11 |
Jack Martin |
8/25/1985 12:00:00 AM |
443-324-6522 |
Austin |
500.00 |
Select EmployeeID, EmployeeName, DateOfBirth, PhoneNo, City, Salary
from
Employees
The above
'SELECT" statement also brings out all the columns and data from the 'EMPLOYEES'
table as you have explicitly called every column. This type of explicit calling
is preferred to use of '*' in 'SELECT' statements, the reason we shall see later
on as we progress.
1 |
Richard Hughes |
4/23/1945 12:00:00 AM |
343-123-2002 |
New Orleans |
950.00 |
2 |
Taryn Sinclair |
3/22/1980 12:00:00 AM |
232-344-8755 |
San Francisco |
800.00 |
3 |
Ted Horowitz |
3/31/1960 12:00:00 AM |
435-657-9068 |
New York |
700.00 |
4 |
Jonathan Douglas |
7/7/1971 12:00:00 AM |
323-454-7656 |
Salt Lake City |
600.00 |
5 |
Miranda Leigh |
7/19/1983 12:00:00 AM |
634-465-8046 |
New York |
500.00 |
6 |
Jana Rae |
9/23/1976 12:00:00 AM |
434-434-1254 |
Houston |
400.00 |
7 |
Lita Rosanna |
9/14/1982 12:00:00 AM |
565-782-1223 |
Long Island |
650.00 |
8 |
Colin Flooks |
12/29/1988 12:00:00 AM |
954-652-2111 |
Salt Lake City |
600.00 |
9 |
Anthony Frank |
2/22/1988 12:00:00 AM |
323-243-1249 |
San Francisco |
750.00 |
10 |
Stephanie Lynn |
7/30/1979 12:00:00 AM |
543-765-4653 |
Salt Lake City |
450.00 |
11 |
Jack Martin |
8/25/1985 12:00:00 AM |
443-324-6522 |
Austin |
500.00 |
SQL is NOT case sensitive.You can write it as 'SELECT' or 'select'. It operates in the same manner.