📚 College Credit Guide ✓ UPI Study 🕐 7 min read

When Should You Use Left and Right Joins in SQL?

This article explains left and right joins in SQL, compares them with inner joins, and shows how to pick the join that keeps the rows you need.

US
UPI Study Team Member
📅 July 05, 2026
📖 7 min read
US
About the Author
The UPI Study team works directly with students on credit transfer, degree planning, and course selection. We've helped thousands of students figure out what counts toward their degree and how to finish faster without paying more than they have to. This post is written the way we'd explain it to you directly.

Left and right joins keep unmatched rows; inner joins do not. That is the whole idea, and once you see which table gets preserved, SQL result sets get much easier to read. A left join keeps every row from the left table, even when the other side has no match. A right join does the same thing for the right table. An inner join only returns rows where both tables match on the join condition. If a customer has no orders, a left join still shows that customer. If you use an inner join, that row disappears. That difference matters in database programming because the question you ask shapes the rows you get back. Are you checking who exists in a table, or only who matched another table? Those are not the same thing. A report for 500 students with enrollment gaps needs a different join than a report for 500 matched enrollments. Learn the preserved side first, then read the query from that side outward. SQL stops feeling slippery once you do that.

Vibrant close-up of code displayed on a monitor with various programming details — UPI Study

How Do Left and Right Joins Work?

A left join keeps all rows from the left table, a right join keeps all rows from the right table, and any missing match shows up as NULL on the other side. That preserved table is the part you protect, and the join direction tells you which rows must survive.

Think of two tables with 100 customers and 80 orders. A left join from customers to orders gives you 100 customer rows, not 80, because the customer table sits on the left. If 20 customers never ordered, their order columns come back as NULL. A right join flips that logic. If orders sits on the right, every order row stays, even if the customer record does not match cleanly.

The catch: The word order matters more than the word type here. SQL does not care about your story, only about which table comes first in the query. That is why a query written as customers LEFT JOIN orders reads differently from orders RIGHT JOIN customers, even though both can return the same 100 preserved rows.

I like teaching this with a simple rule: ask which table owns the rows you cannot lose. If the answer is the 12 departments in a company report, that table becomes the preserved side. If the answer is the 50 products in a catalog, keep that table on the protected side. The side you preserve gives the join its real meaning, not the word left or right by itself.

A lot of students get tripped up because they read the join from the wrong table. Start with the preserved table, then check where the NULLs land. That habit saves time in a database programming course and in real work, especially when a query returns 3 tables and 1,000 rows.

How Do Left Joins Differ From Inner Joins?

Inner joins only keep matches, so they drop any row that fails the join condition. Left and right joins belong to outer joins, which keep one full side and fill the missing partner with NULL. That difference decides whether a report shows 90 matched records or the full 120-row base table.

Join TypePreserved RowsUnmatched Rows
Inner joinOnly matchesDropped
Left joinAll left rowsNULL on right
Right joinAll right rowsNULL on left
Best useExact matchesComplete list + gaps
Typical reportMatched salesStudents without enrollments

Worth knowing: Many teams write right joins as left joins because left-to-right reading feels cleaner. That is not a law, just a habit built from years of query reviews and fewer mistakes. If your brain reads the preserved table first, the left join usually wins.

Which Rows Stay in a Left Join Result?

A left join always keeps every row from the left table, even if the right table has zero matching rows. That means you can read the result row by row and ask one simple question: did this left-side row find a partner or not?

Use customers and orders as the cleanest example. Say the customers table has 4 rows and the orders table has 2 matching rows. A left join from customers to orders returns all 4 customers. The 2 customers with no orders still appear, and their order fields show NULL. That is the whole point of the preserved side. The same logic works for students and enrollments, products and sales, or departments and employees.

Reality check: NULL does not mean zero. It means no match. That tiny difference matters in reports, filters, and counts, because a NULL order date is not the same thing as an order date of 0 days or an empty string.

Read the output from left to right and stop at the first table. If that table has 25 students, you should see 25 student rows after the join unless another filter removes some rows later in the WHERE clause. That is where people mess up. They think the join “lost” rows, but the WHERE clause often did the damage after the join already preserved them.

A sharp habit helps here: look at the left table first, then scan the right-side columns for NULLs. If you can explain why 7 rows have NULLs and 18 rows do not, you understand the join better than most beginners do.

When Should You Use Right Joins?

Right joins make sense when the table you want to preserve already sits on the right, and rewriting the query would make the logic harder to read. In practice, many teams still prefer left joins, but right joins can fit a clean 2-table report.

Database Programming is a useful place to see this pattern in a structured way, especially if you want the join logic tied to real query practice.

Database Programming UPI Study Course

Learn Database Programming Online for College Credit

This is one topic inside the full Database Programming course on UPI Study — a self-paced, online class that earns real college credit. Credits are ACE and NCCRS evaluated and transfer to partner colleges across the US and Canada. Courses start at $250 with no deadlines and lifetime access.

