Search This Blog

MySQL SELECT Query

The SQL SELECT command is used to fetch data from MySQL database. You can use this command at mysql> prompt as well as in any script like PHP.

Syntax:
Here is generic SQL syntax of SELECT command to fetch data from MySQL table:

SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE Clause]
[OFFSET M ][LIMIT N]


You can use one or more tables separated by comma to include various condition using a WHERE clause. But WHERE clause is an optional part of SELECT command.

You can fetch one or more fields in a single SELECT command.

You can specify star (*) in place of fields. In this case SELECT will return all the fields

You can specify any condition using WHERE clause.

You can specify an offset using OFFSET from where SELECT will start returning records. By default offset is zero

You can limit the number of returned using LIMIT attribute.

Fetching Data from Command Prompt:
This will use SQL SELECT command to fetch data from MySQL table tutorials_tbl

Example:
Following example will return all the records from tutorials_tbl table:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * from tutorials_tbl
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
| 1 | Learn PHP | John Poul | 2007-05-21 |
| 2 | Learn MySQL | Abdul S | 2007-05-21 |
| 3 | JAVA Tutorial | Sanjay | 2007-05-21 |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.01 sec)