MySQL SELECT Statement with Examples


How to Use the MySQL SELECT Statement

In this tutorial, you'll learn how to use the MySQL SELECT statement to select records from one or more tables in a database.

SELECT Statement Syntax

The syntax of the SELECT statement will look like this:

SELECT column(s) FROM table;

However, if you need to select all columns from a table, then you use * character between SELECT and FROM as follows:

SELECT * FROM table;

Suppose you have the customers table, which consists of four columns,  cusotmer_idcusotmer_namegroup, and joined_date.

For example, let's say you want to select only two columns, customer_name, and group, then you can write the MySQL SELECT statement as follows:

SELECT customer_name, group
FROM customers;

The result of the above MySQL SELECT statement will look like this:

customer_namegroup
Ida FrankRetail Customers
Dawn GriffinWholesale Customers
Audrey JordanOnline Customers
Josh WarnerRegular Customers

Now, let's say you need to select all columns in the customer table.

You can write all the column names in the SELECT statement like this:

SELECT customer_id, customer_name, group, joined_date
FROM customers;

Or, you can write like this:

SELECT * FROM customers;

MySQL will return the result set that looks like this:

customer_idcustomer_namegroupjoined_date
#ID_00001Ida FrankRetail CustomersFebruary 10th 2018
#ID_00002Dawn GriffinWholesale CustomersJanuary 8th 2016
#ID_00003Audrey JordanOnline CustomersOctober 1st 2013
#ID_00004Josh WarnerRegular CustomersMarch 30th 2012

Note that when you use * in the MySQL SELECT statement, the order of columns in the result set will be the same as the columns in the table.

SELECT Statement and WHERE Clause

In some cases, you don't want MySQL to return all records in the table. You want to retrieve only the records that match conditions you specify.

To select all records that match specified conditions, you can use the WHERE clause in the MySQL SELECT statement

Let's take a look at an example:

SELECT * FROM customers
WHERE customer_id = "#ID_00002";

Here, MySQL returns only the records that customer_id is equal to "#ID_00002" from the above statement.

So, for example, if there is only one record that customer_id matches the above condition, then only that record will be returned in the result set. And MySQL returns nothing if there are no records match.

In the previous example, we've used only one condition. 

In the following examples, I show you how to specify more conditions using AND or OR operator in the WHERE clause.

OR & AND Operator in the MySQL SELECT Statement

Let's take a look at an example of using the OR operator in the WHERE clause:

SELECT * FROM customers
WHERE customer_id = "#ID_00002" OR customer_id="#ID_00003";

And here is an example of using the AND operator in the WHERE clause:

SELECT * FROM customers
WHERE joined_date = "January 8th 2016" AND group="Retail Customers";

Also, you can use both AND & OR operators in the WHERE clause list this:

SELECT * FROM customers
WHERE joined_date = "January 8th 2016" AND group="Retail Customers" OR group="Online Customers";

In this tutorial, you've learned how to use the MySQL SELECT statement to select records from one or more tables in a database. If you need to include all the columns in the result set, you can write all the columns in the select statement, or you also can use the (*) character. If you want MySQL returns only records that match conditions you specify, use the WHERE clause.

Leave a Reply

Your email address will not be published. Required fields are marked *