Explore on UPI Study →

How Do You Choose the Correct Join?

Pick the join by asking one blunt question: which table must keep all of its rows? If you answer that first, the rest of the query gets easier in under 1 minute.

  1. Start with the table whose rows must stay. If you need all 64 courses, that table belongs on the preserved side.
  2. Decide whether missing matches should still appear. If yes, use left or right join; if no, use inner join.
  3. Run a tiny sample with 5 to 10 rows. That size makes NULLs easy to spot without drowning in output.
  4. Check where the NULLs land after the join. If the wrong side shows NULLs, you picked the wrong direction.
  5. Use inner join for exact overlap, left join for a full base list, and right join only when the right table owns the rows you cannot lose.

Bottom line: A left join answers “show me everything from this table, plus matches.” A right join answers the same question from the other side. Inner join answers a narrower question: “show me only the rows that match on both sides.”

Database Fundamentals pairs well with this because join choice makes more sense once you can picture primary keys and foreign keys moving together across 2 tables.

What Examples Show Left and Right Joins Best?

The best examples use everyday tables with obvious gaps: customers and orders, products and sales, departments and employees. Those pairs make the preserved side visible in 1 glance, which is why teachers use them so often in SQL labs and exams.

If you want all 15 customers whether or not they ordered, a left join from customers to orders fits. If you want all 200 products even when no sale exists for March 2026, the product table stays preserved. If you want all 12 departments, even the one with 0 employees, a left join again gives the cleanest result. A right join can do the same job, but it often reads backward to new students.

What this means: Left joins usually win in database programming because most people read queries from left to right and think in terms of the first table they name. That habit makes reviews faster and cuts down on silly mistakes, especially when a query already joins 3 tables.

Right joins still have a place, but they can feel awkward in code shared by a team of 5 or 6 people. I would rather see one clear left join than a clever right join that makes everyone stop and reread the query. The SQL engine does not care, but the humans do.

Database Programming helps you practice these patterns with real query examples, and Data Structures and Algorithms helps when you later compare query logic to more formal problem solving.

How Can You Read NULLs Without Getting Lost?

NULLs tell you where the join found no match, and that makes them the fastest clue in the result set. A left join with 40 rows on the left and 12 NULLs on the right tells you 12 left-side rows had no partner.

Read each row from the preserved side first. Then look across to the joined columns and ask whether the values came from a match or from a gap. If the row starts with a student ID, that student still exists in the result even when the enrollment date shows NULL. If the row starts with a department name, that department stays put even when the employee count drops to NULL.

A common mistake comes from putting filters in the wrong place. If you add a WHERE clause after a left join and filter on a right-side column, you can throw away the very rows you meant to keep. That is a nasty little trap, and I think it causes more beginner errors than the join type itself.

A better habit is to test with 3 rows you understand by sight. One matched row. One unmatched row. One row with a value that looks suspicious. If the result matches your 3-row expectation, you picked the right join and you read the NULLs the right way.

How Does This Help in Database Programming Courses?

Join choice shows up early in a database programming course because it trains you to think about data shape, not just syntax. A query that returns 2 matching rows from 10 can look correct and still answer the wrong question.

That is why students should practice with reports that have missing data on purpose. A course exercise with 20 orders and 5 missing customer links teaches more than a perfect table with no gaps. Real databases almost never stay tidy for long.

Some students chase the fanciest join and miss the simple rule: keep the table whose rows matter most. That rule works in homework, exams, and real business reports. It also helps when you study online and review the query at night after a 2-hour session, because the logic stays the same no matter the screen.

Database Programming fits that kind of practice well, especially if you want repeated examples of left join, right join, and inner join behavior without guessing what the output should look like.

Frequently Asked Questions about SQL Joins

Final Thoughts on SQL Joins

Left join, right join, and inner join all answer different questions, and the best one depends on which rows you need to keep. If you want a full list with missing matches visible, use an outer join. If you want only overlap, use an inner join. That rule sounds simple, but it saves a lot of bad reports. The fastest way to read a result set is to name the preserved table first, then scan for NULLs on the other side. Do that with customers and orders, departments and employees, or products and sales, and the pattern starts to feel plain instead of strange. Right joins matter less in day-to-day work because left joins read more naturally for most teams, but the logic stays the same. Do not trust the query until you test it with 5 to 10 rows you understand. One matched row, one unmatched row, one odd row. That small habit catches the mistakes that bigger datasets hide. If you can explain which rows survive and why, you already understand the heart of SQL joins. Use that rule the next time a report looks off, and the fix usually shows itself fast.

How UPI Study credits actually work

Ready to Earn College Credit?

ACE & NCCRS approved · Self-paced · Transfer to colleges · $250/course or $99/month

More on Database Programming
© UPI Study. This article and its educational content are solely owned by UPI Study and licensed under CC BY-NC-ND 4.0. It is not free to reuse or modify. Any citation must credit UPI Study with a direct link to this page.