What are semi-joins and anti-joins, and how are they typically expressed in SQL?
Last updated
A semi-join returns left rows with at least one match in another table, without duplicating them per match. Write it with
EXISTSorIN.An anti-join returns left rows with no match. Write it with
NOT EXISTSorLEFT JOIN ... WHERE right_key IS NULL.Prefer
NOT EXISTSoverNOT IN: if the subquery returns any NULL,NOT INyields no rows.
- https://duckdb.org/docs/lts/sql/query_syntax/from
- https://learn.microsoft.com/en-us/azure/databricks/sql/language-manual/sql-ref-syntax-qry-select-join
- https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-join
- https://learn.microsoft.com/de-de/azure/databricks/sql/language-manual/sql-ref-syntax-qry-select-join
- https://dev.mysql.com/doc/refman/8.1/en/semijoins-antijoins.html
- https://medium.com/@avinash14/semi-join-anti-join-the-most-misunderstood-performance-tools-in-sql-afb6e103bd25
- https://www.geeksforgeeks.org/dbms/difference-between-anti-join-and-semi-join/
- https://blog.jooq.org/a-probably-incomplete-comprehensive-guide-to-the-many-different-ways-to-join-tables-in-sql/