CodeBlanks Logo Icon CodeBlanks

Adding a column

Add a column with ADD COLUMN.

Adding a column

With ADD COLUMN you add a new column to a table. You can give a default value too.

ALTER TABLE customers
ADD COLUMN email VARCHAR(100) DEFAULT 'unknown';

This query adds an email column with default 'unknown'.

Adding a column — details

Existing rows get the default value for the new column. Without DEFAULT, NULL is used.

For example, a column added with ADD COLUMN active BOOLEAN DEFAULT TRUE starts as TRUE on every existing row.

Practice Adding a column

Add an email VARCHAR(100) column to customers.

Starter code

query.sql
-- add column
ALTER TABLE customers

Sample solution

query.sql
ALTER TABLE customers
ADD COLUMN email VARCHAR(100);

Expected output

Tablo güncellendi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Add ADD COLUMN

Question
ALTER TABLE customers
____ email VARCHAR(100);
Answer
ALTER TABLE customers
ADD COLUMN email VARCHAR(100);

Explanation: ADD COLUMN is the right answer.

2 What if DEFAULT is missing in ADD COLUMN?

  • Raises an error
  • NULL is returned Correct answer
  • An empty string
  • Zero

Explanation: Without DEFAULT, NULL is used.

3 Adding a column: match the terms

Term Meaning
ADD COLUMN add column
DEFAULT default
NULL empty
existing row old record

All lessons in Intermediate