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

    Building tables and changing data · Lesson 14 of 20

    INSERT, UPDATE and DELETE

    Add, change and remove rows, without wiping the table by accident.

    10 minutes2 games

    These three commands change data:

    • INSERT INTO adds rows.
    • UPDATE … SET … WHERE changes existing rows.
    • DELETE FROM … WHERE removes rows.
    SQL
    INSERT INTO students (name, city, xp, joined)
    VALUES ('Salma', 'Aswan', 0, '2026-06-01'),
           ('Tarek', 'Cairo', 0, '2026-06-02');
    
    UPDATE students
    SET xp = xp + 25
    WHERE city = 'Cairo';
    
    DELETE FROM students
    WHERE name = 'Hana';
    
    SELECT id, name, city, xp FROM students ORDER BY id;

    UPDATE or DELETE without WHERE hits every row. A safe habit: write a SELECT with the same WHERE first, check those are the rows you mean, then swap SELECT for UPDATE.

    RETURNING and UPSERT

    RETURNING gives back the changed rows, so no second SELECT is needed (great for learning the new id). ON CONFLICT says "if the row exists, update it instead of failing", which is called an upsert. Both look the same in SQLite and PostgreSQL.

    SQL
    -- Laila is already in course 2; bump her progress instead of failing
    INSERT INTO enrollments (student_id, course_id, progress)
    VALUES (1, 2, 90)
    ON CONFLICT (student_id, course_id)
    DO UPDATE SET progress = excluded.progress
    RETURNING student_id, course_id, progress;

    Important

    DELETE removes rows but keeps the table; DROP TABLE removes the table and all its data. Real apps often use soft delete: a deleted_at column instead of actually deleting, so data can be restored and history kept.

    Spot the bug

    This was meant to reward only Omar, but every student got 100. What's wrong?

    Interview question

    What's the difference between DELETE, TRUNCATE and DROP?

    Exercise

    Youssef (id = 5) has NULL xp. Give him 50 XP so it becomes 50 (not NULL), then show his name and xp.

    Expected result:
    name | xp
    Youssef | 50
    SQL
    
    SELECT name, xp FROM students WHERE id = 5;

    Learn more from

    • PostgreSQL docs: Inserting datapostgresql.org
    • PostgreSQL docs: Updating datapostgresql.org
    • SQLite: UPSERTsqlite.org
    • SQLite: RETURNINGsqlite.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousCREATE TABLE and constraintsNextTransactions: all or nothing
    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.