DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 85 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 85 of my 100-day full-stack engineering streak! 🎯 Yesterday, I explored structural data intersections using the INNER JOIN. Today, I stepped up into handling data asymmetry and tracking unmatched records across schemas by mastering: SQL LEFT JOIN and RIGHT JOIN! 🔗📊

In real-world databases, relationships aren't always perfectly symmetrical. Sometimes you need to retrieve a complete list of entries from one table, regardless of whether they have matching relational logs in another. Today, I engineered queries to do exactly that!


🧠 Unpacking Outer Joins: LEFT vs. RIGHT Logic

Unlike an INNER JOIN which completely drops rows if no mutual match exists, Outer Joins preserve the integrity of a primary master table:

1. The LEFT JOIN (Mastering the Left Side)

  • It returns all rows from the left table (Table A), plus the matched rows from the right table (Table B).
  • The NULL Behavior: If a row on the left has no matching reference pointer on the right, the query still fetches the left record, but automatically populates the right table's columns with NULL fields.
  • Real-world example: Finding every registered customer, even those who haven't placed an order yet.

2. The RIGHT JOIN (Inverting the Focus)

  • It behaves exactly like the left join, but completely flips the priority. It returns all records from the right table (Table B), along with matching pairs from the left table (Table A).
  • If there are rows in the right table that don't match any entity on the left, the left table's columns stream as NULL.

🛠️ Conceptual Look at the Day 85 Join Queries

Here is how I structured my multi-table relational scripts today to capture asymmetrical data structures fluidly:


sql
-- LEFT JOIN: Fetching all students, matching courses if they exist
SELECT student.id, student.name, course.course_name 
FROM student 
LEFT JOIN course 
ON student.id = course.student_id;

-- RIGHT JOIN: Inverting focus to fetch all course data points
SELECT student.name, course.course_id, course.course_name
FROM student 
RIGHT JOIN course 
ON student.id = course.student_id;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)