NOTE: Show you how to use a simple SELECT FROM
statement to query the data from a single table.
Introduction to MySQL SELECT FROM statement
The SELECT
statement allows you to select data from one or more tables. To write a SELECT
statement in MySQL, you use this syntax:
SELECT
column1,
column2,
column3...
FROM
Table_name;
In this syntax:
- First, specify one or more columns from which you want to select data after the
SELECT
keyword. If theSELECT
has multiple columns, you need to separate them by a comma (,
). - Second, specify the name of the table from which you want to select data after the
FROM
keyword. - The semicolon (
;
) is optional, which denotes the end of a statement. If you have two or more statements, you need to use the semicolon(;)
to separate them so that MySQL will execute each statement individually.
- Using the SELECT FROM statement to retrieve data from a single column example
Using the employees
table that has eight columns: employeeNumber, lastName, firstName, extension, email, officeCode, reportsTo, and jobTitle.. The following example uses the SELECT FROM
statement to retrieve the last names of all employees:
SELECT lastName
FROM employees;
The result of a SELECT
statement is called a result set as it’s a set of rows that results from the query.
- Using the SELECT FROM statement to query data from multiple columns example
The following example uses the SELECT FROM
statement to get the first name, last name, and job title of employees:
SELECT
lastName,
firstName,
jobTitle
FROM
employees;
- Using the SELECT FROM statement to retrieve data from all columns example
If you want to select data from all the columns of the employees
table, you can specify all the column names in the SELECT
clause like this:
SELECT
employeeNumber,
lastName,
firstName,
extension,
email,
officeCode,
eportsTo,
jobTitle
FROM
employees;
Alternatively, you can use the asterisk (*) which is the shorthand for all columns. For example:
SELECT *
FROM employees;
The query returns data from all the columns of the employees
table. The SELECT *
is often called “select star” or “select all” since it selects data from all columns of the table. In practice, you should use the SELECT *
for the ad-hoc queries only.