Skip to main content

NPV Queries - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min readAsked at: Amazon
Practice this problem

Problem Statement

Table: NPV

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| year          | int     |
| npv           | int     |
+---------------+---------+
(id, year) is the primary key (combination of columns with unique values) of this table.
The table has information about the id and the year of each inventory and the corresponding net present value.

 

Table: Queries

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| year          | int     |
+---------------+---------+
(id, year) is the primary key (combination of columns with unique values) of this table.
The table has information about the id and the year of each inventory query.

 

Write a solution to find the npv of each query of the Queries table.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

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   |
| 7    | 2019   |
| 7    | 2020   |
| 13   | 2019   |
+------+--------+
Output: 
+------+--------+--------+
| id   | year   | npv    |
+------+--------+--------+
| 1    | 2019   | 113    |
| 2    | 2008   | 121    |
| 3    | 2009   | 12     |
| 7    | 2018   | 0      |
| 7    | 2019   | 0      |
| 7    | 2020   | 30     |
| 13   | 2019   | 40     |
+------+--------+--------+
Explanation: 
The npv value of (7, 2018) is not present in the NPV table, we consider it 0.
The npv values of all other queries can be found in the NPV table.

Approach Overview

Problem Overview: The database contains two tables: NPV(id, year, npv) and Queries(id, year). For every query pair (id, year), return the corresponding NPV value. If the pair does not exist in the NPV table, return 0 instead.

Approach 1: LEFT JOIN with IFNULL (O(n) time, O(1) extra space)

The most direct solution uses a LEFT JOIN between Queries and NPV on both id and year. A left join guarantees every row from Queries appears in the result even when no matching record exists in NPV. When a match is missing, SQL returns NULL for the npv column. Use IFNULL(npv, 0) (or COALESCE) to replace that NULL value with 0. The database performs the join once across the two tables, giving linear performance relative to the number of rows scanned. This approach is concise, efficient, and the typical pattern used for handling missing relational data.

This technique relies on core concepts from SQL and database querying. Specifically, you match rows using a composite join condition Queries.id = NPV.id AND Queries.year = NPV.year. The result set directly produces the required columns: the query identifiers plus the computed NPV value.

Approach 2: Correlated Subquery with COALESCE (O(n) time, O(1) extra space)

Another option retrieves the NPV value using a correlated subquery. For each row in Queries, run a subquery that searches the NPV table for the matching (id, year). If the subquery returns NULL or no result, wrap it with COALESCE(..., 0) to return 0. This approach is logically straightforward because it mirrors the problem statement: for each query, look up the NPV value.

While the complexity is still roughly linear for small datasets due to database optimization, correlated subqueries can be less efficient on large tables compared to joins. Most query planners internally transform this pattern into a join anyway. Still, understanding both forms is useful when working with SQL joins and relational lookups.

Recommended for interviews: The LEFT JOIN approach is the expected solution. It shows you understand relational joins and how to handle missing matches using IFNULL or COALESCE. The subquery version demonstrates equivalent logic but is less commonly preferred because joins scale better and are easier to optimize.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
LEFT JOIN with IFNULLO(n)O(1)Standard relational lookup when you must return all rows from Queries even if no match exists
Correlated Subquery with COALESCEO(n)O(1)When expressing logic per-row is clearer or when joins are restricted

Video Solution

LeetCode 1421 Interview SQL Question with Detailed Explanation | Practice SQL • Everyday Data Science • 6,394 views views

Frequently Asked Questions

Is NPV Queries easy or hard?
NPV Queries is labeled Easy with an acceptance rate above 80%. The challenge mainly tests understanding of SQL joins and handling NULL values when a matching record does not exist.
NPV Queries Python/Java solution
The problem is categorized as a database problem, so the expected solution is written in SQL rather than Python or Java. The query uses LEFT JOIN and IFNULL/COALESCE to retrieve the NPV value or return 0 when no matching record exists.
How to solve NPV Queries in O(n)?
Perform a LEFT JOIN from Queries to NPV using the conditions Queries.id = NPV.id AND Queries.year = NPV.year. Then use IFNULL(NPV.npv, 0) to replace missing values. The database scans and joins rows once, producing the result in linear time.
What is the best approach for NPV Queries?
The best approach uses a LEFT JOIN between the Queries and NPV tables on both id and year, then replaces missing values using IFNULL or COALESCE. This ensures every query row appears in the output while returning 0 for missing matches. The solution runs in O(n) time and uses constant extra space.
Is NPV Queries asked at Google/Amazon/Meta?
NPV Queries represents a common SQL interview pattern involving joins and handling NULL values. Variants of join-based lookup questions frequently appear in database interview rounds at companies like Amazon, Meta, and other data-focused teams.
What data structure is used in NPV Queries?
The problem uses relational database tables and SQL join operations. Conceptually, the join works like a keyed lookup between two datasets using the composite key (id, year). SQL engines internally use indexing or hash joins to perform this matching efficiently.
What is the time complexity of NPV Queries?
The typical SQL solution using a LEFT JOIN runs in O(n) time relative to the number of rows processed by the query planner. Databases use indexing and join algorithms to efficiently match rows. The space complexity is O(1) because no additional data structures are required beyond the query result.

Ready to solve this problem?

Practice NPV Queries with our built-in code editor and test cases.

Practice on FleetCode