The article
Query your table as a graph.Petabytes. Subsecond. No data movement.

Filtering packages that have a security advisory should leave you with package records. An inner join can instead produce one row per advisory, changing the meaning of a later count or report. Choosing a semi join makes the intended output explicit: keep qualifying records without expanding them into every matching pair.
This guide explains that behavior, the difference between left and right semi joins, and how to express them with EXISTS, IN, and Spark SQL. The examples use one small software inventory to make duplicates, missing matches, and NULL values visible.
What Is a Semi Join in SQL?
A semi join returns rows from one input when at least one matching row exists in another input. It returns columns only from the retained input. The other input determines which rows qualify.
For a left semi join, the retained input is the left table. If a package matches several advisories, that package row still appears only once. If it matches none, it disappears. This is the behavior described by Spark SQL's semi join definition.
The qualification is per input row, not per distinct value. If the retained input already contains two identical rows, both can survive. A semi join prevents multiplication by matching rows on the other side; it does not deduplicate its own input.
That distinction matters when the input is itself a query result. If an earlier join has already repeated a package, adding a semi join later will not repair that repetition. Establish the required row grain before applying the existence filter.
How Does a Semi Join Work?
Consider these fictional packages rows. Each package_id identifies one package-version record; package_name is a display label.
| package_id | package_name |
|---|---|
| 101 | codec-core |
| 102 | http-kit |
| 103 | image-utils |
| 104 | parser-base |
The advisories table contains:
| advisory_id | package_id | severity |
|---|---|---|
| ADV-A | 101 | high |
| ADV-B | 101 | medium |
| ADV-C | 103 | high |
| ADV-D | NULL | high |
For each package row, an equality-based semi join asks whether any advisory has the same package_id. Package 101 qualifies through two advisories but contributes one row. Package 103 qualifies through one. Packages 102 and 104 do not qualify. The unassigned advisory, ADV-D, matches no package through ordinary equality.
The result contains package IDs 101 and 103. Adding another advisory for 101 would leave that result unchanged.
In a real inventory, choose a key that identifies the relevant package version and ecosystem. Matching only a display name could qualify an unrelated version with the same name. Semi join semantics control row multiplication; the join predicate still determines whether the relationship is correct.

