Skip to main content

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

  1. Comparing against a specific value:
sql
SELECT * FROM Employees WHERE Age = 30;
  • Selects every employee whose Age equals 30.
  1. Greater than or less than:
sql
SELECT * FROM Employees WHERE Age > 25;
  • Selects employees older than 25.
  1. Several conditions (AND, OR):
sql
SELECT * FROM Employees WHERE Age > 25 AND DepartmentID = 101;
  • Selects employees older than 25 who work in department 101.
  1. Checking set membership (IN):
sql
SELECT * FROM Employees WHERE DepartmentID IN (101, 102, 103);
  • Selects employees working in departments 101, 102, or 103.
  1. Pattern matching (LIKE):
sql
SELECT * FROM Employees WHERE Name LIKE 'A%';
  • Selects employees whose names start with the letter "A".

Notes:

  • WHERE is applied before grouping (GROUP BY) and sorting (ORDER BY).
  • Different conditions can be combined for precise filtering.

Put simply, WHERE lets you select only the rows that match certain rules or criteria.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.