MySQL Select

The MySQL SELECT statement is used to fetch data from table.

Syntax of SELECT statement

mysql->SELECT [Fields] FROM Tablename [WHERE CLAUSE] [LIMIT];

The fields can be either single field name or multiple fields name.

The given query select first_name and last_name from 'students' table.

mysql->SELECT first_name, last_name FROM students;
baseurl.'/images/select2.jpg'; ?> alt="mysqlselect">

Select All

We can use '*' special character to select all fields values.

mysql->SELECT * FROM students;
baseurl.'/images/select1.jpg'; ?> alt="mysqlselect">

WHERE Clause

We can also fetch data on condition basis by using WHERE clause. The given query fetch only student data whose id is '2'.

mysql->SELECT * FROM students WHERE id = '2';
baseurl.'/images/select3.jpg'; ?> alt="mysqlselect">

We can also apply multiple conditions in SELECT statement, like -

mysql->SELECT * FROM students WHERE class = '4A' OR age = '10'; 
baseurl.'/images/select4.jpg'; ?> alt="mysqlselect">

LIMIT

To fetch limited result rows use LIMIT keyword. The given query returns only two rows data.

mysql->SELECT * FROM students LIMIT 2;
baseurl.'/images/select5.jpg'; ?> alt="mysqlselect">

ORDER BY

We can sort multiple columns in ascending or descending order.

mysql->SELECT * FROM students ORDER BY first_name ASC;
baseurl.'/images/select6.jpg'; ?> alt="mysqlselect">