Consider the following example table of employee information:
EmployeeID |
FirstName |
LastName |
Department |
Age |
JoinDate |
1001 |
Phil |
Roach |
TECH |
24 |
23/1/87 |
1002 |
Chris |
England |
TECH |
36 |
1/10/86 |
1003 |
Andreas |
Smith |
SALES |
25 |
18/6/90 |
1004 |
Jim |
Smith |
ADMIN |
30 |
10/3/92 |
1005 |
Julia |
Allan |
SALES |
25 |
26/9/91 |
To select the entire table, the SQL command is:
SELECT EmployeeID, FirstName, LastName, Department, Age, JoinDate FROM Employee;
This would return every row and column from the table. The two SQL keywords are SELECT and FROM. Note that every SQL statement ends with a semi-colon ";".
A shorter alternative expression is:
SELECT * FROM Employee;
The asterisk simply stands for all, or every, column.
To select only names and departments, use:
SELECT FirstName, LastName, Department FROM Employee;
Note that you can re-arrange the order of the columns by supplying the field list in a different order. For example:
SELECT Department, FirstName, LastName FROM Employee;
This would give the following results table (or query):
Department |
FirstName |
LastName |
TECH |
Phil |
Roach |
TECH |
Chris |
England |
SALES |
Andreas |
Smith |
ADMIN |
Jim |
Smith |
SALES |
Julia |
Allan |
If your table or field names include spaces, you’ll need to surround them with square brackets as follows:
SELECT [field number 1] FROM [My test table];
See also: