MySQL Where Clause

MySQL Where Clause is a statement used for filtering records based on conditions.

MySQL Where clause syntax

The basic syntax of a where Clause is

Select <column-names> from <table>
where <conditions>;

Pre-requisites:

Command-line client:

  • Log in with user credentials with at least Select credentials.
  • Select the database to use.

MySQL Workbench:

  • Open a new connection with user credentials.
  • Select a database to use.

We will look at examples using different options.

Simple where Clause in MySQL using Command-line

We will use the where Clause with literal values to get zero or more records.

select * from departments where dept_id =2;

SELECT * FROM DEPARTMENTS
WHERE DEPT_NAME = 'APPLICATION DEVELOPMENT';

Examples using the MySQL Workbench:

Using MySQL Where Clause with > operator: 

This will return all the records which satisfy the greater than condition i.e. with values greater than the value mentioned in the condition for that column. The values which are less or equal will not be included in the output.  

Example:

SELECT * FROM SALARIES WHERE SALARY > 80000;

Using MySQL Where Clause with < operator

Here only the records with values less than the value mentioned are returned. The rest are not included in the output result set.

Example:

SELECT * FROM SALARIES WHERE SALARY < 39000;

Using MySQL Where Clause with >= operator

All the records that have a value greater than or equal to the value mentioned in the condition will be included. The records with values less than the value mentioned for the column will not be included in the result set.

SELECT * FROM SALARIES WHERE SALARY >= 80000;

Using MySQL Where Clause with <= operator

The records with values less than or equal to the value mentioned will be included in the result set. 

SELECT * FROM SALARIES WHERE SALARY <= 80000;

Using MySQL Where Clause with a NULL operator

To check Null conditions, we do:

SELECT * FROM EMPLOYEES WHERE HIRE_DATE IS NULL;

Using MySQL Where Clause with NOT NULL operator

Only the records with No Null values are returned in the result set. The NULL values for that column are excluded.

SELECT * FROM EMPLOYEES WHERE HIRE_DATE IS NOT NULL;

There are many other options we can use with the where Clause and make massive, complex queries. We will look at those examples in further articles.