
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What is the meaning of “SELECT” statement in MySQL and how can it be used?
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.
- Related Articles
- How Groups function can be used in MySQL SELECT clause?
- How Can MySQL CASE statement be used in stored procedure?
- How MySQL IF statement can be used in a stored procedure?
- How MySQL WHILE loop statement can be used in stored procedure?
- How MySQL REPEAT loop statement can be used in stored procedure?
- How Can MySQL LOOP statement be used in a stored procedure?
- How can column data be used within MySQL CASE statement?
- How MySQL IF ELSE statement can be used in a stored procedure?
- In MySQL, what is Bit-field notation and how it can be used to write bit-field value?
- How can MySQL IF ELSEIF ELSE statement be used in a stored procedure?
- How MySQL SUM() function evaluates if it is used with SELECT statement that returns no matching rows?
- What is Sieving? Where can it be used?
- How can SELECT, without reference to any table, be used to calculate expression in MySQL?
- How can I use MySQL IF() function within SELECT statement?
- What should be used to implement MySQL LIKE statement in MongoDB?
