MySQL – SELECT Record
In MySQL, the SELECT statement is used to retrieve data from one or more tables. By using this statement, we can retrieve records that match specified criteria for all fields or specified fields. In addition, it is capable of working with a wide variety of scripting languages, such as PHP, Ruby, and many others.
Syntax
Here is the general syntax for fetching data from tables:
SELECT field_name1, field_name 2,... field_nameN FROM table_name1, table_name2... [WHERE condition] [GROUP BY field_name(s)] [HAVING condition] [ORDER BY field_name(s)] [OFFSET M ][LIMIT N];
Syntax for all fields:
SELECT * FROM tables [WHERE conditions] [GROUP BY fieldName(s)] [HAVING condition] [ORDER BY fieldName(s)] [OFFSET M ][LIMIT N];
Parameters
field_name(s) or * : Specifies one or more columns to return in the result set. Using an asterisk(*) represents all fields of a table.
table_name(s) : This is the name of the tables from which we wish to retrieve data.
WHERE: There is an optional clause in this sentence. This specifies the condition that returned the matched records.
GROUP BY: The option is optional. It collects data from multiple records and groups them according to one or more columns.
HAVING: The option is optional. This function works with the GROUP BY clause and returns only rows that satisfy the TRUE condition.
ORDER BY: The option is optional. This function sorts the records in the result set.
OFFSET: The option is optional. The column specifies which row will be returned first. Initially, it starts at zero.
LIMIT: The option is optional. By using this parameter, the number of returned records in the result set can be limited.
Example
single column from the table
SELECT first_name FROM employees;
multiple columns from the table
SELECT first_name, last_name, emp_no FROM employees;