SQL Boy
TutorialsPlayground

Format & Validate

SQL FormatterSQL MinifierSyntax Validator

Convert

JSON to SQLCSV to SQLSQL to JSONRegex to SQLExcel to SQLSQL Dialect Convertersoon

Visualize

ER Diagram GeneratorSQL Schema Diff

Generate

SQL Mock Data Generator

Analyze

SQL Query ExplainerSQL Query Analyzer

Workflow hubs

FormattingConversionSchemaAnalysis
View all tools
Daily ChallengeInterviewsCheat SheetBlog

© 2026 SQL Boy. Built for developers.

ContactPrivacyTermsAI Usage PolicyEditorial StandardsAboutRSS Feed

New conversation

What can I help with?

Ask about this page, get a hint on a challenge, or explore SQL concepts.

I know what's on this page and can give answers grounded in SQL Boy content.

Current page

Investments In 2016

/challenges/investments-in-2016

Usage status will load after login
SQL Boy
TutorialsPlayground

Format & Validate

SQL FormatterSQL MinifierSyntax Validator

Convert

JSON to SQLCSV to SQLSQL to JSONRegex to SQLExcel to SQLSQL Dialect Convertersoon

Visualize

ER Diagram GeneratorSQL Schema Diff

Generate

SQL Mock Data Generator

Analyze

SQL Query ExplainerSQL Query Analyzer

Workflow hubs

FormattingConversionSchemaAnalysis
View all tools
Daily ChallengeInterviewsCheat SheetBlog
SQL Challenges/Investments in 2016
Challenge DetailMediumReviewed 2026-04-20

Investments in 2016

Calc total TIV_2016 for all policyholders who have the same TIV_2015 as one or more other policyholders, and are not located in the same city (lat, lon) as any other policyholder. Round to 2 decimals.

AggregationHavingSubqueries

This challenge now meets the current minimum standard for sitemap inclusion and independent indexing.

2026-01-10Medium

Investments in 2016

Calc total TIV_2016 for all policyholders who have the same TIV_2015 as one or more other policyholders, and are not located in the same city (lat, lon) as any other policyholder. Round to 2 decimals.

Available Tables:
SQL Editor
Loading...

What This Challenge Is Testing

This problem combines two different set filters on the same table: duplicate prior-year value and unique location. The same pattern appears in fraud screening, policy analysis, and entity quality checks where an item must satisfy several population-level rules before it contributes to a final metric.

Topic Tags

AnalyticsSubqueriesAggregations

Schema At A Glance

insurance
pidtiv_2015tiv_2016latlon

Solution Approaches

01

Filter with two grouped subqueries

SELECT
  ROUND(SUM(tiv_2016), 2) AS tiv_2016
FROM insurance
WHERE tiv_2015 IN (
  SELECT tiv_2015
  FROM insurance
  GROUP BY tiv_2015
  HAVING COUNT(*) > 1
)
  AND (lat, lon) IN (
    SELECT lat, lon
    FROM insurance
    GROUP BY lat, lon
    HAVING COUNT(*) = 1
  )

This is the standard solution. One grouped subquery finds prior-year values shared by multiple policyholders, and the other identifies locations that appear exactly once.

02

Compute the flags once in CTEs

WITH shared_tiv AS (
  SELECT tiv_2015
  FROM insurance
  GROUP BY tiv_2015
  HAVING COUNT(*) > 1
),
unique_locations AS (
  SELECT lat, lon
  FROM insurance
  GROUP BY lat, lon
  HAVING COUNT(*) = 1
)
SELECT
  ROUND(SUM(i.tiv_2016), 2) AS tiv_2016
FROM insurance i
JOIN shared_tiv s
  ON i.tiv_2015 = s.tiv_2015
JOIN unique_locations u
  ON i.lat = u.lat
 AND i.lon = u.lon

The CTE version makes the two eligibility rules easier to inspect separately. That is helpful when you need to debug which condition excludes a specific row in a larger production dataset.

Common Pitfalls

The subtle bug is mixing up duplicate and unique criteria. `tiv_2015` must be shared with at least one other row, while `(lat, lon)` must be unique. Reversing either condition changes the business meaning completely.

Frequently Asked Questions

Why are latitude and longitude treated as one condition together?

Because location uniqueness is defined by the full coordinate pair. Either column alone is not enough to identify whether two policyholders occupy the same place in this schema.

What broader SQL idea does this teach?

It teaches multi-stage eligibility filtering: using grouped subsets to define row populations before applying a final aggregation.

Concept Guides

Use these guides when you want the fuller explanation behind the SQL pattern this challenge is testing.

Mastering SQL GROUP BY: From Basics to Advanced Aggregations

Learn how GROUP BY transforms rows into summaries. Watch data collapse into groups with animations and master COUNT, SUM, AVG, and HAVING.

SQL Conditional Aggregation: Beyond Basic GROUP BY

Turn multiple queries into one with conditional aggregation. Learn how CASE WHEN inside aggregate functions creates powerful single-query reports and pivot tables.

SQL Subqueries Explained: Queries Within Queries

Unlock the power of nested queries. Learn when to use subqueries in SELECT, WHERE, and FROM clauses with practical, runnable examples.

Topic Hubs

If this problem exposed a deeper weakness, use a topic hub to move through a curated cluster instead of random article hopping.

Analytics

Analytics SQL and business metrics

A practical cluster for cohorts, funnels, customer value, and the kinds of reporting questions teams actually ask every week.

Advanced SQL

Advanced query patterns

Use this path when you are moving beyond beginner SELECT queries into CTEs, window functions, and conditional logic.

Practice Path

If you want the rotating practice flow, open this same question from the daily stream. If you want interview-style packaging, continue into the interview pool after this rep.

Back to ChallengesOpen In DailyBrowse Interviews

Related Challenges

Customers Who Bought All ProductsMedium

Return only the customers whose distinct purchased product set covers every product listed in the catalog.

Friend Requests I: Overall Acceptance RateMedium

Calculate overall friend request acceptance rate while separating request volume from accepted pairs cleanly.

Percentage of Users AttendedMedium

Calculate contest participation rate by dividing each contest’s registrations by the total user base, not by contest-local counts.

© 2026 SQL Boy. Built for developers.

ContactPrivacyTermsAI Usage PolicyEditorial StandardsAboutRSS Feed