Count how many accounts exist for each producttype. Table definitions (from the source exercise):
SQLLevel: IntermediateGROUP BY and HAVING
Count how many accounts exist for each product_type.
Table definitions (from the source exercise):
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: GROUP BY the product type; COUNT(*) per group.
SELECT product_type, COUNT(*) AS num_accounts
FROM accounts
GROUP BY product_type;
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