BBampaa

Lessons › SQL › Chapter 7: Creating and changing tables

SQL lesson

ALTER TABLE

ALTER TABLE changes a table's shape, not its rows. ADD COLUMN puts a new, empty column on every row. RENAME COLUMN gives a column a clearer name. To fill a new column, use UPDATE from Level 49.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Add or rename a column

ALTER TABLE targets ADD COLUMN region TEXT

Result:

storemonthtargetregion
Lagos2024-03-015000
Accra2024-03-013000

Step by step

  1. The table whose shape changes.
    ALTER TABLE targets ADD COLUMN region TEXT
  2. A new, empty column on every row.
    ALTER TABLE targets ADD COLUMN region TEXT
  3. Same data, clearer name.
    ALTER TABLE targets ADD COLUMN region TEXT

Practice

Try each one in your head first, then open the answer.

1. Add a column called region (TEXT) to targets.
ALTER TABLE targets ADD COLUMN region TEXT
2. In targets, rename the column target to monthly_target.
ALTER TABLE targets RENAME COLUMN target TO monthly_target
3. Add a column called reason (TEXT) to returns.
ALTER TABLE returns ADD COLUMN reason TEXT
4. Add a column called discount (NUMERIC) to orders.
ALTER TABLE orders ADD COLUMN discount NUMERIC
5. In staff, rename the column name to full_name.
ALTER TABLE staff RENAME COLUMN name TO full_name

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed