MySQL Select: With 3 online demos

MySQL select statement

A few main points about the select command:

  • To retrieve existing data from the tables of MySQL database we use the Select statement.
  • A simple select statement without a where clause may retrieve whole table data e.g. select * from table_name.
  • You may retrieve all columns by using ‘*’ or by giving column names of table separated by commas in the select statement.
  • Generally we use Select statement with Where clause to limit the returned data from tables. The Where clause will include any conditions or criteria.
  • By using single select MySQL statement, you can retrieve data not only from one table but more than one tables by using Joins.

Following are a few examples of using select statement. The examples include retrieving whole table data, only a few columns data as well as using where clause to return only required rows.

Before that, let us first look at the syntax of Select in MySQL.

MySQL Select syntax

Following is the general syntax of using select command:

Select * from table_name

By using that query, the whole table data will be retrieved.

Using Select to return only given columns:

The syntax to fetch only specific columns data by using select statement is:

Select col1, col2, col3 from table_name

Using where clause

Select * from table_name

Where col_1 = val1

Select statement examples in MySQL

To explain Select statement with examples, we have already created a table, tbl_employee in our test database. The test table contains five columns with data as shown in the graphic below.

Retrieving whole data by select query

This is how we can retrieve whole table data of tbl_emploee, by using the select statement.

The select query:

Select statement with column names example

In the following example, we will only retrieve employee names and salaries from the tbl_emploee table. Look at the query and output by clicking the link below:

The select MySQL query will be:

Select with Where clause example

In the above examples, we retrieved all data by simply using the ‘*’ and then all columns data by specifying column names in the select statement. The following example uses Where clause in Select statement where we will only retrieve a specific employee’s row by giving its ID in the where clause. The query and output can be seen by clicking the link:

The Select query with where clause:

Also see MySQL insert into | MySQL create table

Was this article helpful?

Related Articles

Leave A Comment?