MySQueaL Resources

resources for mysql admins and developers who are squealing for help

More Resources

Data can be retrieved from one or more tables, given in a comma-separated list. If multiple tables are specified, other clauses must define how the tables are joined. The remaining clauses may be called on to refine the data to be retrieved, to order it, and so forth. These various keywords, options and clauses are detailed in other documentation pages:

Related Release Notes
Main Doc Pages
Privileges Required
  • SELECT
Related Release Notes
Comments
MySQL columns: everywhere I look are tables and columns; life is filled with databases of information to be ordered and grouped. (Berlin, Germany)

SELECT Statement

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.

hits past month: 33 ;  last updated: may 4, 2009 - 2:34am ;  parent: Data Manipulation Statements

Syntax

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.

Return to Data Manipulation Statements page of our MySQL Documentation