Queries: Selecting Data from a Table

An SQL query requests data from the database and receives the results. This process, also known as data retrieval, is expressed using the select statement.

A query has three main parts: the select clause, the from clause, and the where clause.

The query process, also known as data retrieval, is used for selections, which retrieve a subset of the rows in one or more tables, or for projections, which retrieve a subset of the columns in one or more tables.

A simple example of a select statement is:

select select_list 
from table_list 
where search_conditions 

The select clause specifies the columns you want to retrieve. The from clause specifies the tables to search. The where clause specifies which rows in the tables you want to see. For example, the following select statement finds, in the pubs2 database, the first and the last names of writers living in Oakland from the authors table:

select au_fname, au_lname 
from authors 
where city = "Oakland"

Results of this query appear in columnar format:

au_fname        au_lname 
--------------  ----------- 
Marjorie        Green 
Dick            Straight 
Dirk            Stringer 
Stearns         MacFeather 
Livia           Karsen
(5 rows affected)