SUM(CASE WHEN ... THEN 1 ELSE 0 END)

Concepts covered: sqlConditionalAgg

The fundamental form is SUM(CASE WHEN condition THEN value ELSE 0 END). The CASE returns the value when the condition is TRUE, and 0 otherwise. SUM adds them all up. The result is the sum of values from matching rows; non-matching rows contribute 0 and do not affect the total. The pattern works because SUM ignores NULL and treats 0 as additive identity; both routes (NULL or 0) give the right answer. The canonical query Reading the pattern GROUP BY region collapses the orders to one row per region. Each conditional SUM filters which orders contribute: premium_revenue sums only the premium-tier orders, standard_revenue sums only standard-tier orders. total_revenue sums all orders unconditionally. order_count counts all orders unconditionally. Five columns in the output: the grouping key plus

About This Interactive Section

This section is part of the Conditional Aggregation: Beginner lesson on DataDriven, a free data engineering interview prep platform. Each section includes explanations, worked examples, and hands-on code challenges that execute in real time. SQL queries run against a live database. Python runs in a sandboxed Docker container. Data modeling problems validate against interactive schema canvases. All content is framed around what data engineering interviewers actually test at companies like Meta, Google, Amazon, Netflix, Stripe, and Databricks.

How DataDriven Lessons Work

DataDriven combines four interview rounds (SQL, Python, Data Modeling, Pipeline Architecture) with adaptive difficulty and spaced repetition. Easy problems get harder as you improve. Weak concepts resurface until you master them. Your readiness score tracks progress across every topic interviewers test. Every lesson section ends with problems you solve by writing and running real code, not by picking multiple-choice answers.