Skip to content

Learn online at your pace

Databases & SQL • November 29, 2026 • 9 min read

SQL JOINs Explained Simply: INNER, LEFT and RIGHT JOIN With Examples

The Qtec Mastery

Author

SQL JOINs Explained Simply: INNER, LEFT and RIGHT JOIN With Examples

Real databases split information across several tables, so sooner or later you need to put it back together: a student's name next to their course, or every payment next to the person who made it. This guide gives you SQL joins explained simply, with every result worked out on a small Kenyan college database. By the end you will know when to use INNER, LEFT and RIGHT JOIN, how to join three tables, and how to find records that have no match.

A JOIN combines rows from two tables using a matching column. INNER JOIN keeps only rows that match in both tables. LEFT JOIN keeps every row from the left table and fills in NULL where there is no match. RIGHT JOIN does the same for the right table.

Databases from Zero, part 3 of 6. Previous: SQL for Beginners: 10 Queries You Will Use Every Day · Next: Database Design for Beginners: Tables, Keys and Normalisation

What data are we joining?

We use the same three tables as part 2: students, courses and payments. The CREATE TABLE statements are in that post. Start from empty tables and load exactly this data, plus one new course that nobody has joined yet, so that every join has something interesting to show:

INSERT INTO courses (course_name, fee_kes) VALUES
  ('Web Development', 10000.00),
  ('Python Programming', 20000.00),
  ('Cyber Security', 15000.00),
  ('Data Analysis', 20000.00);

INSERT INTO students (full_name, phone, county, course_id, enrolled_on) VALUES
  ('Wanjiku Kamau', '0712345678', 'Nairobi', 1, '2026-01-10'),
  ('Brian Otieno', '0723456789', 'Kisumu', 2, '2026-01-12'),
  ('Amina Hassan', '0734567890', 'Mombasa', 3, '2026-02-03'),
  ('Kevin Mutua', '0745678901', 'Machakos', 2, '2026-02-15'),
  ('Faith Chebet', '0756789012', 'Uasin Gishu', NULL, '2026-03-01');

INSERT INTO payments (student_id, mpesa_code, amount_kes, paid_on) VALUES
  (1, 'SKA4H7XQ2P', 10000.00, '2026-01-10 09:15:00'),
  (2, 'SKB2M9RT5L', 10000.00, '2026-01-12 14:30:00'),
  (2, 'SKC7P3WN8D', 5000.00, '2026-02-12 10:05:00'),
  (3, 'SKD5Q1ZV6H', 15000.00, '2026-02-03 16:45:00');

So courses have IDs 1 to 4, students 1 to 5 and payments 1 to 4. Keep three facts in mind: Faith has no course, Kevin and Faith have not paid, and nobody is on Data Analysis (course 4).

What is an INNER JOIN?

An INNER JOIN returns only the rows where the join condition matches in both tables. Here we match each student's course_id to the course with the same ID:

SELECT s.full_name, c.course_name
FROM students s
INNER JOIN courses c ON s.course_id = c.course_id
ORDER BY s.student_id;
full_namecourse_name
Wanjiku KamauWeb Development
Brian OtienoPython Programming
Amina HassanCyber Security
Kevin MutuaPython Programming

The letters s and c are aliases, short nicknames for the tables, so you can write s.full_name instead of students.full_name. Notice who is missing. Faith has no course, so there is nothing to match and she disappears. Data Analysis has no students, so it disappears too. In MySQL, plain JOIN means the same as INNER JOIN.

What is a LEFT JOIN?

A LEFT JOIN keeps every row from the left table (the one after FROM), whether or not it has a match. Where there is no match, the right table's columns come back as NULL:

SELECT s.full_name, c.course_name
FROM students s
LEFT JOIN courses c ON s.course_id = c.course_id
ORDER BY s.student_id;
full_namecourse_name
Wanjiku KamauWeb Development
Brian OtienoPython Programming
Amina HassanCyber Security
Kevin MutuaPython Programming
Faith ChebetNULL

Faith is back. Use a LEFT JOIN whenever the question is "all of these, plus extra details if they exist", for example "every student, and their course if they have one".

What is a RIGHT JOIN?

A RIGHT JOIN is the mirror image: it keeps every row from the right table. Here, every course, with its students if it has any:

SELECT c.course_name, s.full_name
FROM students s
RIGHT JOIN courses c ON s.course_id = c.course_id
ORDER BY c.course_id, s.student_id;
course_namefull_name
Web DevelopmentWanjiku Kamau
Python ProgrammingBrian Otieno
Python ProgrammingKevin Mutua
Cyber SecurityAmina Hassan
Data AnalysisNULL

Data Analysis now appears with no student, and Faith has gone because she matches no course. In practice, most developers rewrite a RIGHT JOIN as a LEFT JOIN with the tables swapped (FROM courses c LEFT JOIN students s ...). The result is the same and it is easier to read when every join in a query points the same way.

Does MySQL have a FULL OUTER JOIN?

No. A FULL OUTER JOIN keeps unmatched rows from both sides, and PostgreSQL and SQL Server support it, but MySQL 8 does not. The usual workaround is to combine a LEFT JOIN and a RIGHT JOIN with UNION:

SELECT s.full_name, c.course_name
FROM students s
LEFT JOIN courses c ON s.course_id = c.course_id
UNION
SELECT s.full_name, c.course_name
FROM students s
RIGHT JOIN courses c ON s.course_id = c.course_id
ORDER BY full_name;
full_namecourse_name
NULLData Analysis
Amina HassanCyber Security
Brian OtienoPython Programming
Faith ChebetNULL
Kevin MutuaPython Programming
Wanjiku KamauWeb Development

