SQL WHERE Example

What is SQL WHERE?

The WHERE clause in SQL allows you to filter the results of a query based on specific conditions. It's how you narrow down what data you're looking for.

Example

Let's say we have a table called 'Employees' with columns 'EmployeeID', 'FirstName', 'LastName', and 'Department'."

We want to find all employees in the 'Sales' department.

SELECT * FROM Employees WHERE Department = 'Sales';

Another Example

We want to find all employees with an ID greater than 10.

SELECT * FROM Employees WHERE EmployeeID > 10;

A More Complex Example

We want to find employees who are not from New York.

SELECT * FROM Employees WHERE City != 'New York';

Example with Multiple Conditions

Find all employees with a name containing 'Smith' and a department equal to 'Marketing'."

SELECT * FROM Employees WHERE FirstName LIKE '%Smith%' AND Department = 'Marketing';

Example with an Alias

Find all employees with 'John' in their name.

SELECT * FROM Employees WHERE FirstName LIKE '%John%';