MySQL: Using WHERE clause in SELECT statement

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

When retrieving data from a MySQL database, filtering the dataset to get the required information is a common task. This is where the WHERE clause comes in handy. It allows you to specify conditions to filter the rows returned by a SELECT statement. In this tutorial, we will delve deep into using WHERE clause in MySQL with a wide array of examples, progressing from basic to advanced applications.

The Basic WHERE Clause

The following is a basic example demonstrating how the WHERE clause is used in a SELECT statement to retrieve only the rows that meet the specified condition:

SELECT * FROM employees WHERE department = 'Sales';

This query retrieves all rows from the employees table where the department column has the value Sales .

Comparing Numeric Values

If you are working with numeric data, you can use mathematical comparison operators in the WHERE clause. Here are the operators typically used:

  • Equal to: =
  • Greater than: >
  • Less than: <
  • Greater than or equal to: >=
  • Less than or equal to: <=
  • Not equal to: != or <>

Example:

SELECT * FROM employees WHERE salary > 50000;

This query will return all employees who earn more than 50,000.

Using AND, OR, and NOT

You can combine multiple conditions using the AND and OR logical operators. The query only returns rows that satisfy all conditions when using AND or any of the conditions when using OR.

SELECT * FROM employees WHERE department = 'Sales' AND salary > 50000;

To exclude a condition, use NOT:

SELECT * FROM employees WHERE NOT department = 'Sales';

Matching Patterns with LIKE

When you need to perform a pattern match rather than a direct comparison, the LIKE operator comes into play. It’s used with wildcards such as % (which matches any sequence of characters) and _ (which matches any single character).

SELECT * FROM employees WHERE name LIKE 'Jo%';

This retrieves any employee whose name starts with ‘Jo’.

Including NULL Values

To include rows with NULL values in a specific column, you need to use IS NULL or IS NOT NULL.

SELECT * FROM employees WHERE manager_id IS NULL;

This query selects employees who do not have a manager_id assigned.

Working with IN()

If you need to filter data based on a list of possible values for a column, you can use the IN() operator.

SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');

This returns employees who are either in the Sales or Marketing departments.

Understanding BETWEEN

The BETWEEN operator is useful for conditions where a range is being specified.

SELECT * FROM employees WHERE salary BETWEEN 40000 AND 50000;

This returns employees whose salary is between 40,000 and 50,000.

Advanced WHERE Conditions

Leveraging subqueries in the WHERE clause is an example of an advanced application:

SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

This query selects employees earning above the company average.

Another advanced technique is to use expressions in the WHERE clause:

SELECT * FROM employees WHERE (YEAR(CURRENT_DATE) - YEAR(birth_date)) > 30;

This selects employees who are over 30 years old.

Combining with ORDER BY and LIMIT

To sort the results of a query that use a WHERE clause, you can chain it with the ORDER BY clause. Moreover, to limit the number of returned results, you can include LIMIT as well.

SELECT * FROM employees WHERE department = 'Sales' ORDER BY salary DESC LIMIT 10;

This will show the top 10 highest earning sales employees.

Conclusion

The WHERE clause is a fundamental aspect of querying in MySQL that provides great flexibility when filtering data sets. By coupling it with other SQL clauses and operators, sophisticated queries can be constructed to precisely manipulate and present data as required.