One of the most common sources of confusion in IB Computer Science SQL questions is the difference between WHERE and HAVING. Many students know that both involve conditions, but they apply those conditions at different stages of a query and to different types of data.
IB examiners often include questions specifically designed to test whether students truly understand this distinction.
Why HAVING and WHERE Both Exist
At first glance, WHERE and HAVING seem to do the same thing — they both apply conditions. However, they work on different levels of data:
- WHERE filters individual records (rows)
- HAVING filters groups of records created by GROUP BY
Understanding this difference is the key to full marks.
What Is WHERE?
The WHERE clause is used to filter records before any grouping or aggregation takes place.
WHERE:
- Applies to individual rows
- Works with raw data values
- Is evaluated early in the query
WHERE is commonly used to:
- Select records matching a condition
- Limit data before processing
For example, WHERE can:
- Select students in a specific year group
- Filter orders above a certain value
In IB terms, WHERE controls which records enter the query.
What Is HAVING?
The HAVING clause is used to filter after has been applied.
