INNER JOIN Deep Dive: Mistakes
Module: Joins & Relationships
SELECT * FROM orders, customers WHERE orders.customer_id = customers.customer_id;
SELECT o.*, c.customer_name FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id;
The comma syntax (FROM a, b WHERE...) is outdated. Modern INNER JOIN syntax is clearer and more explicit about the join type.
Always use explicit INNER JOIN with ON clause for better readability and maintainability
Medium
Old comma syntax is deprecated and less readable
SELECT * FROM orders INNER JOIN customers;
SELECT * FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id;
Without ON clause, every order row is combined with every customer row. For 1000 orders × 500 customers = 500,000 result rows!
Always include ON clause to specify how tables should be matched
Critical
Missing ON clause creates cartesian product
SELECT orders.order_id, customers.customer_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id;
SELECT o.order_id, c.customer_name FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id;
Using full table names repeatedly makes queries long and hard to read. Table aliases (o, c) make queries cleaner.
Use meaningful but short table aliases: customers c, orders o (not t1, t2)
Low
Verbose table names make queries hard to read
SELECT * FROM orders o INNER JOIN customers c ON LOWER(o.customer_id) = LOWER(c.customer_id);
SELECT * FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id;
Using LOWER() or other functions on join columns prevents the database from using indexes, causing slow full table scans.
Keep join conditions simple (column = column) to allow index usage
High
Functions on join columns prevent index usage