MySQL SELECT Statement with Examples

How to Use the MySQL SELECT Statement

In this tutorial, you will learn to select rows from a table using the MySQL SELECT statement.

SELECT is the most basic of SQL. However, it is also a query that can increase the execution speed depending on a combination of conditions.

To select rows from multiple tables, you can use the MySQL join clause, INNER JOIN, LEFT JOIN, RIGHT JOIN, and CROSS JOIN.

Use the WHERE clause to retrieve only rows that match the specified criteria.

You can also use MySQL aggregate functions such as SUM(), COUNT(), AVG(), MAX(), and MIN() in SELECT statements.

SELECT Statement Syntax

The syntax of the SELECT statement will look like this:

SELECT column(s) FROM table;

However, if you want to select all columns of a table, use the * character between SELECT and FROM as follows:

SELECT * FROM table;

SELECT Statement Examples

Suppose we have a table named customers, and it consists of 4 columns: customer_id, customer_name, group, and joined_date.

For example, if we want to select only two columns: customer_name and group, the MySQL SELECT statement can be written as follows:

SELECT customer_name, group
FROM customers;

The result set of the above MySQL SELECT statement is as follows:

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

To select all columns of the customer table, you can write the SQL query as follows:

SELECT customer_id, customer_name, group, joined_date
FROM customers;

Or, you can (*) and write like this:

SELECT * FROM customers;

MySQL returns a 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

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

SELECT Statement and WHERE Clause

Sometimes you don't want to get all the rows in the table. You want to retrieve only rows that match the specified criteria.

In such cases, you can use a WHERE clause in a MySQL SELECT statement.

Here is an example:

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

This SQL query returns only rows whose customer_id is equal to "#ID_00002".

So, for example, if there is only one row whose customer_id matches the above condition, only that record will be returned in the result set. MySQL will not return anything if there is no matching row.

In the previous example, there was only one condition.

In this example, multiple conditions are used in a WHERE clause using the AND or OR operator.

Using the OR and AND operators in MySQL SELECT statements

The following is an example of using the OR operator in a WHERE clause:

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

The following is an example of using the AND operator in a WHERE clause:

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

You can also use both AND and OR operators in the WHERE clause as follows:

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

Summary of MySQL SELECT Statement

In this tutorial, you have learned how to use the MySQL SELECT statement to select rows from a table in a given database.

If you want to include all columns in the result set, you can also write all columns in the select statement or use the (*) character.

Use a WHERE clause if you want to get only rows that match the specified conditions.

You can also use a combination of a SELECT statement and a LIMIT clause to limit the number of rows.


See also:
MySQL LIKE Operator Pattern Matching and Examples
MySQL SUBSTRING_INDEX Function with Examples
MySQL EXISTS Operator with Examples
MySQL ROW_NUMBER Function with Examples
MySQL CONCAT() Function | Concatenate Strings in MySQL

Leave a Comment