ADD COLUMN

SQL · Working with Tables

Theory

ALTER TABLE ... ADD COLUMN appends a new column to an existing table. ALTER TABLE employees ADD COLUMN email VARCHAR(150) adds the column at the end of the column list, and every existing row gets NULL in it until you fill it with UPDATE. New rows can supply a value straight away.

If you give a DEFAULT, existing rows show that default instead of NULL: ADD COLUMN status VARCHAR(10) DEFAULT 'active'. Adding a NOT NULL column without a default fails on a table that already has rows, because those rows would break the rule; add it with a default, or add it nullable, backfill, then set NOT NULL. Most engines cannot place the column in the middle (MySQL is an exception with AFTER col). SELECT * output changes, which is one more reason to list columns explicitly.

Why we use it

Businesses keep collecting new facts, like a customer's email or an order's channel. ADD COLUMN extends the table to hold them without losing or reloading existing data.

When to use it

Use ADD COLUMN when a new attribute belongs to the entity. If the attribute can have many values per row (several phone numbers), create a separate related table instead of adding phone1, phone2 columns.

How it works

  1. ALTER TABLE employees ADD COLUMN email
  2. Is a DEFAULT given? Yes: Existing rows show the default. No: Existing rows get NULL.
  3. NOT NULL, no default, rows exist? Yes: Error: add a default or backfill. No: Column added.
  4. Fill old rows with UPDATE if needed
  5. New column is part of the table

Syntax

ALTER TABLE table_name
ADD COLUMN column datatype;

Example

ALTER TABLE employees ADD COLUMN email VARCHAR(150);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic