Introduction to MySQL SELECT 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 select_list FROM table_name;
In this syntax:
First, specify one or more columns from which you want to select data after the SELECT keyword. If the select_list 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. It 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.
The SELECT and FROM are the keywords. By convention, you write the SQL keywords in uppercase. However, it’s not mandatory. Because SQL is case-insensitive, you can write the SQL statement in lowercase, uppercase, etc. For example:
Using the MySQL SELECT statement to retrieve data from a single column example
The following example uses the SELECT statement to select the last names of all employees:
SELECT lastName FROM employees;
Using the MySQL SELECT statement to query data from multiple columns example
The following example uses the SELECT statement to get the first name, last name, and job title of employees:
SELECT lastName, firstName, jobTitle FROM employees;
Even though the employees table has many columns, the SELECT statement returns data of three columns lastName, firstName, and jobTitle specified in the SELECT clause:
Using the MySQL SELECT 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:
Alternatively, you can use the asterisk (*) which is the shorthand for all columns. For example:
SELECT * FROM employees;