SELECT

To retrieve and display data from tables within a database, use this MySQL statement. It also has more than one syntax and many options—it also allows for sub-queries.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

SELECT [flags] {*|column|expression}[, ...]
  FROM table[,  . . . ] 
  [WHERE condition]
  [GROUP BY {column|expression|position}[ASC|DESC],  . . . 
     [WITH ROLLUP]]
  [HAVING condition]
  [ORDER BY {column|expression|position}[ASC|DESC] ,  . . . ]
  [LIMIT {[offset,] count|count OFFSET offset}]
  [PROCEDURE procedure(arguments)]
  options

Explanation

Use this MySQL statement to retrieve and display data from tables within a MySQL database. It has many clauses and options, but for simple data retrieval many of them can be omitted. The basic syntax for the statement is shown. After the SELECT keyword, some keywords to control the whole operation may be given. Next comes an asterisk to retrieve all columns, a list of columns to retrieve, or expressions returning values to display, separated by commas.

Examples

Here is a simple example of how you can use the SELECT statement:

SELECT name_first, name_last, telephone_home,
DATEDIFF(now( ), last_review)
AS 'Days Since Last Review'
FROM employees;

In this example, three columns from MySQL and the results of an expression based on a fourth column are to be displayed. The first and last name of each employee, each employee's home telephone number, and the difference between the date of the employee's last employment review and the date now are listed. This last field has the addition of the AS keyword to set the column heading of the results set, and to name an alias for the field. An alias may be referenced in subsequent clauses of the same statement (e.g., the ORDER BY clause). To select all columns in a MySQL table, the wildcard * can be given instead of the column names.