How do you filter rows by condition?
Filtering rows by condition in SQL is done with the keyword WHERE. It lets you select only the records that match a given criterion.
Syntax:
sql
SELECT Column1, Column2, ...
FROM Table_Name
WHERE Condition;Example conditions
- Comparing against a specific value:
sql
SELECT *
FROM Employees
WHERE Age = 30;- Selects every employee whose
Ageequals 30.
- Greater than or less than:
sql
SELECT *
FROM Employees
WHERE Age > 25;- Selects employees older than 25.
- Several conditions (
AND,OR):
sql
SELECT *
FROM Employees
WHERE Age > 25 AND DepartmentID = 101;- Selects employees older than 25 who work in department 101.
- Checking set membership (
IN):
sql
SELECT *
FROM Employees
WHERE DepartmentID IN (101, 102, 103);- Selects employees working in departments 101, 102, or 103.
- Pattern matching (
LIKE):
sql
SELECT *
FROM Employees
WHERE Name LIKE 'A%';- Selects employees whose names start with the letter "A".
Notes:
WHEREis applied before grouping (GROUP BY) and sorting (ORDER BY).- Different conditions can be combined for precise filtering.
Put simply,
WHERElets you select only the rows that match certain rules or criteria.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.