A data analyst runs a query in Databricks SQL that aggregates sales data by product category. The analyst notices that the query results include a category value of NULL. The analyst wants to exclude rows where the category is NULL from the aggregation. Which SQL clause should be added to the query?
The WHERE clause filters rows before aggregation, so adding WHERE category IS NOT NULL removes rows with NULL category from the input to the aggregation. This ensures the aggregation does not include NULL as a group. It is the correct way to exclude NULLs before grouping.
Why this answer
To exclude NULL categories before aggregation, use a WHERE clause with IS NOT NULL. This filters out rows with NULL category before grouping, so the aggregation does not include a NULL group. HAVING filters after aggregation and is less efficient, while ORDER BY only sorts.
Exam trap
The trap here is using HAVING instead of WHERE to filter NULLs; HAVING filters after aggregation and does not prevent the aggregation from processing NULL rows, which can be less efficient and may still include the NULL group if not properly filtered.