UNION removes the duplicate rows that both halves return. MySQL sorts NULL first in ascending order, which is why the empty course is at the top. One caution: UNION also removes rows that are genuinely identical, such as two students with the same name on the same course. If that matters, include the IDs in the SELECT so every row is unique.

How do I join three tables?

Add one JOIN per extra table, each with its own ON condition. Here is every payment with the student's name and course:

SELECT s.full_name, c.course_name, p.mpesa_code, p.amount_kes
FROM students s
INNER JOIN courses c ON s.course_id = c.course_id
INNER JOIN payments p ON p.student_id = s.student_id
ORDER BY p.paid_on;
full_namecourse_namempesa_codeamount_kes
Wanjiku KamauWeb DevelopmentSKA4H7XQ2P10000.00
Brian OtienoPython ProgrammingSKB2M9RT5L10000.00
Amina HassanCyber SecuritySKD5Q1ZV6H15000.00
Brian OtienoPython ProgrammingSKC7P3WN8D5000.00

Brian appears twice because he made two payments. A join produces one row per match, so a one-to-many relationship repeats the "one" side.

Fee balances: joins plus GROUP BY

Mix join types with the GROUP BY you learned in part 2, and you get a report the bursar would actually use:

SELECT s.full_name,
       c.fee_kes,
       COALESCE(SUM(p.amount_kes), 0) AS paid_kes,
       c.fee_kes - COALESCE(SUM(p.amount_kes), 0) AS balance_kes
FROM students s
INNER JOIN courses c ON s.course_id = c.course_id
LEFT JOIN payments p ON p.student_id = s.student_id
GROUP BY s.student_id, s.full_name, c.fee_kes
ORDER BY s.student_id;
full_namefee_kespaid_kesbalance_kes
Wanjiku Kamau10000.0010000.000.00
Brian Otieno20000.0015000.005000.00
Amina Hassan15000.0015000.000.00
Kevin Mutua20000.000.0020000.00

The LEFT JOIN to payments keeps Kevin even though he has paid nothing, and COALESCE turns his NULL total into 0. Faith is left out by the INNER JOIN to courses, which is right: she has no fee yet.

How do I find students with no payments?

This is one of the most useful patterns in SQL. Do a LEFT JOIN, then keep only the rows where the right side found nothing:

SELECT s.student_id, s.full_name
FROM students s
LEFT JOIN payments p ON p.student_id = s.student_id
WHERE p.payment_id IS NULL
ORDER BY s.student_id;
student_idfull_name
4Kevin Mutua
5Faith Chebet

We test p.payment_id because it is the primary key, so it can only be NULL when no payment row matched. The same pattern finds courses with no students, products that never sold or customers who never ordered.

What are the most common JOIN mistakes?

  • Filtering the right table in WHERE. LEFT JOIN payments p ON p.student_id = s.student_id WHERE p.amount_kes > 5000 silently drops Kevin and Faith, because their NULL amounts fail the test. Your LEFT JOIN has become an INNER JOIN. Put the condition in the ON clause instead: ON p.student_id = s.student_id AND p.amount_kes > 5000.
  • Ambiguous column names. Both students and courses have a course_id, so SELECT course_id fails with error 1052 ("Column 'course_id' in field list is ambiguous"). Always prefix with the alias.
  • Double counting. Joining two different one-to-many tables at once (say payments and exam results) multiplies the rows, and SUM gives inflated totals. Total each one separately first.
  • Joining on the wrong columns. Joining p.payment_id = s.student_id runs without error but gives nonsense. Join a foreign key to the primary key it points at.

Joins work best on well-designed tables with clear keys, which is the subject of part 4.

Practise joins with real reports

SQL for Data Analysis builds the reports businesses ask for, from joins and grouping to clean summaries, taught online with a certificate on completion.

See the course →

Also useful: DBMS 1: Database Foundations and SQL (10 weeks, KES 20,000)

Frequently asked questions

Is JOIN the same as INNER JOIN?

Yes. In MySQL, JOIN on its own is an inner join. Writing INNER just makes your intention clearer.

Which is faster, LEFT JOIN or INNER JOIN?

Choose the one that answers your question correctly. Speed depends far more on indexes on the joined columns, which part 5 explains.

Can I join a table to itself?

Yes, that is called a self join. You list the table twice with different aliases, for example to match each staff member with their manager in the same staff table.

Why does my JOIN return more rows than the table has?

Because one row on one side matches several rows on the other. Brian has two payments, so joining students to payments shows him twice. That is expected behaviour, not a bug.

Comments (0)

Comments appear after a quick review. Your email is never shown.

You Might Also Like

What Is a Database? A Beginner's Guide With Real Kenyan Examples

What Is a Database? A Beginner's Guide With Real Kenyan Examples

Every app you use, from your school portal to M-Pesa, keeps its information some...

Read More →
SQL for Beginners: 10 Queries You Will Use Every Day

SQL for Beginners: 10 Queries You Will Use Every Day

Most of the SQL people write at work comes down to about ten patterns. This guid...

Read More →
Database Design for Beginners: Tables, Keys and Normalisation

Database Design for Beginners: Tables, Keys and Normalisation

Most messy databases start life as a spreadsheet that grew too big: names typed...

Read More →