SQL WHERE Clause

The WHERE clause in SQL is used to filter records and fetch only those that fulfill a specified condition. It is an essential part of SQL queries when you need to work with a subset of the data from a table based on some criteria. In this tutorial, we will explore the basics of the WHERE clause and how to use it effectively in SQL queries.

Syntax

The basic syntax of the WHERE clause is as follows:

sql
1SELECT column1, column2, ... 2FROM table_name 3WHERE condition;
  • column1, column2, ...: The columns or fields you want to retrieve.
  • table_name: The name of the table from which to retrieve the data.
  • condition: The condition that must be met for a record to be included in the result set.

Using the WHERE Clause

Simple Conditions

You can use various operators with the WHERE clause to form conditions:

  • =: Equal to
  • <> or !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Example:

sql
1SELECT * FROM Employees 2WHERE Department = 'Sales';

This query retrieves all records from the Employees table where the Department is 'Sales'.

Combining Conditions

You can combine multiple conditions using the AND and OR logical operators:

Using AND

sql
1SELECT * FROM Employees 2WHERE Department = 'Sales' AND Salary > 50000;

This query returns all employees from the Sales department with a salary greater than 50,000.

Using OR

sql
1SELECT * FROM Employees 2WHERE Department = 'Sales' OR Department = 'Marketing';

This query retrieves all employees from either the Sales or Marketing departments.

IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.

Example:

sql
1SELECT * FROM Employees 2WHERE Department IN ('Sales', 'Marketing', 'IT');

This query returns all employees who work in Sales, Marketing, or IT departments.

BETWEEN Operator

The BETWEEN operator selects values within a given range.

Example:

sql
1SELECT * FROM Employees 2WHERE Salary BETWEEN 40000 AND 60000;

This query returns all employees whose salary is between 40,000 and 60,000.

LIKE Operator

The LIKE operator is used to search for a specified pattern in a column.

Example:

sql
1SELECT * FROM Employees 2WHERE FirstName LIKE 'J%';

This query retrieves all employees whose first name starts with the letter 'J'.

NOT Operator

The NOT operator is used to exclude records that match the condition.

Example:

sql
1SELECT * FROM Employees 2WHERE NOT Department = 'Sales';

This query returns all employees who do not work in the Sales department.

Tips for Using the WHERE Clause

  • Always ensure that the condition in the WHERE clause is specific enough to filter out the unwanted records.
  • Use parentheses () to group conditions when combining AND and OR to control the order of evaluation.
  • Be cautious with NULL values; comparisons with NULL using the = operator will not match. Use IS NULL or IS NOT NULL instead.
  • When using LIKE, remember that % represents zero or more characters, and _ represents a single character.
  • To improve performance, try to use indexed columns in the WHERE clause conditions.