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 8 of 20

    Keys and relationships

    Primary and foreign keys, one-to-many and many-to-many.

    8 minutes2 games

    Why not put everything in one table? If we wrote the course name next to every enrolled student, we'd repeat JavaScript a hundred times; renaming the course would mean a hundred edits, and missing one would leave the data contradicting itself.

    The fix: each kind of thing gets its own table, and tables point at each other through keys.

    • enrollments (student_id, course_id, progress)
      • student_id → students.id
        • students (id, name, city, xp)
      • course_id → courses.id
        • courses (id, title, price)

    enrollments is a junction table: each row says "this student takes this course".

    • Primary key (students.id): identifies each row; never repeated, never NULL.
    • Foreign key (enrollments.student_id): a column pointing at a primary key in another table.

    Relationship kinds: one-to-many (a student has one mentor, a mentor has many students: mentor_id), and many-to-many (a student takes many courses, a course has many students), which needs a junction table like enrollments.

    SQL
    -- Which courses is Laila (id 1) taking?
    SELECT * FROM enrollments WHERE student_id = 1;
    
    -- The ids alone aren't readable; look them up:
    SELECT id, title FROM courses WHERE id IN (1, 2, 5);

    We did it by hand in two steps: get the ids, then look them up. The JOIN in the next lesson does both in one query.

    Important

    A foreign key isn't just documentation: the database refuses an enrollment for a student who doesn't exist, or deleting a student who still has enrollments (unless you say ON DELETE CASCADE). PostgreSQL always enforces this; SQLite only does after PRAGMA foreign_keys = ON.

    Quick question

    What kind of relationship links students and courses?

    Interview question

    Why not store a student's courses in one column like '1,2,5'?

    Exercise

    From enrollments: get the course_id of courses student 4 has finished (progress = 100).

    Expected result:
    course_id
    3
    SQL
    SELECT course_id
    FROM enrollments;

    Learn more from

    • PostgreSQL docs: Foreign keyspostgresql.org
    • SQLite: Foreign key supportsqlite.org
    • Wikipedia: Database normalizationen.wikipedia.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousCASE, COALESCE and text functionsNextINNER JOIN
    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.