Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL WHERE Clause

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:

More notes:

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

  1. Equal condition:
SELECT * FROM users WHERE username = 'test';

  1. Not equal condition:


SELECT * FROM users WHERE username != 'runoob';

  1. Greater than condition:
SELECT * FROM products WHERE price > 50.00;

  1. Less than condition:


SELECT * FROM orders WHERE order_date < '2023-01-01';

  1. Greater than or equal condition:


SELECT * FROM employees WHERE salary >= 50000;

  1. Less than or equal condition:
SELECT * FROM students WHERE age <= 21;

  1. 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;

  1. Fuzzy match condition (LIKE):


SELECT * FROM customers WHERE first_name LIKE 'J%';

  1. IN condition:
SELECT * FROM countries WHERE country_code IN ('US', 'CA', 'MX');

  1. NOT condition:


SELECT * FROM products WHERE NOT category = 'Clothing';

  1. BETWEEN condition:


SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';

  1. IS NULL condition
SELECT * FROM employees WHERE department IS NULL;

  1. 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 ' ' ; while ( $row = mysqli_fetch_array ( $retval , MYSQLI_ASSOC ) ) { echo " " . " " . " " . " " . " " ; } echo '
Tutorial ID Title Author Submission Date
{$row['runoob_id']}{$row['runoob_title']}{$row['runoob_author']}{$row['submission_date']}
' ; // Release memory mysqli_free_result ( $retval ) ; mysqli_close ( $conn ) ; ?>

The output is shown below: