The WHERE keyword in SQL is used to filter records based on specific conditions. It is typically used in a SELECT, UPDATE, DELETE, or INSERT statement to specify which rows should be affected by the query.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
- Selecting specific rows:
SELECT * FROM Employees
WHERE Age > 30;
This will return all columns from the Employees table where the Age is greater than 30.
- Using multiple conditions with AND/OR:
SELECT * FROM Employees
WHERE Age > 30 AND Department = 'HR';
This will return all employees who are older than 30 and belong to the HR department.
- Using operators like
=,>,<,BETWEEN,LIKE, etc.
SELECT * FROM Employees
WHERE Salary BETWEEN 40000 AND 60000;
This returns employees with a salary between 40,000 and 60,000.
- Using
LIKEfor pattern matching:
SELECT * FROM Employees
WHERE Name LIKE 'John%';
This returns employees whose name starts with "John."
- Using
INfor matching a list of values:
SELECT * FROM Employees
WHERE Department IN ('HR', 'Finance', 'Sales');
This returns employees who work in the HR, Finance, or Sales departments.
- Using
IS NULLto check for null values:
SELECT * FROM Employees
WHERE ManagerID IS NULL;
This returns employees who do not have a manager (i.e., their ManagerID is NULL).
The WHERE clause helps in filtering the data as per the specified condition.
No comments:
Post a Comment