AnalystPath

Group Tenants by Shared Rent

SQLMediumMid level~15 min

Problem

Table: `Tenants`

```text
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| tenant_id | int |
| tenant_name | varchar |
| rent | int |
+-------------+---------+
tenant_id is the primary key for this table.
Each row gives a tenant's ID, name, and the monthly rent they pay.
```

A building manager wants to bundle tenants into rent groups for a survey. A group must contain **at least two** tenants, and every tenant in a group pays the **same rent**. All tenants paying the same rent must land in the same group. A tenant whose rent amount is unique belongs to no group.

A group's `tier_id` is the rank of its rent in **ascending** order: the cheapest qualifying rent is tier 1, the next is tier 2, and so on.

Write a solution to report each grouped tenant together with their `tier_id`.

Return the result table ordered by `tier_id` ascending, then by `tenant_id` ascending.

Tables

Example rows — the live problem includes the full dataset.

Tenants
tenant_idtenant_namerent
1Dana4000
2Eli4000
3Gil5500

Expected output

Your answer should return 5 rows with the columns tenant_id, tenant_name, rent, tier_id.

Starter code (SQL)

SELECT *
FROM Tenants;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions