Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← MySQL for Data Analysis Expert · Lesson 11 of 15

Module Ten

📖 Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
11
Module 10: MySQL for Data Analysis Expert – Joins & Beyond

Module 10: MySQL for Data Analysis Expert – Joins & Beyond

Hello, young data analyst! Welcome to Module 10. In this module, we will learn how to connect two or more tables together. This is like putting puzzle pieces side by side to see the whole picture. We will use JOINS to combine information from different tables. By the end, you will be able to answer even bigger questions using MySQL. Let's jump in!

Learning Objectives

  • Understand what a JOIN is and why we use it.
  • Learn the most common type: INNER JOIN.
  • Use LEFT JOIN to include all rows from one table.
  • Combine tables using a shared column (like a student ID).
  • Write queries that pull data from multiple tables.
  • Think about how tables are connected in real life.

Warm-up Story: The School Festival Puzzle 🧩

Imagine your school is having a big festival. You have two lists. List A has student names and their class. List B has student names and their favourite snack. But the lists are separate! You want to make one big list that shows each student's name, class, and favourite snack. To do this, you need to join the two lists by matching the student names. This is exactly what a JOIN does in MySQL. It brings information together to make a complete story.

Main Lessons

Lesson 1: What is a JOIN?

Definition: A JOIN is a way to combine data from two or more tables using a common column.

Why it's important: Data is often stored in separate tables to keep it organized. JOINs let us bring it back together.

Simple explanation: Imagine you have two boxes. Box A has names, Box B has ages. You want to know each person's age. You match the names to find the ages.

Real-life example: A school has a table for students and a table for grades. JOIN them to see each student's grades.

School example: Teacher has a list of students and a list of their project marks. JOIN to see who got what.

Home example: You have a list of family members and a list of their favourite foods. JOIN to make a family menu.

Nigerian example: A shop has a table for customers and a table for orders. JOIN to see what each customer bought.

Illustration:

  Table A (Names)   Table B (Ages)
  -------           -------
  Chidi             10
  Amina             12
  Bola              11

  JOIN → One big table: Chidi-10, Amina-12, Bola-11
  

Mini summary: A JOIN combines tables.

Lesson 2: The Key – Linking Columns

Definition: A key is a column that is common between tables, like a student ID.

Why it's important: The key tells MySQL how to match rows.

Simple explanation: It's like a secret code that is the same in both lists.

Real-life example: Your school ID number links your name to your grades.

School example: Student ID is used in attendance and marks tables.

Home example: Your phone number links your contacts to their addresses.

Nigerian example: BVN (Bank Verification Number) links a customer to their bank accounts.

Illustration:

  Students: id (1,2,3), name
  Grades: student_id (1,2,3), subject, score
  The 'id' and 'student_id' are the keys.
  

Mini summary: Keys are the matching columns.

Lesson 3: INNER JOIN – The Most Common Join

Definition: INNER JOIN returns only rows that have a match in both tables.

Why it's important: It's used when you only want data that exists in both tables.

Simple explanation: It's like a Venn diagram – only the overlapping part.

Real-life example: You want students who have both a name AND a grade.

School example: List only students who are registered AND have submitted homework.

Home example: List family members who have both a birthday AND a favourite colour.

Nigerian example: Customers who have both a phone number AND made an order.

Illustration:

  Table A       Table B
  id name       id city
  1  Chidi      1  Lagos
  2  Amina      3  Abuja
  INNER JOIN → 1-Chidi-Lagos (only common id 1)
  

Mini summary: INNER JOIN shows only matching rows.

Lesson 4: LEFT JOIN – Keep All from Left

Definition: LEFT JOIN returns all rows from the left table, and matching rows from the right.

Why it's important: Sometimes we want all data from the first table, even if there's no match in the second.

Simple explanation: It's like the left table is the boss – we keep all its rows.

Real-life example: You want all students, and if they have grades, show them. If not, show NULL.

School example: Show all teachers, and if they have classes, show those.

Home example: List all toys, and if they have batteries, show that.

Nigerian example: Show all markets, and if they have a stall rental, show it.

Illustration:

  Left table   Right table
  id name      id city
  1  Chidi     1  Lagos
  2  Amina     3  Abuja
  LEFT JOIN → 1-Chidi-Lagos, 2-Amina-NULL (no city for Amina)
  

Mini summary: LEFT JOIN keeps all rows from the left.

Lesson 5: RIGHT JOIN – Keep All from Right

