Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    SQLSQL

    First questions to the data

    • What are SQL and databases?
    • SELECT: choosing columns
    • WHERE: filtering rows
    • ORDER BY and LIMIT: sort and take the top

    Summarizing and grouping

    • Aggregates: COUNT, SUM, AVG
    • GROUP BY and HAVING
    • CASE, COALESCE and text functions

    Joining tables

    • Keys and relationships
    • INNER JOIN
    • LEFT JOIN: leave nobody out
    • Three-table joins and self joins
    • Subqueries and CTEs

    Building tables and changing data

    • CREATE TABLE and constraints
    • INSERT, UPDATE and DELETE
    • Transactions: all or nothing
    • SQL injection: the most dangerous mistake

    Professional level

    • Indexes: why is my query slow?
    • Window functions: rankings and totals without losing rows
    • Designing tables: normalization
    • Project: analytics for a learning platform
    SQL

    Joining tables · Lesson 11 of 20

    Three-table joins and self joins

    From the junction table to the details, and students with their mentors.

    9 minutes2 games

    To get "student name + course title" you go through the junction table: students → enrollments → courses. Each extra JOIN adds a table to the chain.

    SQL
    SELECT s.name, c.title, e.progress
    FROM enrollments e
    JOIN students s ON s.id = e.student_id
    JOIN courses c ON c.id = e.course_id
    WHERE e.progress = 100
    ORDER BY s.name;

    Self join: a table with itself

    mentor_id in students points at another student in the same table. To get the mentor's name, join the table to itself under two names: s for the student and m for the mentor.

    SQL
    SELECT s.name AS student, m.name AS mentor
    FROM students s
    LEFT JOIN students m ON m.id = s.mentor_id
    ORDER BY mentor, student;
    • mentors
      • Laila
        • Karim
        • Omar
        • Ali
      • Nour
        • Youssef
        • Mariam
      • Hana (no mentor, no students)

    Important

    We used LEFT JOIN so students without a mentor (Laila, Nour, Hana) stay, with NULL. A plain JOIN would drop them.

    Order the lines

    Order the query that lists each student's name with their course title.

    1. Click the lines in the right order

    Interview question

    What is a self join? Give a real example.

    Exercise

    For every course: title and the number of enrolled students as students, including courses with none. Sort by the count, highest first, then by title.

    Expected result:
    title | students
    HTML & CSS | 3
    JavaScript | 3
    Python | 3
    SQL | 2
    React | 1
    C++ | 0
    SQL
    SELECT c.title
    FROM courses c
    

    Learn more from

    • PostgreSQL docs: Joins between tablespostgresql.org
    • Wikipedia: Join (SQL), self-joinen.wikipedia.org
    • SQLite: SELECT (join clause)sqlite.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousLEFT JOIN: leave nobody outNextSubqueries and CTEs
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.