Name

SELECT

Synopsis

SELECT [flags] {column,  . . . |expressions}[,  . . . ] 

  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

Use this statement to retrieve and display data from tables within a database. It has many clauses and options. However, for simple data retrieval many of them can be omitted. The basic syntax for the statement is shown. After the SELECT keyword, some flags may be given. Next, a list of columns to retrieve and/or expressions may be given, separated by commas. For the tables, all that is required is one or more table names from which to retrieve data in a comma-separated list. The remaining clauses may be called on to refine the data to be retrieved, to order it, and so forth. These various flags, options, and clauses are detailed in subsections to this statement explanation. Here is a simple example of how you can use this statement:

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

In this example, three columns 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. ...

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.