Definition: RIGHT JOIN returns all rows from the right table, and matching rows from the left.

Why it's important: Sometimes we want all data from the second table.

Simple explanation: The right table is the boss – we keep all its rows.

Real-life example: You want all cities, and if there are students from them, show them.

School example: Show all clubs, and if students belong, show those.

Home example: List all rooms, and if they have furniture, show that.

Nigerian example: Show all states, and if there are shops, show them.

Illustration:

  Left table   Right table
  id name      id city
  1  Chidi     1  Lagos
  2  Amina     3  Abuja
  RIGHT JOIN → 1-Chidi-Lagos, 3-NULL-Abuja (no name for Abuja)
  

Mini summary: RIGHT JOIN keeps all rows from the right.

Lesson 6: FULL OUTER JOIN – All from Both (Sometimes not available)

Definition: FULL OUTER JOIN returns all rows from both tables, matching when possible.

Why it's important: It shows everything.

Simple explanation: It's like combining two sets completely.

Real-life example: You want to see all students and all subjects, even if some don't match.

School example: All teachers and all classrooms.

Home example: All family members and all pets.

Nigerian example: All markets and all products.

Illustration:

  A: 1,2   B: 2,3
  FULL JOIN → 1,2,3
  

Mini summary: FULL OUTER JOIN keeps every row from both.

Lesson 7: Using JOIN with WHERE – Filtering

Definition: You can add WHERE after a JOIN to filter the results.

Why it's important: You might only want some rows, like only students above 10 years old.

Simple explanation: JOIN first, then filter with WHERE.

Real-life example: JOIN students and grades, then WHERE grade > 80.

School example: JOIN teachers and classes, WHERE subject = 'Math'.

Home example: JOIN family and chores, WHERE chore = 'Wash dishes'.

Nigerian example: JOIN customers and orders, WHERE city = 'Lagos'.

Illustration:

  SELECT * FROM students
  JOIN grades ON students.id = grades.student_id
  WHERE grades.score > 90;
  

Mini summary: WHERE filters a JOINed result.

Lesson 8: JOIN with ORDER BY – Sorting Combined Data

Definition: You can sort the result of a JOIN using ORDER BY.

Why it's important: To see the combined data in a nice order.

Simple explanation: After joining, sort like you normally do.

Real-life example: Join students and grades, then order by score descending.

School example: Join teachers and classes, order by class name.

Home example: Join family and birthdays, order by age.

Nigerian example: Join markets and goods, order by price.

Illustration:

  SELECT * FROM students
  JOIN grades ON students.id = grades.student_id
  ORDER BY grades.score DESC;
  

Mini summary: ORDER BY sorts the joined table.

Lesson 9: JOIN with GROUP BY – Summaries from Combined Data

Definition: You can group the result of a JOIN.

Why it's important: To get summaries like average score per class.

Simple explanation: Join tables, then group by a column.

Real-life example: Join students and classes, then average score per class.

School example: Join teachers and classes, count classes per teacher.

Home example: Join family and expenses, sum per person.

Nigerian example: Join customers and orders, total spent per customer.

Illustration:

  SELECT class, AVG(score) FROM students
  JOIN grades ON students.id = grades.student_id
  GROUP BY class;
  

Mini summary: GROUP BY on a JOIN gives summaries.

Lesson 10: Joining More Than Two Tables

Definition: You can join three or more tables in one query.

Why it's important: Real databases often have many tables.

Simple explanation: Chain joins together: A JOIN B ON ... JOIN C ON ...

Real-life example: Students, grades, and subjects – to get student, subject, and score.

School example: Teachers, classes, rooms – to see which teacher is in which room.

Home example: Family, chores, schedule – to see who does what and when.

Nigerian example: Customers, orders, products – to see what each customer bought.

Illustration:

  SELECT * FROM students
  JOIN grades ON students.id = grades.student_id
  JOIN subjects ON grades.subject_id = subjects.id;
  

Mini summary: You can join many tables.

Lesson 11: Nigerian Example – School Management

Let's say a school in Ibadan has tables: Students (id, name), Classes (id, class_name), and Scores (student_id, class_id, score). We can JOIN to find each student's scores per class.

  SELECT s.name, c.class_name, sc.score
  FROM Students s
  JOIN Scores sc ON s.id = sc.student_id
  JOIN Classes c ON sc.class_id = c.id;
  

Lesson 12: Fun Example – Superhero Teams