This row-by-row explanation describes the result, not a mandatory execution algorithm. An optimizer can implement the same logic using different plans, including indexed lookups or a materialized set of matching keys. MySQL's semijoin documentation describes several such strategies.
SQL Semi Join Syntax
A common way to express a semi join is a correlated EXISTS subquery:
SELECT p.package_id, p.package_name
FROM packages AS p
WHERE EXISTS (
SELECT 1
FROM advisories AS a
WHERE a.package_id = p.package_id
)
ORDER BY p.package_id;The outer query supplies the returned columns. The subquery tests whether a qualifying advisory exists for the current package. PostgreSQL documents this as an existence test that produces at most one output row per outer row, regardless of matching-row count. PostgreSQL subquery expressions describe the semantics.
Some engines also expose SEMI JOIN directly. Spark accepts LEFT SEMI JOIN; the explicit join keywords are dialect-specific, so do not assume that syntax works everywhere an EXISTS query does.
Types of Semi Joins
Left and right describe which input contributes the output. They do not mean that unmatched rows are retained, as they are in an outer join.
| Operation | Output columns | Matching-row behavior |
|---|---|---|
| Inner join | Either input | Returns every matching pair |
| Left semi join | Left input only | Keeps each left row that has a match |
| Right semi join | Right input only | Keeps each right row that has a match |
| Left anti join | Left input only | Keeps each left row that has no match |
An anti join reverses the existence condition. It belongs alongside semi joins when comparing filtering choices, but its result contains the nonmatching rows.
Semi joins are directional: retaining packages answers a different question from retaining advisories. Decide which records the report must contain before choosing the orientation.
Left Semi Join Explained
With packages on the left and advisories on the right, the left semi join returns:
| package_id | package_name |
|---|---|
| 101 | codec-core |
| 103 | image-utils |
The two advisories for codec-core establish the same fact: that its package record qualifies. Their individual IDs and severities are not part of this result.
This makes the result suitable for counting package records with advisories or passing those records into another workflow. Counting these output rows yields two packages, while counting the matching pairs would yield three.
A left outer join answers a different question. It retains packages 102 and 104 as well, filling the advisory columns with NULL, and still produces two matching rows for package 101. Choose it when you need every package alongside optional advisory details.
Right Semi Join Explained
For the same pair of inputs, a right semi join retains advisory rows whose package exists. Express that portably by putting advisories in the outer query:
SELECT a.advisory_id, a.package_id, a.severity
FROM advisories AS a
WHERE EXISTS (
SELECT 1
FROM packages AS p
WHERE p.package_id = a.package_id
)
ORDER BY a.advisory_id;This returns ADV-A, ADV-B, and ADV-C. ADV-D is excluded because its NULL package ID does not satisfy the equality condition. Both advisories for package 101 survive because they are separate rows in the retained input.
The logical operation and the SQL spelling are separate concerns. SQL Server's operator reference includes a Right Semi Join logical operator. That does not make RIGHT SEMI JOIN portable SQL syntax. In Spark, swap the inputs and use LEFT SEMI JOIN instead.
SQL Semi Join Examples
Compare the existence query with an inner join that selects only package columns:
SELECT p.package_id, p.package_name
FROM packages AS p
INNER JOIN advisories AS a
ON a.package_id = p.package_id
ORDER BY p.package_id, a.advisory_id;The result contains codec-core twice and image-utils once. Selecting only p columns does not change the inner join's matching-pair behavior.
Adding DISTINCT and changing the ordering to ORDER BY p.package_id produces the same two rows as the semi join for this dataset because the selected columns include the package's unique ID. That equivalence needs care: selecting only a nonunique package name could collapse separate package records. Use an existence condition when existence is the actual requirement.
To find package records without a matching advisory, use the anti-join form:
SELECT p.package_id, p.package_name
FROM packages AS p
WHERE NOT EXISTS (
SELECT 1
FROM advisories AS a
WHERE a.package_id = p.package_id
)
ORDER BY p.package_id;This returns http-kit and parser-base. It means no matching advisory exists in this table, not that the packages are free of vulnerabilities.
How to Perform a Semi Join Using EXISTS
Put the complete qualifying condition inside the correlated subquery. To retain packages with a high-severity advisory:
SELECT p.package_id, p.package_name
FROM packages AS p
WHERE EXISTS (
SELECT 1
FROM advisories AS a
WHERE a.package_id = p.package_id
AND a.severity = 'high'
)
ORDER BY p.package_id;The result remains codec-core and image-utils. The medium-severity advisory does not satisfy the predicate, but codec-core has another advisory that does.
SELECT 1 supplies a constant; it neither counts matches nor limits the subquery to one row. EXISTS tests for a returned row, even when the selected value is NULL, as illustrated in Microsoft's EXISTS reference.
Keep the correlation condition. Removing a.package_id = p.package_id asks whether any high-severity advisory exists anywhere. Because one does, every package would pass. Qualifying both sides with aliases helps expose that mistake during review.
How to Perform a Semi Join Using IN
IN is a concise membership test when matching one key:
SELECT p.package_id, p.package_name
FROM packages AS p
WHERE p.package_id IN (
SELECT a.package_id
FROM advisories AS a
)
ORDER BY p.package_id;This returns the same package rows as the original EXISTS query. Repeated 101 values in the subquery do not repeat the outer row, so adding DISTINCT inside this subquery is unnecessary for correctness.
However, IN and EXISTS do not always produce the same Boolean value. For package 102, PostgreSQL evaluates this membership test to unknown because there is no equal value and the subquery includes NULL. The correlated EXISTS test is false. Both exclude the package in a positive WHERE filter, which accepts only true. See PostgreSQL's IN semantics.
Negation changes the outcome. Replacing IN with NOT IN here returns no packages: known matches fail, and the remaining comparisons are unknown. NOT EXISTS returns packages 102 and 104 as intended. The PostgreSQL NOT IN reference explains why a NULL in the subquery prevents unmatched values from passing NOT IN.
Prefer EXISTS when several predicates must refer to the same related row. Use IN when a key-membership expression makes the condition clearer.
Semi Join in Spark SQL
Spark exposes the operation directly:
SELECT p.package_id, p.package_name
FROM packages AS p
LEFT SEMI JOIN advisories AS a
ON p.package_id = a.package_id
AND a.severity = 'high'
ORDER BY p.package_id;This returns codec-core and image-utils. The right input can supply columns for the join condition, but only the left input supplies output columns. To return advisories instead, reverse the inputs:
SELECT a.advisory_id, a.package_id, a.severity
FROM advisories AS a
LEFT SEMI JOIN packages AS p
ON a.package_id = p.package_id
ORDER BY a.advisory_id;That returns ADV-A, ADV-B, and ADV-C. Spark's documented grammar provides [LEFT] SEMI, rather than a RIGHT SEMI form. Spark's join reference lists the supported forms.
Ordinary = does not match null keys to each other. Spark also provides null-safe equality, <=>, which treats two nulls as equal. Use it only when that is the intended relationship; it changes which rows qualify.
Semi Join Performance and Query Optimization
A semi join states that the query needs existence rather than every match. That can avoid unnecessary result expansion, but it does not guarantee a particular algorithm or a faster query in every workload.
Check the plan before changing syntax. MySQL can transform eligible IN and EXISTS predicates into semijoins and choose strategies such as first-match lookup or materialization. Two differently written queries may therefore get similar plans. Neither spelling is a universal performance winner.
Index the lookup where appropriate. For the PostgreSQL example, a candidate index is:
CREATE INDEX advisories_package_severity_idx
ON advisories (package_id, severity);It can support equality lookups by package and severity. PostgreSQL's multicolumn-index documentation explains how leading-column constraints affect B-tree scans. Whether this index helps depends on table size, selectivity, and the rest of the workload; the tiny example does not establish a speed benefit.
Measure representative execution. In PostgreSQL, prefix a query with EXPLAIN (ANALYZE, BUFFERS) to inspect actual execution and buffer activity. Compare estimated and actual row counts, repeated scans, and the work required to find matches. ANALYZE here executes the query. The EXPLAIN guide explains how to interpret the plan.
Account for distributed work. In Spark, broadcasting a sufficiently small lookup side can reduce shuffle work, while a larger input may require another strategy. Statistics and runtime information influence those choices. Spark's performance-tuning guide describes broadcast selection, hints, and adaptive execution. Inspect the chosen plan instead of assuming that an existence filter eliminates scans or network traffic.
When to Use a Semi Join in SQL
Use a semi join when another dataset determines eligibility while the output should retain its original row grain: packages with advisories, invoices with payments, or events belonging to an approved account set. It is particularly useful before aggregation, where multiplying rows would inflate counts or sums.
Use an inner join when the output needs matching-row details. Use an anti join when the question asks which records lack a match. If you need the number of advisories per package, aggregate the advisory rows; existence alone discards that multiplicity.
The package example can also lead to a different question: which applications depend on an affected package through several intermediate dependencies, and along which paths? A recursive SQL query can follow those chains. A graph model offers another way to express the application, package, and advisory relationships over existing tables.
PuppyGraph is a real-time, zero-ETL graph query engine that reads SQL databases, data warehouses, and lakehouses in place, including Iceberg and Delta Lake tables, with no data movement into a separate graph database. It compiles graph queries into a plan of node and edge operators that runs in its own distributed engine, which optimizes specifically for multi-hop traversals. PuppyGraph describes the engine as supporting agents across 3,000 tables, datasets measured in petabytes, and subsecond graph queries, with latency depending on workload and deployment. You connect sources, use the built-in AI assistant to propose a node-and-edge model for approval, and query it with openCypher or Gremlin while the same tables remain available to SQL. AI agents can query that model through MCP; the engine rejects references the model does not define and returns feedback they can use to correct the query. The architecture overview explains the connection, modeling, and query path.



Conclusion
A semi join filters one input by matching-row existence while preserving that input's multiplicity. Use EXISTS for a clear correlated condition, IN for membership, and Spark's LEFT SEMI JOIN when explicit syntax fits. Check null handling before negating a condition, and choose performance changes from execution plans rather than syntax alone.
Try the forever-free PuppyGraph Developer Edition and book a demo with the team to see how openCypher and Gremlin queries follow dependency paths across warehouse and lakehouse tables, with no graph-specific ETL, when your package investigation extends beyond direct advisory matches.





