SQL WHERE Clause

SQL WHERE Clause in SQL DML Statements

SQL WHERE Clause

The WHERE clause in SQL is an essential part of Data Manipulation Language (DML) statements. It is not a mandatory clause, but it plays a crucial role in filtering records and refining query results. The WHERE clause helps to restrict the number of rows affected by SQL statements like SELECT, UPDATE, and DELETE, ensuring that only the required data is processed.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Why Use the WHERE Clause?

When working with large datasets, retrieving all records at once is inefficient. The WHERE clause allows users to fetch only the relevant records that meet specific conditions, improving both performance and accuracy.

Syntax of SQL WHERE Clause

SELECT column1, column2, ..., column_n  
FROM table_name  
WHERE [condition];  

The WHERE clause is placed immediately after the FROM clause and before any other clauses like ORDER BY or GROUP BY.

Conditional Operators Used in WHERE Clause

The WHERE clause supports various conditional operators for data filtering:

Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to

Example Usage of SQL WHERE Clause

Consider a table named employees with the following structure:

emp_id emp_name department salary
101 Rahul IT 60000
102 Anjali HR 55000
103 Amit Finance 75000
104 Priya IT 70000

Example 1: Using WHERE in SELECT Statement

To fetch employees with a salary greater than 60,000:

SELECT emp_name, department, salary  
FROM employees  
WHERE salary > 60000;

Example 2: Using WHERE in UPDATE Statement

To increase the salary of employees in the IT department:

UPDATE employees  
SET salary = salary + 5000  
WHERE department = 'IT';

Example 3: Using WHERE in DELETE Statement

To remove employees from the HR department:

DELETE FROM employees  
WHERE department = 'HR';

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

The WHERE clause is a powerful feature of SQL that helps refine queries, making them efficient and precise. By applying conditions, you can control which rows are affected by SELECT, UPDATE, and DELETE statements. Mastering the WHERE clause is essential for effective database management and optimization.

For more SQL tutorials, stay connected with UpdateGadh!


sql where multiple conditions
sql where clause multiple values
having clause in sql
where clause in sql example
group by clause in sql
the sql where clause mcq
order by clause in sql
select clause in sql
sql interview questions
sql where clause example
sql where clause w3schools
sql where clause oracle

Post Comment