You have a table of Superheroes (id, name) and Powers (hero_id, power). JOIN to see each hero's powers.

Key Vocabulary

  • JOIN: Combining tables.
  • INNER JOIN: Only matching rows.
  • LEFT JOIN: All rows from left table.
  • RIGHT JOIN: All rows from right table.
  • FULL OUTER JOIN: All rows from both.
  • Key: A column used to match tables.
  • Primary Key: Unique ID for a row.
  • Foreign Key: A key that links to another table.
  • NULL: No data (empty).

Important Concepts

1. Primary Key: A column that uniquely identifies each row (like student_id).

2. Foreign Key: A column in one table that refers to the primary key of another table.

3. Referential Integrity: Ensures that foreign keys point to existing rows.

Step-by-Step Explanations

Step 1: Identify the tables you want to join.

Step 2: Find the common column (key).

Step 3: Write the JOIN clause: JOIN table2 ON table1.key = table2.key.

Step 4: Add WHERE, GROUP BY, ORDER BY as needed.

Step 5: Run the query and check the results.

Real-life Examples

  • An online store joins customers and orders to see purchase history.
  • A hospital joins patients and appointments.
  • A library joins books and borrowers.

Nigerian Examples

  • A bank joins customers and accounts.
  • A transport company joins buses and routes.
  • A university joins students and courses.

Fun Examples

  • Join your friends and their favourite games.
  • Join your toys and their colours.
  • Join your class and their pet names.

Everyday Examples

  • Combining a phone contact list with a birthday list.
  • Matching a shopping list with a price list.
  • Linking a recipe with ingredients.

Teacher Notes

Use simple, relatable datasets like class lists. Emphasize the concept of matching keys. Show visual Venn diagrams to explain INNER, LEFT, RIGHT joins.

Parent Tips

Help your child find examples of tables at home (e.g., family members and birthdays). Ask: "How would you connect these?"

Interesting Facts

  • MySQL was first released in 1995.
  • JOINs are used by almost every application that uses databases.
  • The concept of joins comes from set theory in mathematics.

Did You Know?

MySQL is used by many Nigerian companies like Flutterwave and Paystack to manage their data.

Remember This

  • INNER JOIN = only matches.
  • LEFT JOIN = all left + matches.
  • RIGHT JOIN = all right + matches.
  • Always specify the join condition.

Common Mistakes

  • Forgetting the ON clause.
  • Joining on wrong columns.
  • Confusing LEFT and RIGHT.
  • Not using table aliases for readability.

Best Practices

  • Use table aliases (e.g., s for students).
  • Always specify the join condition with ON.
  • Test with small data first.
  • Use appropriate join type.

Illustrations

Join Flowchart

  Start
    |
    V
  Choose Tables
    |
    V
  Identify Key
    |
    V
  Write JOIN Query
    |
    V
  Run and Check
    |
    V
  Finish
  

Table Example: Students and Grades

Student IDNameGrade
1ChidiA
2AminaB

Comparison Table: JOIN Types

JOIN TypeReturns
INNER JOINOnly matching rows
LEFT JOINAll rows from left, matches from right
RIGHT JOINAll rows from right, matches from left
FULL JOINAll rows from both

End-of-Module Summary

In this module, you learned how to combine tables using JOINs. You know about INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. You can now answer questions that need data from multiple tables. You're becoming a true data analysis expert!

Frequently Asked Questions

  1. What is a JOIN? – Combining tables.
  2. What is INNER JOIN? – Only matching rows.
  3. What is LEFT JOIN? – All rows from left.
  4. What is RIGHT JOIN? – All rows from right.
  5. What is a key? – A matching column.
  6. What is a primary key? – Unique ID.
  7. What is a foreign key? – Link to another table.
  8. Can we join more than two tables? – Yes.
  9. Can we filter after JOIN? – Yes, with WHERE.
  10. Can we sort after JOIN? – Yes, with ORDER BY.

Review Questions

  1. What is a JOIN?
  2. What does INNER JOIN return?
  3. What does LEFT JOIN return?
  4. What does RIGHT JOIN return?
  5. What is a key?
  6. What is a primary key?
  7. What is a foreign key?
  8. Can you filter a JOIN with WHERE?
  9. Can you sort a JOIN with ORDER BY?
  10. Can you group a JOIN with GROUP BY?
  11. How do you join three tables?
  12. What is NULL?
  13. What is the difference between INNER and LEFT JOIN?
  14. Give an example of a real-life JOIN.
  15. Why are JOINs useful?

