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

    Transactions: all or nothing

    BEGIN, COMMIT, ROLLBACK, and what ACID means.

    9 minutes2 games

    Imagine a student sending 100 XP to a friend: two steps, subtract from one and add to the other. If the server crashes between them, XP vanishes. A transaction groups the steps into one unit: all of them happen, or none do.

    SQL
    BEGIN;
    UPDATE students SET xp = xp - 100 WHERE name = 'Laila';
    UPDATE students SET xp = xp + 100 WHERE name = 'Omar';
    COMMIT;
    
    SELECT name, xp FROM students WHERE name IN ('Laila', 'Omar');

    BEGIN starts, COMMIT makes everything permanent. If something goes wrong midway, ROLLBACK undoes everything back to before BEGIN:

    SQL
    BEGIN;
    DELETE FROM students;              -- oops
    SELECT COUNT(*) AS during FROM students;
    ROLLBACK;
    
    SELECT COUNT(*) AS after FROM students;
    ACIDMeaning

    Atomicity

    all or nothing

    Consistency

    constraints hold before and after

    Isolation

    concurrent transactions don't see each other's half-done work

    Durability

    after COMMIT data survives even a power cut

    Important

    Without BEGIN, each statement is its own tiny transaction that commits by itself (autocommit). In apps, keep transactions short: don't wait on an API call or the user inside one, because it may hold locks that block everyone else.

    What's the output?

    BEGIN;
    UPDATE students SET xp = 0 WHERE id = 1;
    ROLLBACK;
    SELECT xp FROM students WHERE id = 1;

    What will this code print?

    Interview question

    Explain ACID. What are isolation levels?

    Exercise

    Inside a transaction: move 100 XP from Nour (id = 4) to Hana (id = 7) and COMMIT. Then show both students' name and xp, ordered by id.

    Expected result:
    name | xp
    Nour | 310
    Hana | 160
    SQL
    BEGIN;
    
    COMMIT;
    

    Learn more from

    • PostgreSQL docs: Transactionspostgresql.org
    • SQLite: Transactionssqlite.org
    • Wikipedia: ACIDen.wikipedia.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousINSERT, UPDATE and DELETENextSQL injection: the most dangerous mistake
    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.