Record Selection
The most basic SQL statements are constructed through selection statements. Selection statements return a group of records whose values can be filtered, grouped, ordered, or altered. To jump into the basics of SQL, you must first learn how to create a simple selection query.
Selection Statements
Statements that select records all begin with the keyword
SELECT. The syntax for a simple select statement
is as follows:
SELECT [predicate] {* |
field_name1 [AS alias_name1]
[field_name2 [AS alias_name2]] [, ...]]}
FROM table_expression
[IN external_database]This syntax declaration might be slightly intimidating if you have never seen SQL before, but rest assured, you will know this like the back of your hand before long.
Let’s begin by taking a look at the simplest of all select
statements, one without any predicate (or secondary keyword), the
SELECT statement.
SELECT
The SELECT
statement is used to return
selected fields from all records, in one or more tables, located in
the current database or an external database. Let’s image that
we have a table named Employees in our current
database. The Employees table has the following
field names: FirstName, LastName, and EmployeeID. The
Employees table is populated as in Table A-1.
Table A-1. The Employees Table
|
FirstName |
LastName |
EmployeeID |
|---|---|---|
|
Jason |
Roff |
100 |
|
Tammi |
Roff |
101 |
|
Kimberly |
Roff |
102 |
|
John |
DelWhatta |
200 |
|
John |
Katsosing |
201 |
By using a simple SELECT statement, we can list
all of the employees and their IDs:
SELECT ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access