Database Reference
In-Depth Information
Basic Selection
The basic elements of thesyntax for the
SELECT
statement are the
SELECT
keyword, the
column you want to select, and the table from which to retrieve the data:
SELECT
column
FROM
table
;
If you want to select more than onecolumn, list them separated bycommas. If you want to
select all of the columns in a table, you can use the asterisk as a wildcard instead of listing
all of the columns. Let's use the
rookery
database you just loaded with data to see a
practical example of this basic syntax. Enter the following SQL statement in
mysql
to get a
list of all of the columns and rows in the
birds
table:
USE
rookery
;
SELECT
*
FROM
birds
;
This is the most minimal
SELECT
statement that you can execute successfully. It tells
MySQL to retrieve all of the data contained in the
birds
table. It displays the columns in
the order you defined them in the table's
CREATE
or
ALTER
statements, and displays rows
in the order they are found in the table, which is usually the order that the data was entered
into the table.
To select only certain columns, do something like this:
SELECT
bird_id
,
scientific_name
,
common_name
FROM
birds
;
This
SELECT
statement selects only three columns from each row found in the
birds
table. There are also many ways to choose particular rows, change the order in which they
are displayed, and limit the number shown. These are covered in the following sections of
this chapter.