Return the name of customers with a credit score below 650 who ALSO have at least one closed account. Combining a risk…
SQLLevel: IntermediateServicing and risk analytics
Return the name of customers with a credit score below 650 who ALSO have at least one closed account. Combining a risk attribute with a behavior attribute is the standard segmentation shape.
Table definitions (from the source exercise):
CREATE TABLE customers(customer_id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT, credit_score INTEGER);
CREATE TABLE accounts(account_id INTEGER PRIMARY KEY, customer_id INTEGER, product_type TEXT, open_date TEXT, credit_limit INTEGER, status TEXT);
Hints
- Define one output row and the source-table keys.
- Check duplicates, NULLs and the order of filtering and aggregation.
Answer
Approach: Join customers to accounts, filter score < 650 (NULL scores fail this comparison, which is correct here) and status = closed. DISTINCT in case of multiple closed accounts.
SELECT DISTINCT c.name
FROM customers c
JOIN accounts a ON a.customer_id = c.customer_id
WHERE c.credit_score < 650
AND a.status = 'closed';
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