Where clause
WHERE Clause
In DBMS (Database Management Systems), the
WHERE clause is a conditional filter used in SQL (Structured Query
Language) to specify which rows should be returned, updated, or deleted in a
query. It plays a critical role in refining the results by applying conditions
to the data.
Role in DBMS
In a relational database management system (RDBMS),
data is organized into tables consisting of rows and columns. The WHERE
clause allows users to apply selection operations on this data, which is
a fundamental operation in relational algebra—specifically, it
represents the σ (sigma) operation, which performs selection based on
predicates.
Definition of WHERE Clause
The WHERE clause is used in SQL statements to filter
records that fulfill a specified condition. It is applied in SELECT, UPDATE,
DELETE, and other SQL statements.
Importance of the WHERE Clause
- Data
Filtering: Ensures only required data is
fetched or affected.
- Efficient Query Execution: Reduces
data load on memory by narrowing down the results.
- Security and Control: Prevents
accidental modification or deletion of all records.
- Foundation
for Advanced Queries: Essential in subqueries,
joins, aggregations, etc.
Logical Operation
The condition in a WHERE clause is evaluated as a boolean
expression. Each row in the table is tested against this condition:
- If
TRUE, the row is included in the result.
- If
FALSE, the row is excluded.
- If
the result is NULL (unknown), the row is also excluded because SQL
uses three-valued logic: TRUE, FALSE, and UNKNOWN.
Use in SQL
Statements
The WHERE clause can be used in:
SQL Statement |
Purpose of WHERE Clause |
SELECT |
Retrieves specific rows |
UPDATE |
Modifies specific rows |
DELETE |
Removes specific rows |
INSERT (with subqueries) |
Filters rows in subqueries |
Syntax
SELECT column1, column2, ... FROM table_name WHERE
condition;
Example
Suppose we have a table called Students:
StudentID |
Name |
Age |
Department |
1 |
Anjali |
20 |
CSE |
2 |
Bhargavi |
22 |
ECE |
3 |
Chitra |
21 |
CSE |
4 |
Devi |
23 |
MECH |
Query:
SELECT * FROM Students WHERE Department = 'CSE';
Output:
StudentID |
Name |
Age |
Department |
1 |
Anjai |
20 |
CSE |
3 |
Chitra |
21 |
CSE |
Common Operators in WHERE Clause
Operator |
Description |
Example |
= |
Equal to |
Age = 21 |
> |
Greater than |
Age > 20 |
< |
Less than |
Age < 22 |
>= |
Greater than or equal to |
Age >= 22 |
<= |
Less than or equal to |
Age <= 21 |
<> or != |
Not equal to |
Department <> 'CSE' |
AND |
Combines multiple conditions |
Age > 20 AND Department='CSE' |
OR |
Either condition must be true |
Age = 20 OR Age = 23 |
NOT |
Negates a condition |
NOT Department = 'MECH' |
BETWEEN |
Range of values |
Age BETWEEN 20 AND 22 |
IN |
Matches any value in a list |
Department IN ('CSE','ECE') |
LIKE |
Pattern matching (used with text) |
Name LIKE 'A%' |
IS NULL |
Checks for NULL values |
Department IS NULL |
WHERE with Other SQL Statements
1.
SELECT
SELECT * FROM Students WHERE Age >
21;
2. UPDATE
UPDATE Students SET Department = 'IT'
WHERE Name = 'David';
3. DELETE
DELETE
FROM Students WHERE Age < 21;
Example:
SELECT Department, COUNT(*) FROM Students WHERE Age
> 20 GROUP BY Department HAVING COUNT(*) > 2;
Notes
- The
WHERE clause filters before the final output is generated.
- It
does not support column aliases created in the SELECT clause.
- For
text comparisons, use single quotes: 'text'.
Comments
Post a Comment