What is the meaning of “SELECT” statement in MySQL and how can it be used?

MySQLMySQLi Database

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

Syntax

Here is generic syntax of SELECT command to fetch data from the MySQL table −

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

Some important points about SELECT statement are as follows −

  • We can use one or more tables separated by a comma to include various conditions using a WHERE clause, but the WHERE clause is an optional part of the SELECT command.

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

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

  • We can specify any condition using the WHERE clause.

  • We can specify an offset using OFFSET from where SELECT will start returning records. By default, the offset starts at zero.

  • We can limit the number of returns using the LIMIT attribute.

Example

mysql> Select * from Employee;

+------+--------+
| Id | Name |
+------+--------+
| 100 | Ram |
| 200 | Gaurav |
| 300 | Mohan |
+------+--------+

3 rows in set (0.00 sec)

mysql> Select * from Employee Where Name = ‘Ram’;

+------+--------+
| Id | Name |
+------+--------+
| 100 | Ram |
+------+--------+

1 row in set (0.00 sec)

mysql> Select Id from Employee;

+-----+
| Id |
+-----+
| 100 |
| 200 |
| 300 |
+-----+

3 rows in set (0.00 sec)

Above example shows some ways in which we can use a SELECT statement to fetch records from a MySQL table.

raja
Published on 19-Feb-2018 15:14:17
Advertisements