NPV Queries - Problem
You're working as a financial analyst for an investment firm that needs to quickly lookup Net Present Value (NPV) data for various assets across different years.
You have two tables:
- NPV Table: Contains historical NPV data for assets, with columns
id,year, andnpv - Queries Table: Contains lookup requests for specific assets and years
Your task is to match each query with its corresponding NPV value. If a query asks for an asset-year combination that doesn't exist in the NPV table, return null for that NPV value.
Goal: Write a SQL query that returns each query along with its corresponding NPV value (or null if not found).
Input & Output
example_1.sql โ Basic NPV Lookup
$
Input:
NPV table:
| id | year | npv |
|----|------|-----|
| 1 | 2018 | 100 |
| 7 | 2020 | 30 |
| 13 | 2019 | 40 |
| 1 | 2019 | 113 |
| 2 | 2008 | 121 |
| 3 | 2009 | 12 |
| 11 | 2020 | 99 |
| 7 | 2019 | 0 |
Queries table:
| id | year |
|----|------|
| 1 | 2019 |
| 2 | 2008 |
| 3 | 2009 |
| 7 | 2018 |
| 1 | 2018 |
| 13 | 2019 |
โบ
Output:
| id | year | npv |
|----|------|------|
| 1 | 2019 | 113 |
| 2 | 2008 | 121 |
| 3 | 2009 | 12 |
| 7 | 2018 | null |
| 1 | 2018 | 100 |
| 13 | 2019 | 40 |
๐ก Note:
Each query is matched with its corresponding NPV value. Query (7, 2018) returns null because no NPV record exists for id=7 in year 2018.
example_2.sql โ All Queries Have Matches
$
Input:
NPV table:
| id | year | npv |
|----|------|-----|
| 1 | 2020 | 50 |
| 2 | 2020 | 75 |
| 3 | 2020 | 25 |
Queries table:
| id | year |
|----|------|
| 1 | 2020 |
| 2 | 2020 |
| 3 | 2020 |
โบ
Output:
| id | year | npv |
|----|------|-----|
| 1 | 2020 | 50 |
| 2 | 2020 | 75 |
| 3 | 2020 | 25 |
๐ก Note:
Perfect match scenario where every query finds its corresponding NPV value in the NPV table.
example_3.sql โ No Matches Found
$
Input:
NPV table:
| id | year | npv |
|----|------|-----|
| 1 | 2018 | 100 |
| 2 | 2019 | 200 |
Queries table:
| id | year |
|----|------|
| 1 | 2020 |
| 3 | 2018 |
| 2 | 2020 |
โบ
Output:
| id | year | npv |
|----|------|------|
| 1 | 2020 | null |
| 3 | 2018 | null |
| 2 | 2020 | null |
๐ก Note:
Edge case where none of the queries match any records in the NPV table, resulting in all null NPV values.
Constraints
- 1 โค NPV.id, Queries.id โค 104
- 1990 โค NPV.year, Queries.year โค 2050
- -108 โค NPV.npv โค 108
- 1 โค NPV table rows โค 5 ร 104
- 1 โค Queries table rows โค 5 ร 104
- Each (id, year) combination in NPV table is unique
- Each (id, year) combination in Queries table is unique
Visualization
Tap to expand
Understanding the Visualization
1
Prepare Tables
Load both Queries (patron requests) and NPV (book catalog) tables
2
Execute LEFT JOIN
Database matches each query with corresponding NPV record using optimized join algorithm
3
Handle Missing Data
Queries without matches get NULL NPV values, ensuring all queries appear in result
4
Return Complete Result
Single result set containing all queries with their NPV values or NULL
Key Takeaway
๐ฏ Key Insight: LEFT JOIN is the perfect tool for lookup operations where you need to preserve all queries and gracefully handle missing data with NULL values.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code