Thursday, December 19, 2024

SQL WHERE Keyword

 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:

  1. 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.

  1. 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.

  1. 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.

  1. Using LIKE for pattern matching:
SELECT * FROM Employees
WHERE Name LIKE 'John%';

This returns employees whose name starts with "John."

  1. Using IN for 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.

  1. Using IS NULL to 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