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, and npv
  • 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
Library Analogy: Patron Requests โ†” Book CatalogPatron Requests (Queries)๐Ÿ“š "Finance 2018" (id=1)๐Ÿ“š "Economics 2020" (id=2)๐Ÿ“š "Math 2019" (id=3)๐Ÿ“š "Physics 2021" (id=4)Book Catalog (NPV Table)๐Ÿ“š Finance 2018 โ†’ Shelf A10๐Ÿ“š Economics 2019 โ†’ Shelf B5๐Ÿ“š Math 2019 โ†’ Shelf C3๐Ÿ“š History 2020 โ†’ Shelf D7LEFT JOINMatch RequestsLibrarian Response (Result)โœ… Finance 2018 โ†’ Found at Shelf A10โŒ Economics 2020 โ†’ Not Availableโœ… Math 2019 โ†’ Found at Shelf C3โŒ Physics 2021 โ†’ Not Available๐Ÿ” LEFT JOIN ProcessStep 1: Database loads both tables into memoryStep 2: Uses hash join or merge join to efficiently match recordsStep 3: Keeps ALL queries, adds NULL for unmatched NPV valuesResult: Every patron gets a response - either book location or "not available"๐Ÿ’ก Key Insight: LEFT JOIN MagicLEFT JOIN ensures no query is forgotten - all requests get responses,even when the requested data doesn't exist (returns NULL)
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.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 10
24.7K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen