WHERE Clause

WHERE Clause

In this tutorial, we are going to discuss about the WHERE Clause in DQL. In Data Query Language (DQL), the WHERE clause is used to filter records. It specifies conditions that must be met for records to be selected. The syntax for a WHERE clause in DQL is similar to SQL and is used in a SELECT statement to limit the results returned by the query.

WHERE Clause
WHERE

In MySQL, the WHERE clause is used to filter the rows returned by a SELECT, UPDATE, or DELETE statement. It allows you to specify a condition that must be met for a row to be included in the result set.

Syntax

The basic syntax of a WHERE clause in a SELECT statement is as follows:

SELECT column1, column2, column3, ...
FROM table_name
WHERE condition;

Example

Suppose we have a table named Employees with the following columns: EmployeeID, FirstName, LastName, Department, and Salary. To select records where the Department is ‘Sales’, you would use:

SELECT EmployeeID, FirstName, LastName, Department, Salary
FROM Employees
WHERE Department = 'Sales';
Common Operators in the WHERE Clause
  • = : Equal
  • <> or != : Not equal
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal
  • <= : Less than or equal
  • BETWEEN : Between an inclusive range
  • LIKE : Search for a pattern
  • IN : Specify multiple possible values for a column
Combining Conditions

You can combine multiple conditions using logical operators such as AND, OR, and NOT.

Example with AND and OR

To select records where the Department is ‘Sales’ and the Salary is greater than 50000, you would use:

SELECT EmployeeID, FirstName, LastName, Department, Salary
FROM Employees
WHERE Department = 'Sales' AND Salary > 50000;

To select records where the Department is either ‘Sales’ or ‘Marketing’, you would use:

SELECT EmployeeID, FirstName, LastName, Department, Salary
FROM Employees
WHERE Department = 'Sales' OR Department = 'Marketing';
Using LIKE for Pattern Matching

To select records where the LastName starts with ‘venkat’, you would use:

SELECT EmployeeID, FirstName, LastName, Department, Salary
FROM Employees
WHERE LastName LIKE 'venkat%';
Using BETWEEN for Range

To select records where the Salary is between 40000 and 60000, you would use:

SELECT EmployeeID, FirstName, LastName, Department, Salary
FROM Employees
WHERE Salary BETWEEN 40000 AND 60000;

The WHERE clause is a powerful tool in DQL for filtering and refining the data you retrieve from a database.

That’s all about the WHERE Clause in DQL. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy SQL..!!

WHERE Clause
Scroll to top