SQL Cookbook Lesson
How to add a column in SQL
Learn how to add a column in SQL with examples and explanations from SQLPad.
Problem
You have an existing table in your SQL database and you need to add a new column to it.
Sample Data
Consider you have a table named Employees with the following structure:
| EmployeeID | FirstName | LastName |
|---|---|---|
| 1 | John | Doe |
| 2 | Jane | Doe |
| 3 | Alice | Johnson |
| 4 | Bob | Smith |
MySQL Solution
ALTER TABLE Employees
ADD COLUMN Email VARCHAR(255);
PostgreSQL Solution
ALTER TABLE Employees
ADD COLUMN Email VARCHAR(255);
Explanation
In both MySQL and PostgreSQL, the ALTER TABLE statement is used to add, delete/drop or modify columns in the existing database.
To add a new column to the table, the ADD COLUMN command is used followed by the column name and the data type of the column.
In the given solution, we are adding a new column named 'Email' with a data type of VARCHAR(255) to the Employees table. The VARCHAR data type is used to store characters of length up to 255. After the execution of the above SQL command, the Email column will be added at the end of Employees table.