To retrieve data from a table, the table is queried. An SQL SELECT statement is used to do this. The statement is divided into a select list (the part that lists the columns to be returned), a table list (the part that lists the tables from which to retrieve the data), and an optional qualification (the part that specifies any restrictions). The following query lists all columns of all employees in the table in no particular order.
Here, “*” in the select list means all columns. The following is the output from this query.
You may specify any arbitrary expression in the select list. For example, you can do:
Notice how the AS clause is used to re-label the output column. (The AS clause is optional.)
A query can be qualified by adding a WHERE clause that specifies which rows are wanted. The WHERE clause contains a Boolean (truth value) expression, and only rows for which the Boolean expression is true are returned. The usual Boolean operators (AND, OR, and NOT) are allowed in the qualification. For example, the following retrieves the employees in department 20 with salaries over $1000.00:
You can request that the results of a query be returned in sorted order:
You can request that duplicate rows be removed from the result of a query:
The following section shows how to obtain rows from more than one table in a single query.