Summary:In this tutorial, you will learn how to MySQL SELECT statement to query data from database tables.
MySQL SELECT Statement Syntax
In order to retrieve data from MySQL database table you need to use MySQL SELECT statement. The following illustrates MySQL SELECT statment syntax:
1 |
SELECT column_name1,column_name2... |
5 |
[HAVING group_conditions]] |
6 |
[ORDER BY sort_columns] |
The MySQL SELECT statement has many optional elements that you can use. The order of FROM, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT has to be in the sequence above.
To select all columns in a table you can use asterisk (*) notation instead of listing all column names in the MySQL SELECT statement. For example, if you need to query all the columns in offices table, you can use the following query:
1 |
SELECT * FROM employees |

The MySQL SELECT statement also allows you to to view partial data of a table by listing columns’ name after the SELECT keyword. This is called projection. For example if you need to view only first name, last name and job title of employee in the employees table, you can use the following query:
1 |
SELECT lastname,firstname,jobtitle |

WHERE Clause
WHERE clause of the MySQL SELECT statement enables you to select particular rows which match its conditions or search criteria. You use WHERE clause to filter the records based on a certain conditions. For example, you can find the president of company by using the following query:
1 |
SELECT firstname,lastname,email |
3 |
WHERE jobtitle="president" |

DISTINCT
With DISTINCT keyword, you can eliminate the duplicate records from the SELECT statement. For example, to find how many job title of all employees in the employees table, you use DISTINCT keyword in SELECT statement as follows:
1 |
SELECT DISTINCT jobTitle FROM employees; |

Sorting result with ORDER BY
The ORDER BY clause allows you to sort the result set on one or more columns in ascending or descending order. To sort the result set in ascending order you use ASC and in descending order you use DESC keywords. By default, the ORDER BY will sort the result set in ascending order. For example, to sort the name of employees by first name and job title, you can execute the following query:
1 |
SELECT firstname,lastname, jobtitle |
3 |
ORDER BY firstname ASC,jobtitle DESC; |
In this tutorial, you’ve learned about basic MySQL SELECT statement to retrieve data from one database table. You’ll learn more about each technique in details in later tutorials.
Related posts:
- Managing Database Index in MySQL
- Working with Database Table – Part I
- Managing Databases in MySQL
- Understanding MySQL TIMESTAMP
- Changing Table Structure Using MySQL ALTER TABLE