Fill-in-the-Blank

  1. _____ JOIN returns only matching rows.
  2. _____ JOIN keeps all rows from the left table.
  3. The common column used to join is called a _____ .
  4. _____ key uniquely identifies a row.
  5. You use _____ after JOIN to filter.

True or False

  1. INNER JOIN returns all rows from both tables. (False)
  2. LEFT JOIN keeps all rows from the left table. (True)
  3. RIGHT JOIN keeps all rows from the right table. (True)
  4. A primary key can be NULL. (False)
  5. You can join more than two tables. (True)

Multiple Choice

  1. Which JOIN returns only matching rows? A) LEFT B) INNER C) RIGHT (Answer: B)
  2. Which JOIN keeps all rows from the left? A) LEFT B) INNER C) RIGHT (Answer: A)
  3. Which JOIN keeps all rows from the right? A) LEFT B) INNER C) RIGHT (Answer: C)
  4. What is a primary key? A) A unique ID B) A foreign key C) A join (Answer: A)
  5. What is a foreign key? A) A unique ID B) A link to another table C) A join (Answer: B)
  6. Can you use WHERE with a JOIN? A) Yes B) No (Answer: A)
  7. Can you use ORDER BY with a JOIN? A) Yes B) No (Answer: A)
  8. Can you use GROUP BY with a JOIN? A) Yes B) No (Answer: A)
  9. How many tables can you join? A) 2 B) Many C) 1 (Answer: B)
  10. What does NULL mean? A) Zero B) No data C) A key (Answer: B)
  11. Which join is most common? A) LEFT B) INNER C) RIGHT (Answer: B)
  12. What is the ON clause used for? A) Filtering B) Specifying the join condition C) Sorting (Answer: B)
  13. Which join is also called a full outer join? A) LEFT B) RIGHT C) FULL (Answer: C)
  14. Can you join tables without a common column? A) Yes B) No (Answer: B)
  15. Which keyword is used for joining? A) LINK B) JOIN C) CONNECT (Answer: B)

Matching

TermDefinition
INNER JOINOnly matching rows
LEFT JOINAll left, matches from right
RIGHT JOINAll right, matches from left
Primary KeyUnique ID
Foreign KeyLink to another table

Short Answer

  1. Write an INNER JOIN query between students and grades.
  2. What is the difference between LEFT and INNER JOIN?
  3. Why do we need keys for joining?

Scenario-based

Scenario: You have tables: Customers (id, name) and Orders (order_id, customer_id, amount). Write a query to list customer names and their total order amount.

Answer: SELECT c.name, SUM(o.amount) FROM Customers c JOIN Orders o ON c.id = o.customer_id GROUP BY c.name;

Group Activity

In groups, create two tables: one for 'Students' and one for 'Sports'. Each student has a sport_id. Write a JOIN to list each student and their sport.

Individual Activity

Create tables: 'Books' (id, title) and 'Authors' (id, name, book_id). Write a query using INNER JOIN to show book titles and author names.

Classroom Discussion

  • Why is it better to split data into multiple tables?
  • When would you use LEFT JOIN instead of INNER JOIN?
  • Can you think of a situation where you need three tables?

Mini Project

Build a small 'School Library' database with tables: Books, Students, and Loans. Write JOIN queries to show which student borrowed which book.

Practical Assignment

Download a sample database with at least 3 tables. Write 5 different JOIN queries (INNER, LEFT, RIGHT, multiple tables, with WHERE/GROUP BY).

Challenge Exercise

Write a query that joins Customers, Orders, and Products to find the total amount spent by each customer on a specific product category.

Quiz Answers (Multiple Choice)

1B, 2A, 3C, 4A, 5B, 6A, 7A, 8A, 9B, 10B, 11B, 12B, 13C, 14B, 15B

Key Takeaways

  • JOINs combine tables using keys.
  • INNER JOIN shows only matches.
  • LEFT JOIN keeps all from left.
  • RIGHT JOIN keeps all from right.
  • You can filter, sort, and group JOIN results.
  • Practice is key to mastering JOINs.

Preparation for Module 11

In the next module, we will learn about advanced topics like subqueries and views. Review your JOIN skills – they will be very useful!

Excellent work! You've completed Module 10. You're now ready for more advanced data analysis.

🏆 Get Certified

🔒

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

🎓 Sign Up to Get Certified
🛠️ Practice Tools
Hands-on simulators & labs - subscription required.
→
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
→