Count Quality Defects Per Record
Count defects across customer_key, external_id, email, status, and phone for every affected record.
- CASE expressions
- String functions
- NULL handling
- Filtering
- Sorting
Exercise brief
Understand the request
Data quality operations lead A quarantine queue prioritizes records by the number of missing or blank critical attributes.
A quarantine queue prioritizes records by the number of missing or blank critical attributes. Count defects across customer_key, external_id, email, status, and phone for every affected record.
Return
- Return record_id and quality_issue_count.
- Order by quality_issue_count descending, then record_id.
Constraints
- Treat NULL as missing for every field and trimmed-empty text as missing for email or status.
- Return only rows with at least one issue.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
quality_records
record_idINTEGERcustomer_keyTEXTexternal_idTEXTemailTEXTstatusTEXTphoneTEXT
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Represent each rule as a 1-or-0 CASE flag.
Hint 2
Blank-aware text rules need both IS NULL and a trimmed-empty comparison.
Hint 3
Add the five flags and filter with their underlying predicates.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT record_id, CASE WHEN customer_key IS NULL THEN 1 ELSE 0 END + CASE WHEN external_id IS NULL THEN 1 ELSE 0 END + CASE WHEN email IS NULL OR TRIM(email) = '' THEN 1 ELSE 0 END + CASE WHEN status IS NULL OR TRIM(status) = '' THEN 1 ELSE 0 END + CASE WHEN phone IS NULL THEN 1 ELSE 0 END AS quality_issue_count FROM quality_records WHERE customer_key IS NULL OR external_id IS NULL OR email IS NULL OR TRIM(email) = '' OR status IS NULL OR TRIM(status) = '' OR phone IS NULL ORDER BY quality_issue_count DESC, record_id;Why this works
Row-level rule flags make a quality score explainable: each defect contributes exactly one unit and can later be expanded into named rule columns.
Success check
The five-field blank record ranks first and all partially incomplete records are retained.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| record_id | quality_issue_count |
|---|---|
| 1007 | 5 |
| 1006 | 2 |
| 1012 | 2 |
| 1002 | 1 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
SQL Aggregations
Build reliable SQL metrics from aggregate functions through grain, fan-out, weighted ratios, rollups, percentiles, and approximate counts.
CTEs & Window Functions
Practice modular CTE pipelines, deterministic window analytics, period comparisons, deduplication, frames, and gaps-and-islands.
CASE Statements & Conditional Logic
Build NULL-aware classifications, precedence-safe decisions, flags, scores, and guarded calculations with portable CASE expressions.
Open the interactive workspace and practice across SQL topics.