MySQL WHERE

In MySQL, WHERE Clause can be used to select required records from a database table. You can specify any condition using WHERE Clause.

MySQL WHERE Clause Syntax

Below is the Basic Syntax of WHERE Clause in the SELECT statement:

MySQL WHERE Clause Examples

To demonstrate the MySQL WHERE Clause, let us create a Player Table with sample records as depicted in the below image by executing the following script.

player-demo-table

Example 1: Get all the Players whose State is New York

We can write a query like below to get all the Players whose state is New York

RESULT:
playerstate

Example 2: Get all the Players whose State is not New York

We can write a query like below to get all the Players whose state is not New York

RESULT:
mysql-where-example-2

Note: This query didn’t return the Player Tim Duncan whose State column value is NULL, because NULL value can’t be compared with some value (i.e. Null means an unknown value, so it can’t be used to compare with any know values). The only operation we can do with NULL is we can check whether it is NULL or NOT NULL as shown in the below examples 3 and 4.

Example 3: Get all the players whose state column value is not NULL

We can write a query like below to get all the Players whose State column has some value (i.e. Players whose State column value is NOT NULL)

RESULT:
mysql-not-null

Example 4: Get all the Players whose State column value is NULL

We can write a query like below to get all the Players whose State column value is NULL

RESULT:
mysql-null

Leave a Reply

Your email address will not be published. Required fields are marked *