From customers, return the number of customers with a known credit score (alias scored) and the number missing one…
SQLLevel: FoundationAggregates
From customers, return the number of customers with a known credit score (alias scored) and the number missing one (alias unscored).
Table definitions (from the source exercise):
CREATE TABLE customers(customer_id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT, credit_score INTEGER);
Hints
- Define one output row and the source-table keys.
- Check duplicates, NULLs and the order of filtering and aggregation.
Answer
Approach: COUNT(column) skips NULLs. For the missing ones, COUNT(*) minus COUNT(column) or SUM(CASE WHEN ... IS NULL ...).
SELECT COUNT(credit_score) AS scored,
COUNT(*) - COUNT(credit_score) AS unscored
FROM customers;
The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.
Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT