said in CS310 Quiz 2 Solution and Discussion:
What SQL clause is used to restrict the rows returned by a query?
The SQL clause used to restrict or filter rows based on a specific condition is the WHERE clause.
It is placed after the FROM clause and before any grouping or sorting clauses. Only the rows that meet the specified criteria (where the condition evaluates to TRUE) will be included in the result set.
Basic Syntax
SELECT column1, column2
FROM table_name
WHERE condition;
Examples of Usage
| Requirement | SQL Example |
|---|---|
| Exact Match | WHERE city = 'London' |
| Numeric Range | WHERE age > 18 |
| Pattern Matching | WHERE name LIKE 'A%' |
| Multiple Conditions | WHERE price < 100 AND stock > 0 |
A Quick Distinction: WHERE vs. HAVING
- WHERE: Filters individual rows before any grouping occurs.
- HAVING: Filters groups after the
GROUP BYclause has been applied.