The table monthly_cards_issued records how many cards of each credit card product were issued in each month. For each card_name, return the maximum minus the minimum monthly issued_amount as difference. Order by difference descending, then card_name ascending.
Input Table(s)
Table monthly_cards_issued (no declared primary key; each (card_name, issue_month, issue_year) combination is unique):
| Column | Type | Meaning |
|---|
| issue_month | INT | Month of issuance, 1 to 12 |
| issue_year | INT | Year of issuance, 2000 to 2100 |
| card_name | VARCHAR(100) | Card product name, non-empty |
| issued_amount | INT | Number of cards of this product issued in that month, 1 to 1,000,000 |
Output Contract
One row per distinct card_name:
| Column | Type | Meaning |
|---|
| card_name | VARCHAR | Card product name |
| difference | INTEGER | MAX(issued_amount) - MIN(issued_amount) over all of that card's rows |
Order by difference descending, then card_name ascending. Each card name appears once, so the order is fully determined.
Example
monthly_cards_issued:
+-------------+------------+----------------+---------------+
| issue_month | issue_year | card_name | issued_amount |
+-------------+------------+----------------+---------------+
| 11 | 2023 | Aurora Cash | 40000 |
| 12 | 2023 | Aurora Cash | 52000 |
| 1 | 2024 | Aurora Cash | 47000 |
| 1 | 2024 | Harbor Travel | 30000 |
| 11 | 2023 | Nova Business | 15000 |
| 1 | 2024 | Nova Business | 9000 |
| 2 | 2024 | Nova Business | 26000 |
| 12 | 2023 | Summit Rewards | 81000 |
| 1 | 2024 | Summit Rewards | 69000 |
+-------------+------------+----------------+---------------+
Expected result:
+----------------+------------+
| card_name | difference |
+----------------+------------+
| Nova Business | 17000 |
| Aurora Cash | 12000 |
| Summit Rewards | 12000 |
| Harbor Travel | 0 |
+----------------+------------+
Nova Business ranges from 9,000 to 26,000, so its difference is 17,000. Aurora Cash (40,000 to 52,000) and Summit Rewards (69,000 to 81,000) tie at 12,000 and are ordered by name. Harbor Travel has one month, so its maximum and minimum are equal and the difference is 0.
Constraints and Clarifications
-
The table has 1 to 10,000 rows.
-
The spread covers every row of a card across all months and years, not one year at a time.
-
Each
(card_name, issue_month, issue_year)
combination appears at most once, so there are no duplicate months to remove.
-
card_name
and
issued_amount
are never NULL.
-
A card with a single row is still returned, with a difference of 0.