|
|
 |
 |
|
Join the Discussion
|
"Share your questions and tap the
knowledge of hundreds of your peers."
Mike
Chapple
|
|
 |
 |
|
|
 |
The Structured Query Language offers database users a
powerful and flexible data retrieval mechanism -- the SELECT statement. In
this article, we'll take a look at the general form of the SELECT statement and
compose a few sample database queries together. If this is your first foray into the world of the
Structured Query Language, you may wish to review the article SQL
Fundamentals before continuing. If you're looking to design a new
database from scratch, the article Creating
Databases and Tables in SQL should prove a good jumping-off point.
Now that you've brushed up on the basics, let's begin our exploration of the
SELECT statement. As with previous SQL lessons, we'll continue to use
statements that are compliant with the ANSI SQL standard. You may wish to
consult the documentation for your DBMS to determine whether it supports
advanced options that may enhance the efficiency and/or efficacy of your SQL
code.
The general form of the SELECT statement appears below:
SELECT select_list
FROM source
WHERE condition(s)
GROUP BY expression
HAVING condition
ORDER BY expression
The first line of the statement tells the SQL processor
that this command is a SELECT statement and that we wish to retrieve information
from a database. The select_list allows us to specify the type of
information we wish to retrieve. The FROM clause in the second line
specifies the specific database table(s) involved and the WHERE clause gives us
the capability to limit the results to those records that meet the specified condition(s).
The final three clauses represent advanced features outside the scope of this
article -- we'll explore them in future SQL lessons.
The easiest way to learn SQL is by example. With that
in mind, let's begin looking at some database queries. Throughout this
article, we'll use the employees table from the fictional XYZ Corporation human
resources database to illustrate all of our queries. Here's the entire
table:
|
EmployeeID
|
LastName
|
FirstName
|
Salary
|
ReportsTo
|
|
1
|
Smith
|
John
|
32000
|
2
|
|
2
|
Scampi
|
Sue
|
45000
|
NULL
|
|
3
|
Kendall
|
Tom
|
29500
|
2
|
| 4 |
Jones |
Abraham |
35000 |
2 |
| 5 |
Allen |
Bill |
17250 |
4 |
| 6 |
Reynolds |
Allison |
19500 |
4 |
| 7 |
Johnson |
Katie |
21000 |
3 |
Retreiving an Entire Table
XYZ Corporation's Director of Human Resources receives a
monthly report providing salary and reporting information for each company
employee. The generation of this report is an example of the SELECT
statement's simplest form. It simply retrieves all of the information
contained within a database table -- every column and every row. Here's
the query that will accomplish this result:
SELECT *
FROM employees
Pretty straightforward, right? The asterisk (*)
appearing in the select_list is a wildcard used to inform the database
that we would like to retrieve information from all of the columns in the
employees table identified in the FROM clause. We wanted to retrieve all
of the information in the database, so it wasn't necessary to use a WHERE clause
to restrict the rows selected from the table. Here's what our query
results look like:
| EmployeeID |
LastName |
FirstName |
Salary |
ReportsTo |
| ---------- |
-------- |
--------- |
------ |
--------- |
| 1 |
Smith |
John |
32000 |
2 |
| 2 |
Scampi |
Sue |
45000 |
NULL |
| 3 |
Kendall |
Tom |
29500 |
2 |
| 4 |
Jones |
Abraham |
35000 |
2 |
| 5 |
Allen |
Bill |
17250 |
4 |
| 6 |
Reynolds |
Allison |
19500 |
4 |
| 7 |
Johnson |
Katie |
21000 |
3 |
In the next section of this lesson, we'll look at some more
powerful queries that allow you to restrict the information retrieved from the
database. Read
on!
Next page > Restricting
Results > Page 1, 2,
3
|