SQL WHERE
SQL WHERE

SQL WHERE

Multi-select
Status
Done

(Learn how to use the WHERE clause to filter rows based on specified conditions.)

Summary: in this tutorial, you will learn how to use the MySQL WHERE clause in the SELECT statement to filter rows from the result set.

Introduction to MySQL WHERE clause

The WHERE clause allows you to specify a search_condition for the rows returned by a query. The following shows the syntax of the WHERE clause:

SELECT
   select_list
FROM
   table_name
WHERE
    search_condition;

The search_condition is a combination of one or more expressions using the logical operator:

The followings are the syntax that can be use with WHERE

  • AND
  • OR
  • BETWEEN low AND high
  • LIKE
  • IN
  • NOT IN
  • IS NULL
  • COMAPARSION
  • NOT BETWEEN low AND high
  • LIMIT

In MySQL, a predicate is a Boolean expression that evaluates to TRUEFALSE, or UNKNOWN.

The SELECT statement will include any row that satisfies the search_condition in the result set.

Besides the SELECT statement, you can use the WHERE clause in the UPDATE or DELETE statement to specify which rows to update or delete.

When executing a SELECT statement with a WHERE clause, MySQL evaluates the WHERE clause after the FROMclause and before the SELECT and ORDER BY clauses:

image

MySQL WHERE clause examples

We’ll use the employees table from the sample database for the demonstration:

image

1. Using the WHERE clause with operator example

2. Using the WHERE clause with the AND operator

3. Using the WHERE clause with OR operator

4. Using the WHERE clause with the BETWEEN operator

5. Using the WHERE clause with the LIKE operator example

6. Using the WHERE clause with the IN operator example

7. Using the WHERE clause with NOT IN operator

8. Using the WHERE clause with the IS NULL operator

9. Using MySQL WHERE clause with comparison operators

10. Using MySQL DISTINCT clause

11. Using MySQL LIMIT clause