MySQL WHERE Clause
We know that SELECT statements are used to read data from MySQL tables.
To conditionally select data from a table, you can add a WHERE clause to the SELECT statement.
The WHERE clause is used to filter query results in MySQL, returning only the rows that satisfy specific conditions.
Syntax
The following is the general syntax for an SQL SELECT statement using a WHERE clause to read data from a table:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Parameter Description:
column1,column2, ... are the names of the columns you want to select; if you use*it means select all columns.table_nameis the name of the table you want to query data from.WHERE conditionis a clause used to specify filter conditions.
More notes:
- In a query you can use one or more tables, separated by commas, and use the WHERE clause to set query conditions.
- You can specify any condition in the WHERE clause.
- You can use AND or OR to specify one or more conditions.
- The WHERE clause can also be used in SQL DELETE or UPDATE commands.
- The WHERE clause is similar to an if condition in a programming language, reading specified data based on field values in the MySQL table.
The following is a list of operators that can be used in a WHERE clause.
The examples in the table below assume A is 10 and B is 20
| Operator | Description | Example |
|---|---|---|
| = | Equal sign, checks whether two values are equal, returns true if they are equal | (A = B) returns false. |
| <>, != | Not equal, checks whether two values are equal, returns true if they are not equal | (A != B) returns true. |
| > | Greater than sign, checks whether the left value is greater than the right value, returns true if the left value is greater than the right value | (A > B) returns false. |
| < | Less than sign, checks whether the left value is less than the right value, returns true if the left value is less than the right value | (A < B) returns true. |
| >= | Greater than or equal sign, checks whether the left value is greater than or equal to the right value, returns true if the left value is greater than or equal to the right value | (A >= B) returns false. |
| <= | Less than or equal sign, checks whether the left value is less than or equal to the right value, returns true if the left value is less than or equal to the right value | (A <= B) returns true. |
Simple Example
- Equal condition:
SELECT * FROM users WHERE username = 'test';
- Not equal condition:
SELECT * FROM users WHERE username != 'runoob';
- Greater than condition:
SELECT * FROM products WHERE price > 50.00;
- Less than condition:
SELECT * FROM orders WHERE order_date < '2023-01-01';
- Greater than or equal condition:
SELECT * FROM employees WHERE salary >= 50000;
- Less than or equal condition:
SELECT * FROM students WHERE age <= 21;
- Combined conditions (AND, OR):
SELECT * FROM products WHERE category = 'Electronics' AND price > 100.00;
SELECT * FROM orders WHERE order_date >= '2023-01-01' OR total_amount > 1000.00;
- Fuzzy match condition (LIKE):
SELECT * FROM customers WHERE first_name LIKE 'J%';
- IN condition:
SELECT * FROM countries WHERE country_code IN ('US', 'CA', 'MX');
- NOT condition:
SELECT * FROM products WHERE NOT category = 'Clothing';
- BETWEEN condition:
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';
- IS NULL condition
SELECT * FROM employees WHERE department IS NULL;
- IS NOT NULL condition:
SELECT * FROM customers WHERE email IS NOT NULL;
The WHERE clause is very useful if we want to read specified data from a MySQL table.
Using the primary key as the condition of a WHERE clause query is very fast.
If the given condition has no matching records in the table, the query will not return any data.
Reading Data from the Command Prompt
We will use a WHERE clause in the SELECT statement to read data from the MySQL table runoob_tbl.
The following example reads all records in the runoob_tbl table where the runoob_author field value is Sanjay:
SQL SELECT WHERE Clause
SELECT
from
runoob_tbl
WHERE
runoob_author
=
'
Runoob Tutorial
'
;
Output result:
String comparison in MySQL's WHERE clause is case-insensitive. You can use the BINARY keyword to make string comparison in the WHERE clause case-sensitive.
Example below:
BINARY Keyword
mysql
SELECT
from
runoob_tbl
WHERE
BINARY
runoob_author
=
'
runoob.com
'
;
Empty
set
(
0.01
sec
)
mysql
SELECT
from
runoob_tbl
WHERE
BINARY
runoob_author
=
'
RUNOOB.COM
'
; +-----------+---------------+---------------+-----------------+ |
runoob_id
|
runoob_title
|
runoob_author
|
submission_date
| +-----------+---------------+---------------+-----------------+ |
3
|
JAVA
Tutorial |
RUNOOB
.
COM
|
2016
05
06
| |
4
| Learning
Python
|
RUNOOB
.
COM
|
2016
03
06
| +-----------+---------------+---------------+-----------------+
2
rows
in
set
(
0.01
sec
)
The example uses the BINARY keyword, which is case-sensitive, so runoob_author='runoob.com' query condition returns no data.
Reading Data Using a PHP Script
You can use the PHP function mysqli_query() with the same SQL SELECT command with a WHERE clause to get data.
This function is used to execute SQL commands, and then the PHP function mysqli_fetch_array() is used to output all the queried data.
Example
The following example returns records from the runoob_tbl table where the runoob_author field value is RUNOOB.COM:
MySQL WHERE clause test:
Runoob Tutorial MySQL WHERE Clause Test' ; echo '
| Tutorial ID | Title | Author | Submission Date |
| {$row['runoob_id']} | " . "{$row['runoob_title']} | " . "{$row['runoob_author']} | " . "{$row['submission_date']} | " . "
The output is shown below: