MySQL Select Query

The MySQL select query is used to retrieve data from a table in our MySQL database. This query basically says "SELECT some columns from a table". It is the most commonly used of the MySQL queries as we normally fetch data more than we write data.

When you run a select query on a table in the database, all rows matching the particular criteria are returned.

The MySQL select query tutorial below explains.

MySQL Select Syntax

The syntax for the SELECT statement in MySQL is as follows:

SELECT column1, column2, ... FROM table_name;
 
# OR
 
SELECT * FROM table_name;

We can either specify the particular fields (column names) that we want to pull data from, or we can use an asterisk (which is the wildcard symbol in MySQL) to return all fields in each row.

MySQL Select Example

Suppose we had a table called "employees" with the following data:

first_name last_name
Paul Pitterson
Francine Beecham
Raul DiNozzo

If we wanted to retrieve all the data from this table in our database we would use this MySQL SELECT statement:

SELECT * FROM employees;

The above MySQL query would return data from all the columns in our table and the result set would look like the table above.

If we only wanted the first names of the employees we would use this MySQL SELECT statement:

SELECT first_name FROM employees;

This code would give:

first_name
Paul
Francine
Raul

It is more efficient to return data only from the columns which are useful to us based on the logic we are using. In other words, if we are only interested in first names, we do not need to write our query so that it returns last names as well. This is a good practice as it will reduce the traffic and load on the MySQL server.

You can use the MySQL Where Clause to further narrow down the data you want your query to return. If you wanted to, you could also order the way the results are returned using the principles from our MySQL order by tutorial.

We hope this MySQL tutorial was useful.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...