What are semi-joins and anti-joins? Explain how EXISTS/NOT EXISTS or IN/NOT IN can implement them as analytical filters.

Quick Overview

Asks what semi-joins and anti-joins are and how EXISTS, NOT EXISTS, IN and NOT IN express them as SQL filters. Tests whether you understand how these filters differ from a plain join, how NULLs affect each form, and how to apply them to funnel and cohort questions.

What are semi-joins and anti-joins? Explain how EXISTS/NOT EXISTS or IN/NOT IN can implement them as analytical filters.

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What are semi-joins and anti-joins? Explain how `EXISTS`/`NOT EXISTS` or `IN`/`NOT IN` can implement them as analytical filters. ```hint Compare with a plain join Think about what an inner join returns when one left row matches three right rows, and what you want when the only question is whether a match exists. ``` ```hint Watch the NULLs Work through `x NOT IN (1, NULL)` with three-valued logic for a value of `x` that is not 1. ``` ### What a Strong Answer Covers - Definitions of both operations, including that the output keeps only left columns and does not repeat a left row for multiple matches - How `EXISTS` and `IN` express a semi-join, and how `NOT EXISTS`, `NOT IN` and the `LEFT JOIN ... IS NULL` pattern express an anti-join - The NULL behavior that makes `NOT IN` unsafe, with an example - Why a plain join used as a filter can duplicate rows, and how the database plans these queries - A realistic analytical use, such as users who did one action but not another within a time window ### Follow-up Questions - When would you write the anti-join as `LEFT JOIN ... WHERE right.key IS NULL`, and which column must you test? - How would you find users who viewed a product in a given week but did not buy it within 7 days of the view? - Does `SELECT DISTINCT` after an inner join give the same result as a semi-join? When does it differ?

Overview: Asks what semi-joins and anti-joins are and how EXISTS, NOT EXISTS, IN and NOT IN express them as SQL filters. Tests whether you understand how these filters differ from a plain join, how NULLs affect each form, and how to apply them to funnel and cohort questions.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What are semi-joins and anti-joins? Explain how EXISTS/NOT EXISTS or IN/NOT IN can implement them as analytical filters.

What a Strong Answer Covers Guidance

  • Definitions of both operations, including that the output keeps only left columns and does not repeat a left row for multiple matches
  • How EXISTS and IN express a semi-join, and how NOT EXISTS , NOT IN and the LEFT JOIN ... IS NULL pattern express an anti-join
  • The NULL behavior that makes NOT IN unsafe, with an example
  • Why a plain join used as a filter can duplicate rows, and how the database plans these queries
  • A realistic analytical use, such as users who did one action but not another within a time window

Follow-up Questions Guidance

  • When would you write the anti-join as LEFT JOIN ... WHERE right.key IS NULL , and which column must you test?
  • How would you find users who viewed a product in a given week but did not buy it within 7 days of the view?
  • Does SELECT DISTINCT after an inner join give the same result as a semi-join? When does it differ?