What are semi-joins and anti-joins, and how are they typically expressed in SQL?

Last updated 3 July 2026

  • A semi-join returns left rows with at least one match in another table, without duplicating them per match. Write it with EXISTS or IN.

  • An anti-join returns left rows with no match. Write it with NOT EXISTS or LEFT JOIN ... WHERE right_key IS NULL.

  • Prefer NOT EXISTS over NOT IN: if the subquery returns any NULL, NOT IN yields 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/
All SQL interview questions