Using the SELECT Statement
The most common of all SQL queries is the SELECT query. This query is generally
constructed using a SELECT clause and a FROM clause. To understand this concept
more clearly, take a look at the following statement, which retrieves all columns
of all records in the Departments table:
SELECT *
FROM Departments
In this case, the SELECT clause lists the columns that you want to retrieve. In this
case, we used *, which means “all columns.” The FROM clause specifies the table
from which you want to pull the records. Together, these two clauses create an
SQL statement that extracts all data from the Departments table.
You’ve probably noticed that the two clauses appear on separate lines. If you
wanted to keep the entire statement on one line, that’s fine, but SQL lets you
separate the statements on multiple lines to make complex queries easier to read.
Also note that although SQL is not actually a case-sensitive language, we’ll capit-
alize the keywords (such as SELECT and FROM) according to the popular convention.
To sum up, here’s the basic syntax used in a SELECT query:
SELECT
This keyword indicates that we want to retrieve data, rather than modify,
add, or delete data—these activities use the UPDATE, INSERT, and DELETE
keywords, respectively, in place of SELECT.
columns
We must provide the names of one or more columns in the database table
from which we want to retrieve data. We can list multiple columns by separ-
ating the column names with commas, or we can use * to select all columns.
We can also prefix each column name with the table name, as shown here:
SELECT Employees.Name, Employees.Username
FROM Employees.Name
This approach is mandatory when two or more of the tables we’re dealing
with contain columns that have the same names. We’ll learn to read data
from multiple tables a little later in the chapter.
297
Using the SELECT Statement