Skip to content
Laviyx Learn

Try

SQL INSERT, UPDATE and DELETE Practice

Practise changing data safely with INSERT, UPDATE and DELETE, and changing tables with CREATE TABLE and ALTER TABLE.

Beginner7 problemsUpdated

Review first: INSERT, UPDATE, DELETE, CREATE TABLE, ALTER TABLE, Constraints

Each problem starts from this tasks table, so changes made in one problem do not carry over to the next:

task_idtitlestatuspriorityassignee
1Write reportopen2Asha
2Fix login bugin_progress1Ravi
3Update docsopen3NULL
4Plan sprintdone2Meera
5Review PRopen1Ravi

Each solution ends with a SELECT so you can see the effect of the change.

Try your answers on these tables in the SQL Playground
  1. 1.Add a task

    Beginner

    Add a task with id 6, title Prepare demo, status open, priority 2, assigned to Meera. Then show the new row.

    Show hint

    List the columns after the table name, then the values in the same order.

    Show solution
    SQLRun
    INSERT INTO tasks (task_id, title, status, priority, assignee)
    VALUES (6, 'Prepare demo', 'open', 2, 'Meera');
    
    SELECT * FROM tasks WHERE task_id = 6;
    Output
    task_id | title        | status | priority | assignee
    --------+--------------+--------+----------+---------
    6       | Prepare demo | open   | 2        | Meera

    Naming the columns is optional when you supply a value for every column, but it is safer. The statement keeps working if someone later adds or reorders columns.

  2. 2.Add several tasks at once

    Beginner

    In one INSERT statement, add these two tasks, then return the total number of tasks as total_tasks:

    • id 6, Book venue, open, priority 3, no assignee yet
    • id 7, Send invites, open, priority 2, assigned to Asha
    Show hint

    Separate each row's values with a comma after VALUES. Use NULL for the missing assignee.

    Show solution
    SQLRun
    INSERT INTO tasks (task_id, title, status, priority, assignee)
    VALUES
      (6, 'Book venue', 'open', 3, NULL),
      (7, 'Send invites', 'open', 2, 'Asha');
    
    SELECT COUNT(*) AS total_tasks FROM tasks;
    Output
    total_tasks
    -----------
    7
  3. 3.Close a task

    Beginner

    Task 2 is finished. Set its status to done, then list the task_id, title and status of every done task.

    Show hint

    UPDATE ... SET ... WHERE. Without the WHERE, every task would change.

    Show solution
    SQLRun
    UPDATE tasks
    SET status = 'done'
    WHERE task_id = 2;
    
    SELECT task_id, title, status
    FROM tasks
    WHERE status = 'done'
    ORDER BY task_id;
    Output
    task_id | title         | status
    --------+---------------+-------
    2       | Fix login bug | done
    4       | Plan sprint   | done
  4. 4.Assign the unassigned

    Beginner

    Give every task that has no assignee to Kiran and set its priority to 1. Then show all tasks with their assignee and priority.

    Show hint

    You can set several columns in one UPDATE, separated by commas. Find the rows with IS NULL.

    Show solution
    SQLRun
    UPDATE tasks
    SET assignee = 'Kiran', priority = 1
    WHERE assignee IS NULL;
    
    SELECT task_id, title, assignee, priority
    FROM tasks
    ORDER BY task_id;
    Output
    task_id | title         | assignee | priority
    --------+---------------+----------+---------
    1       | Write report  | Asha     | 2
    2       | Fix login bug | Ravi     | 1
    3       | Update docs   | Kiran    | 1
    4       | Plan sprint   | Meera    | 2
    5       | Review PR     | Ravi     | 1
  5. 5.Remove finished tasks

    Beginner

    Delete every task whose status is done, then list the remaining tasks' task_id, title and status.

    Show hint

    DELETE FROM with a WHERE clause. Run the same WHERE in a SELECT first to check which rows it matches.

    Show solution
    SQLRun
    DELETE FROM tasks
    WHERE status = 'done';
    
    SELECT task_id, title, status
    FROM tasks
    ORDER BY task_id;
    Output
    task_id | title         | status
    --------+---------------+------------
    1       | Write report  | open
    2       | Fix login bug | in_progress
    3       | Update docs   | open
    5       | Review PR     | open

    A good habit before any DELETE is to run SELECT * FROM tasks WHERE status = 'done' first. If it returns exactly the rows you expect, change SELECT * to DELETE.

  6. 6.Create a table with constraints

    Beginner

    Create a projects table with:

    • project_id: an integer primary key
    • name: text up to 100 characters, required and unique
    • budget: a whole number that cannot be negative
    • start_date: a date

    Insert the project 1, 'Website Redesign', 250000, '2026-11-01', then select the project_id and name of all projects.

    Show hint

    Use PRIMARY KEY, NOT NULL, UNIQUE and a CHECK (budget >= 0) constraint.

    Show solution
    SQLRun
    CREATE TABLE projects (
      project_id INTEGER PRIMARY KEY,
      name       VARCHAR(100) NOT NULL UNIQUE,
      budget     INTEGER CHECK (budget >= 0),
      start_date DATE
    );
    
    INSERT INTO projects (project_id, name, budget, start_date)
    VALUES (1, 'Website Redesign', 250000, '2026-11-01');
    
    SELECT project_id, name FROM projects;
    Output
    project_id | name
    -----------+-----------------
    1          | Website Redesign

    With these constraints, the database itself rejects bad data. Inserting a second project called Website Redesign, a project with no name, or a negative budget fails with an error instead of storing a wrong row.

  7. 7.Add a column

    Beginner

    Add a due_date column (type DATE) to tasks. Set it to 2026-11-15 for the priority-1 tasks, then show every task's task_id, title and due_date.

    Show hint

    ALTER TABLE ... ADD COLUMN, then an UPDATE. Existing rows get NULL in the new column.

    Show solution
    SQLRun
    ALTER TABLE tasks ADD COLUMN due_date DATE;
    
    UPDATE tasks
    SET due_date = '2026-11-15'
    WHERE priority = 1;
    
    SELECT task_id, title, due_date
    FROM tasks
    ORDER BY task_id;
    Output
    task_id | title         | due_date
    --------+---------------+-----------
    1       | Write report  | NULL
    2       | Fix login bug | 2026-11-15
    3       | Update docs   | NULL
    4       | Plan sprint   | NULL
    5       | Review PR     | 2026-11-15

    SQL Server and Oracle write ADD without the word COLUMN (Oracle also wraps the definition in brackets). PostgreSQL, MySQL and SQLite accept ADD COLUMN.