SELECT Syntax

SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]

 

 

TheSELECTstatement is used to pull information from a table.

Selecting All Data

SELECT * FROM pet;

Fix only the erroneous record with anUPDATEstatement:

UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';

Selecting Particular Rows

SELECT * FROM pet WHERE name = 'Bowser';

Specify conditions on any column:

SELECT * FROM pet WHERE birth >= '1998-1-1';

 

Combine conditions: query uses the AND logical operator.

SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';

 

Query uses the OR logical operator.
SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';

 

AND and OR may be intermixed, although AND has higher precedence than OR. If you use both operators, it is a good idea to use parentheses to indicate explicitly how conditions should be grouped:

SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
-> OR (species = 'dog' AND sex = 'f');

Selecting Particular Columns

SELECT name, birth FROM pet;

SELECT owner FROM pet;

 

Minimize the output, retrieve each unique output record just once by adding the keyword DISTINCT:

SELECT DISTINCT owner FROM pet;

Use a WHERE clause to combine row selection with column selection

.
SELECT name, species, birth FROM pet
-> WHERE species = 'dog' OR species = 'cat';

 

SELECT name,
species,
sex,
birth,
death
FROM pet;