DEV Community

Yogaraj
Yogaraj

Posted on

5 SQL Join Mistakes I Made as a Learner (and How I Fixed Them)"

When I first started learning SQL, joins felt like magic—until they didn’t. Joins are incredibly powerful, but they can also be tricky when you're new. I made a lot of mistakes along the way, and in this post, I’ll share the top 5 that tripped me up (with real examples) and how I fixed them. Hopefully, they’ll save you some time and confusion!


1. Trying FULL OUTER JOIN Directly in MySQL

Mistake:

SELECT * FROM food_sales
FULL OUTER JOIN data_sales ON food_sales.ID = data_sales.ID;
Enter fullscreen mode Exit fullscreen mode

I expected this to return all matches plus unmatched rows from both tables. But MySQL threw an error.

Fix: MySQL doesn't support FULL OUTER JOIN directly. You need to simulate it using UNION of a LEFT JOIN and RIGHT JOIN.

Corrected Code:

SELECT fs.ID, fs.City, ds.Product
FROM food_sales fs
LEFT JOIN data_sales ds ON fs.ID = ds.ID

UNION

SELECT ds.ID, fs.City, ds.Product
FROM food_sales fs
RIGHT JOIN data_sales ds ON fs.ID = ds.ID;
Enter fullscreen mode Exit fullscreen mode

2. Using WHERE with Aliases Before Declaring Them

Mistake:

SELECT FS.City, FS.Category, (FS.Qty * FS.UnitPrice) AS TotalSales
FROM food_sales FS
WHERE TotalSales > 50;
Enter fullscreen mode Exit fullscreen mode

I thought I could use the alias TotalSales in the WHERE clause.

Fix: SQL doesn't recognize aliases in WHERE—they're created during the SELECT phase, which runs after WHERE.

Corrected Code:

SELECT FS.City, FS.Category, (FS.Qty * FS.UnitPrice) AS TotalSales
FROM food_sales FS
WHERE (FS.Qty * FS.UnitPrice) > 50;
Enter fullscreen mode Exit fullscreen mode

Or, use a subquery if you want cleaner filtering:

SELECT * FROM (
  SELECT FS.City, FS.Category, (FS.Qty * FS.UnitPrice) AS TotalSales
  FROM food_sales FS
) AS Sub
WHERE TotalSales > 50;
Enter fullscreen mode Exit fullscreen mode

3. Confusion Between ON and WHERE Clauses

Mistake:

SELECT *
FROM food_sales FS, data_sales DS
WHERE FS.ID = DS.ID;
Enter fullscreen mode Exit fullscreen mode

This technically works (old-style join), but it’s messy and confusing.

Fix: Always use JOIN ... ON for better readability and modern SQL syntax.

Corrected Code:

SELECT *
FROM food_sales FS
INNER JOIN data_sales DS ON FS.ID = DS.ID;
Enter fullscreen mode Exit fullscreen mode

4. Overwriting Columns with the Same Alias

Mistake:

SELECT ds.product AS Product, IFNULL(ds.product, 'No Product') AS Product
FROM data_sales ds;
Enter fullscreen mode Exit fullscreen mode

I thought both lines would be fine—but I reused the alias Product, which caused confusion.

Fix: Always use unique aliases in your SELECT clause.

Corrected Code:

SELECT ds.product AS ActualProduct, IFNULL(ds.product, 'No Product') AS CleanedProduct
FROM data_sales ds;
Enter fullscreen mode Exit fullscreen mode

5. Assuming JOINs Automatically Drop NULLs

Mistake: I thought an INNER JOIN would always remove NULL values from all columns.

But when joining on unrelated or mismatched columns, NULLs may still show up in non-joined fields.

Fix: Always check your join condition and consider using COALESCE() or IS NULL to manage missing data explicitly.

Example:

SELECT fs.City, COALESCE(ds.Product, 'Unknown') AS Product
FROM food_sales fs
LEFT JOIN data_sales ds ON fs.ID = ds.ID;
Enter fullscreen mode Exit fullscreen mode

Final Thoughts:

SQL joins are powerful tools, but they require precision and understanding. Each mistake I made taught me something valuable, and I hope by sharing these, your SQL journey becomes a little smoother. Let me know in the comments which one tripped you up—or if you have another SQL lesson to share!

Happy Querying! 😊

💭 Got suggestions, feedback, or just want to say hi?

📩 Reach me at: yogarajprof@gmail.com

Top comments (0)