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!
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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;
You have a table of Superheroes (id, name) and Powers (hero_id, power). JOIN to see each hero's powers.
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 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.
Use simple, relatable datasets like class lists. Emphasize the concept of matching keys. Show visual Venn diagrams to explain INNER, LEFT, RIGHT joins.
Help your child find examples of tables at home (e.g., family members and birthdays). Ask: "How would you connect these?"
MySQL is used by many Nigerian companies like Flutterwave and Paystack to manage their data.
s for students).
Start
|
V
Choose Tables
|
V
Identify Key
|
V
Write JOIN Query
|
V
Run and Check
|
V
Finish
| Student ID | Name | Grade |
|---|---|---|
| 1 | Chidi | A |
| 2 | Amina | B |
| JOIN Type | Returns |
|---|---|
| INNER JOIN | Only matching rows |
| LEFT JOIN | All rows from left, matches from right |
| RIGHT JOIN | All rows from right, matches from left |
| FULL JOIN | All rows from both |
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!
| Term | Definition |
|---|---|
| INNER JOIN | Only matching rows |
| LEFT JOIN | All left, matches from right |
| RIGHT JOIN | All right, matches from left |
| Primary Key | Unique ID |
| Foreign Key | Link to another table |
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;
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.
Create tables: 'Books' (id, title) and 'Authors' (id, name, book_id). Write a query using INNER JOIN to show book titles and author names.
Build a small 'School Library' database with tables: Books, Students, and Loans. Write JOIN queries to show which student borrowed which book.
Download a sample database with at least 3 tables. Write 5 different JOIN queries (INNER, LEFT, RIGHT, multiple tables, with WHERE/GROUP BY).
Write a query that joins Customers, Orders, and Products to find the total amount spent by each customer on a specific product category.
1B, 2A, 3C, 4A, 5B, 6A, 7A, 8A, 9B, 10B, 11B, 12B, 13C, 14B, 15B
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.