Explain Codes LogoExplain Codes Logo

Mysql select rows where left join is null

sql
join
left-join
subqueries
Nikita BarsukovbyNikita Barsukov·Dec 31, 2024
TLDR

To grab unmatched rows from Table1, use:

--Quick and dirty solution, or as we like to say – Fast and Furious MySQL Drift SELECT Table1.* FROM Table1 LEFT JOIN Table2 ON Table1.id = Table2.fk_id WHERE Table2.fk_id IS NULL;

The LEFT JOIN actively finds and pairs all Table1 entries with Table2. If a Table1 row doesn't find its pair in Table2, it romances with a NULL, which is what we filter for with the WHERE clause.

Making things clear: Aliases and multiple conditions

Short is sweet: Using aliases

Adding aliases turns SQL statements into easy-to-read poetry:

SELECT t1.* FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.id = t2.fk_id WHERE t2.fk_id IS NULL;

Aliases, born from using AS, provide clear and concise reference to your tables. Like nicknames for your pet cat, but for databases.

Your key to my heart: Joining on multiple criteria

Got composite keys? No problem. You can JOIN on more than one column:

SELECT t1.* FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.id = t2.fk_id AND t1.another_id = t2.another_fk_id WHERE t2.fk_id IS NULL;

The above snippet generates matches on both id and another_id, treating t2.fk_id IS NULL like that extra piece of dessert you didn't ask for, but still ate because, hey, dessert!

Composite non-matches with IN

To find unlonely t1.id not making any connections in t2.user_one or t2.user_two, IN comes to the rescue:

SELECT t1.* FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.id IN (t2.user_one, t2.user_two) WHERE t2.user_one IS NULL AND t2.user_two IS NULL;

Like checking if your friends showed up at the party (but they didn't, so you end up alone with the karaoke machine).

Optimizing SQL with the EXISTS clause

Like the friend who saves you from awkward conversations at parties, EXISTS optimizes your SQL by bailing you out early:

SELECT t1.* FROM Table1 AS t1 WHERE NOT EXISTS ( SELECT 1 FROM Table2 AS t2 WHERE t1.id = t2.fk_id );

This approach can greatly hasten things up as the query halts as soon as it locates the first match.

Dealing with NULLs like a pro

The chameleon of SQL: Understanding NULL in joins

Understand that NULL in SQL is like a chameleon. Whether it pops up from a join condition or a NULL column value, it can play very different roles.

Multiple LEFT JOINs: Divide and conquer

If you've got multiple columns to compare, break your joins down into separate LEFT JOIN operations:

SELECT t1.* FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.id = t2.user_one LEFT JOIN Table3 AS t3 ON t1.id = t3.user_two WHERE t2.user_one IS NULL AND t3.user_two IS NULL;

Here's a motto for you: Keep the logical separation clear, keep your debugging fears away.

Sneaky detour: Excluding matched rows

You may want to look beyond JOIN columns for a more accurate exclusion of matching rows:

SELECT t1.* FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.id = t2.fk_id WHERE t2.fk_id IS NULL AND t1.some_column NOT IN (<some_values>);

This ensures that a non-matching row is like that uninvited party guest: shows up, but doesn't fit in.

Advanced scenarios: Subqueries and opposites

Subqueries: Your Swiss army knife

If your table relationships or conditions resemble tangled Christmas lights, turn to subqueries and LEFT JOIN:

SELECT t1.* FROM Table1 AS t1 LEFT JOIN (SELECT id FROM Table2 WHERE some_condition) AS t2 ON t1.id = t2.id WHERE t2.id IS NULL;

Here, subqueries encapsulate complex logic like a genie in a bottle – just make sure you use your SQL wishes wisely!