Summarize Rental Income per Landlord
Beginner Mode
Scenario
You work for a real estate company that manages properties for multiple landlords and need to summarize total rental income per landlord.
Task
Write a Snowflake SQL query that:
- Deduplicates
{{ ref("properties_df") }}and{{ ref("landlords_df") }}(the input data may contain duplicate rows) - Joins properties to landlords on
landlord_idso that only landlords who own at least one property are included - Concatenates
first_nameandlast_name(separated by a space) into a column calledlandlord_name - Calculates
total_rental_incomeby summing therentcolumn per landlord - Groups results by
landlord_idandlandlord_name
Schema
properties_df
| Column | Type | Description |
|---|---|---|
| property_id | Integer | Unique property identifier |
| landlord_id | Integer | ID of the landlord who owns the property |
| property_type | String | Type of property (Apartment, Condo, House, etc.) |
| rent | Float | Monthly rent for the property |
| square_feet | Integer | Total square footage of the property |
| city | String | City where the property is located |
landlords_df
| Column | Type | Description |
|---|---|---|
| landlord_id | Integer | Unique landlord identifier |
| first_name | String | First name of the landlord |
| last_name | String | Last name of the landlord |
| String | Email address of the landlord | |
| phone | String | Phone number of the landlord |
Example
properties_df:
| property_id | landlord_id | property_type | rent | square_feet | city |
|---|---|---|---|---|---|
| 1 | 101 | Apartment | 1500 | 1000 | Seattle |
| 2 | 101 | Condo | 1200 | 800 | Seattle |
| 3 | 102 | House | 2000 | 1500 | Bellevue |
| 4 | 103 | Apartment | 1800 | 1200 | Redmond |
| 5 | 103 | Condo | 1000 | 700 | Redmond |
landlords_df:
| landlord_id | first_name | last_name | phone | |
|---|---|---|---|---|
| 101 | John | Smith | [email protected] | 555-123-4567 |
| 102 | Jane | Doe | [email protected] | 555-234-5678 |
| 103 | Bob | Johnson | [email protected] | 555-345-6789 |
| 104 | Mary | Williams | [email protected] | 555-456-7890 |
| 105 | Jack | Brown | [email protected] | 555-567-8901 |
Expected Output:
| landlord_id | landlord_name | total_rental_income |
|---|---|---|
| 101 | John Smith | 2700.0 |
| 102 | Jane Doe | 2000.0 |
| 103 | Bob Johnson | 2800.0 |
Note: Landlords 104 and 105 are excluded because they have no properties.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Broadcom