This article will teach you the fundamentals of the MySQL SELECT statement to fetch the data from a database table.
How to use the SELECT statement in MySQL
The SELECT
command can be used to retrieve information from many tables. In MySQL, a SELECT
statement is written using the following syntax:
SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] [HIGH_PRIORITY] [DISTINCT | | DISTINCTROW | ALL] column [[AS] alias][, ...] [INTO {OUTFILE 'filename' delimiters | DUMPFILE 'filename' | @variable}] [FROM table [[AS] alias] [WHERE condition [, ...]] [GROUP BY {column | expression | position} [ASC | DESC] [, ...] [WITH ROLLUP]] [HAVING condition] [ORDER BY {column | expression | position} [ASC | DESC] [, ...] ] [LIMIT {offset | row_limit | row_limit OFFSET offset}] [PROCEDURE name (arg [, ...])] [FOR UPDATE | LOCK IN SHARE MODE] [UNION [ALL] select substatement]
The programming language used is called SQL (Structured Query Language) (SQL).
To begin, place the SELECT
keyword at the beginning of the query and then list the column names from which you wish to retrieve data. Separate each column in the select list with a comma if there is more than one (,).
Secondly, after the FROM
keyword, name the table whose contents you wish to retrieve.
The use of a semicolon (;
) is discretionary. This symbol indicates the conclusion of a sentence. If you want MySQL to run many commands in sequence, you must use a semicolon (;
) to separate them.
The SELECT
and the FROM
clauses are the focus here. The SQL keywords must always be written with an uppercase letter. On the other hand, it's not required. Since SQL is case-insensitive, the SQL statement can be written in any case.
MySQL Select Statement Examples
In the below examples, I am using the customers table to retrieve the data. And the customer table is having the following structure:
customer_id first_name last_name age country
Example to Query All the Data with All the Columns
The following MySQL SELECT
statement queries all the data and all the columns from the Customers table:
SELECT * FROM Customers;
Output:
customer_id | first_name | last_name | age | country |
---|---|---|---|---|
1 | John | Doe | 31 | USA |
2 | Robert | Luna | 22 | USA |
3 | David | Robinson | 22 | UK |
Example to Query Specific Columns From a Table
Using the SELECT
statement you can query specific columns only. Below is an example:
Select first_name, last_Name, age FROM Customers;
Output:
first_name | last_name | age |
---|---|---|
John | Doe | 31 |
Robert | Luna | 22 |
David | Robinson | 22 |
Four parts make up the basic SELECT statement:
- The first word is "
SELECT
." - The columns that will be shown. In the first case, we used the asterisk (
*
) as a wildcard to request all columns. - The third important grammatical construction is the term
FROM
. - The fourth and last required field is the table's name (in this case, "
Cusotmers
").
You should now be able to select a database, list its tables, and use the MySQL SELECT statement to retrieve all of the data from a table.