SQL: How to Use Where Clause in SQL

The WHERE clause is used to filter the results of a SELECT statement based on one or more specified conditions. It is used to retrieve only the data that meets certain criteria, rather than retrieving all data from a table. The WHERE clause can be used in conjunction with conjunction operators, such as AND and OR, to filter results based on multiple conditions. The AND operator specifies that both conditions must be true for a row to be included in the result set, while the OR operator specifies that at least one condition must be true for a row to be included. The WHERE clause and conjunction operators are essential tools in SQL for filtering data and retrieving only the data that is relevant to your analysis.

WHERE Clause

The WHERE clause in SQL is used to filter the results of a SELECT, UPDATE, or DELETE statement based on certain conditions. The basic syntax for using the WHERE clause is:

SELECT column1, column2, …
FROM table_name
WHERE condition;

For example, to select all columns and rows from the “employees” table where the “employee_id” is equal to 1:

SQL
SELECT * FROM employees
WHERE employee_id = 1;

You can use comparison operators such as =, <>, >, <, >=, <=, IS NULL, IS NOT NULL, BETWEEN, LIKE, IN, and NOT IN to define the conditions.

SQL
SELECT * FROM employees
WHERE hire_date >= '2022-01-01' AND salary < 50000;

This SELECT statement will retrieve all the columns and rows from the “employees” table where the hire_date is greater than or equal to ‘2022-01-01’ and the salary is less than 50000.

You can also use logical operators such as AND, OR, & NOT to combine multiple conditions.

SQL
SELECT * FROM employees
WHERE (first_name = 'John' OR last_name = 'Smith') AND hire_date > '2022-01-01';

This SELECT statement will retrieve all the columns and rows from the “employees” table where the first_name is ‘John’ or last_name is ‘Smith’ and hire_date is greater than ‘2022-01-01’.

You can also use sub-queries in the WHERE clause to filter the results based on the results of another SELECT statement.

AND and OR Conjunctive Operators

In SQL, the AND and OR operators are used to combine multiple conditions in the WHERE clause of a SELECT, UPDATE, or DELETE statement.

The AND operator is used to retrieve rows that meet multiple conditions. For example, the following SQL statement will select all rows from the “employees” table where the “salary” is greater than 50000 and the “hire_date” is greater than ‘2022-01-01’:

SQL
SELECT * FROM employees
WHERE salary > 50000 AND hire_date > '2022-01-01';

The OR operator is used to retrieve rows that meet at least one of multiple conditions. For example, the following SQL statement will select all rows from the “employees” table where the “first_name” is ‘John’ or the “last_name” is ‘Smith’:

SQL
SELECT * FROM employees
WHERE first_name = 'John' OR last_name = 'Smith';

You can use parentheses to group conditions and control the order of evaluation for the AND and OR operators. For example, the following SQL statement will select all rows from the “employees” table where the “first_name” is ‘John’ or the “last_name” is ‘Smith’ and the “hire_date” is greater than ‘2022-01-01’:

SQL
SELECT * FROM employees
WHERE (first_name = 'John' OR last_name = 'Smith') AND hire_date > '2022-01-01';

It’s very important to use the parentheses correctly to ensure that the conditions are evaluated in the intended order.

UPDATE Query

In SQL, the UPDATE statement is used to modify existing data in a table. The basic syntax for updating data in a table is:

UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE some_column = some_value;

For example, to update the “first_name” and “last_name” of an employee with “employee_id” 1 in the “employees” table:

SQL
UPDATE employees
SET first_name = 'Jane', last_name = 'Doe'
WHERE employee_id = 1;

In this example, the SET clause specifies the columns and new values to be updated, and the WHERE clause specifies which rows should be updated (based on the employee_id being 1 in this case).

You can also update multiple rows at once by omitting the WHERE clause:

SQL
UPDATE employees
SET salary = salary + 1000;

This will update the salary of all the employees by increasing it by 1000.

It’s essential to use a WHERE clause in the UPDATE statement, otherwise, it will update all rows in the table, which may not be intended.

DELETE Query

In SQL, the DELETE statement is used to delete existing data from a table. The basic syntax for deleting data from a table is:

DELETE FROM table_name
WHERE some_column = some_value;

For example, to delete an employee with “employee_id” 1 from the “employees” table:

SQL
DELETE FROM employees
WHERE employee_id = 1;

In this example, the WHERE clause specifies which rows should be deleted (based on the employee_id being 1 in this case).

You can also delete multiple rows at once by omitting the WHERE clause:

SQL
DELETE FROM employees;

This will delete all rows from the “employees” table.

It’s important to use a WHERE clause in the DELETE statement, otherwise, it will delete all rows in the table, which may not be intended.

It’s important to note that when you delete data from a table, it will be permanently deleted and cannot be recovered. So, be very careful when using this statement.

The WHERE clause in SQL is used to filter the results of a SELECT statement based on one or more specified conditions. It is used to retrieve only the data that meets certain criteria, rather than retrieving all data from a table. Additionally, conjunction operators, such as AND and OR, can be used in conjunction with the WHERE clause to filter results based on multiple conditions.

The AND operator specifies that both conditions must be true for a row to be included in the result set, while the OR operator specifies that at least one condition must be true for a row to be included. The WHERE clause and conjunction operators are essential tools in SQL for filtering data and retrieving only the data that is relevant to your analysis. However, it is important to use these tools carefully and accurately to ensure that your results are accurate and complete.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.