WHERE

WHERE is an SQL clause that filters the results from a SELECT, UPDATE, or DELETE statement. It only returns or affects the records that satisfy a specified condition.

Example

SELECT *
FROM Employees
WHERE salary > 50000;

Output

| employee_id | first_name | last_name | position | hire_date | salary |
|-------------|------------|-----------|-----------|------------|--------|
| 2 | John | Smith | Developer | 2018-06-10 | 60000 |
| 5 | Jane | Doe | Manager | 2017-01-15 | 75000 |

Explanation

In the above example, WHERE clause is used to filter records. It extracts only those employees from the Employees table whose salary is greater than 50000. All columns of the filtered records are returned and displayed.

Example

SELECT * FROM Employees
WHERE FirstName = 'John';

Output

| EmployeeID | FirstName | LastName | JobTitle |
|------------|-----------|----------|-----------------|
| 1 | John | Doe | Software Engineer|

Explanation

In the given example, the SELECT * FROM Employees WHERE FirstName = 'John'; SQL statement is selecting all records (*) from the Employees table where the FirstName is 'John'. This returned a single record where the FirstName is 'John'.

Example

SELECT *
FROM Employees
WHERE Department = 'Sales';

Output

| EmployeeId | First Name | Last Name | Department |
|------------|------------|-----------|------------|
| 1 | John | Doe | Sales |
| 2 | Jane | Smith | Sales |

Explanation

The SQL command in the example demonstrates the use of the WHERE clause to filter rows in a table. In this case, only employees working in the ‘Sales’ department are returned in the result.

Example

SELECT * FROM employees
WHERE salary > 5000;

Output

| Employee_ID | First_Name | Last_Name | Email | Salary |
|-------------|------------|-----------|-------------|------- |
| 100 | Nancy | Greenberg | nancy@abc.com| 6000 |
| 105 | John | Smith | john@abc.com | 7000 |

Explanation

The example query selects all columns from the employees table where the salary is greater than 5000.

Example

SELECT *
FROM Customers
WHERE Country='Germany';

Output

| CustomerID | CustomerName | ContactName | Country |
|------------|--------------|-------------|---------|
| 2 | Ana Trujillo | Ana Trujillo | Germany |
| 5 | Berglunds snabbköp | Christina Berglund | Germany |
| 9 | Bólido Comidas preparadas | Martín Sommer | Germany |

Explanation

The WHERE clause in the SQL statement is used to extract only the records that fulfill a specified condition. In the example above, only the records where the Country is ‘Germany’ are extracted from the Customers table.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.