Count Transactions per Item, Sorted by Count Then Name
Quick Overview
Count how many transactions each item has in a transactions table and return the items sorted by that count from highest to lowest, with ties broken alphabetically by name. It tests GROUP BY with COUNT and a two-key ORDER BY that makes the output order deterministic.
Count Transactions per Item, Sorted by Count Then Name
Company: PayPal
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Return each `item_name` in `transactions` with its number of transactions as `transaction_count`. Order by `transaction_count` descending, then `item_name` ascending.
```hint Where the sort key comes from
The first sort key does not exist in the table. Work out at which step of the query it becomes available.
```
### Input Table(s)
Table `transactions`
| Column | Type | Meaning |
|---|---|---|
| id | INT | Transaction identifier. Primary key. |
| item_name | VARCHAR(100) | Name of the item bought. NOT NULL. |
| amount | DECIMAL(10,2) | Amount paid, a positive decimal. NOT NULL. |
| transaction_date | DATE | Day of the transaction. NOT NULL. |
Primary key: `id`. Each row is one transaction.
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| item_name | VARCHAR | An item that appears in `transactions`. |
| transaction_count | INT | Number of rows in `transactions` with that `item_name`. |
One row per distinct `item_name`. Order by `transaction_count` descending, then by `item_name` ascending. Each `item_name` appears once, so this ordering is unique.
### Example
`transactions`:
| id | item_name | amount | transaction_date |
|---|---|---|---|
| 1 | Apple | 1.50 | 2024-01-01 |
| 2 | Banana | 0.75 | 2024-01-02 |
| 3 | Apple | 2.00 | 2024-01-03 |
| 4 | Cherry | 3.00 | 2024-01-04 |
| 5 | Banana | 0.80 | 2024-01-05 |
| 6 | Apple | 1.75 | 2024-01-06 |
| 7 | Cherry | 3.25 | 2024-01-07 |
Expected output:
| item_name | transaction_count |
|---|---|
| Apple | 3 |
| Banana | 2 |
| Cherry | 2 |
Apple appears 3 times, and Banana and Cherry each appear 2 times. Sorted by count descending and then alphabetically, the order is Apple (3), Banana (2), Cherry (2).
### Constraints and Clarifications
- The table has between 1 and 10,000 rows.
- `item_name` is never NULL and is at most 100 characters long. Names are compared exactly as stored.
- `amount` is a positive decimal. `amount` and `transaction_date` do not affect the count.
- Every row is a separate transaction, so two rows for the same item on the same day with the same amount count twice.
- Items with the same `transaction_count` are tied and must be listed in ascending `item_name` order.
Overview: Count how many transactions each item has in a transactions table and return the items sorted by that count from highest to lowest, with ties broken alphabetically by name. It tests GROUP BY with COUNT and a two-key ORDER BY that makes the output order deterministic.
Return each item_name in transactions with its number of transactions as transaction_count. Order by transaction_count descending, then item_name ascending.
Input Table(s)
Table transactions
Column
Type
Meaning
id
INT
Transaction identifier. Primary key.
item_name
VARCHAR(100)
Name of the item bought. NOT NULL.
amount
DECIMAL(10,2)
Amount paid, a positive decimal. NOT NULL.
transaction_date
DATE
Day of the transaction. NOT NULL.
Primary key: id. Each row is one transaction.
Output Contract
Column
Type
Meaning
item_name
VARCHAR
An item that appears in transactions.
transaction_count
INT
Number of rows in transactions with that item_name.
One row per distinct item_name. Order by transaction_count descending, then by item_name ascending. Each item_name appears once, so this ordering is unique.
Example
transactions:
id
item_name
amount
transaction_date
1
Apple
1.50
2024-01-01
2
Banana
0.75
2024-01-02
3
Apple
2.00
2024-01-03
4
Cherry
3.00
2024-01-04
5
Banana
0.80
2024-01-05
6
Apple
1.75
2024-01-06
7
Cherry
3.25
2024-01-07
Expected output:
item_name
transaction_count
Apple
3
Banana
2
Cherry
2
Apple appears 3 times, and Banana and Cherry each appear 2 times. Sorted by count descending and then alphabetically, the order is Apple (3), Banana (2), Cherry (2).
Constraints and Clarifications
The table has between 1 and 10,000 rows.
item_name
is never NULL and is at most 100 characters long. Names are compared exactly as stored.
amount
is a positive decimal.
amount
and
transaction_date
do not affect the count.
Every row is a separate transaction, so two rows for the same item on the same day with the same amount count twice.
Items with the same
transaction_count
are tied and must be listed in ascending
item_name
order.