Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
QA interview question
Databases, SQL & BISQL languageJuniorCommonPractical

Write one SQL query that calculates several conditional counts such as passed, failed, and error results.

Answer

Use conditional aggregation: turn each condition into 1 or 0 with CASE and then SUM those values, optionally grouping by build, suite, day or another dimension. This avoids separate queries for every status and makes it easy to reconcile the subtotal counts against the overall row count.

Strong answer includes

  • uses SUM with CASE or a dialect-equivalent filtered aggregate
  • can add GROUP BY for per-build or per-team metrics
  • reconciles conditional totals with the total population

Practical example

Solution · Count several conditions in one query
Dialect · Portable SQLRuntime · SQLite sample DB

Runs against the resettable SQLite sample database used by the playground.

SQL
SQLite · sample DB · browser sandbox
Result
Run the query to see the result.

Turn each condition into 1 or 0 and aggregate it. Add GROUP BY to calculate the same metrics per build, suite, team, or day.

Expected result: One row per group with total and per-condition counts.

#sql#aggregation#case#qa-metrics

Sources

PostgreSQL documentationPostgreSQL Global Development Group · Relational modelling, SQL, joins, constraints, transactions, concurrency, indexes, query plans and recovery