How to Add Column in SQL

How to Add Column in SQL

6 mins readComment
Updated on Aug 16, 2024 11:19 IST

SQL is an important tool for modern data management, and proficiency in it is essential for everyone. This article covers modifying table structures, focusing on adding columns to a MySQL table. Whether you're a student or a professional, this guide will help you improve your SQL skills.

how to add column in sql

Adjusting table structures without losing data is important for meeting business needs. SQL provides the ALTER TABLE statement to add, delete or modify columns and constraints in a table after creation. This is very useful while introducing new fields to collect extra information from applications and systems built on the database.

Prerequisites for Adding Columns

Before adding a new column, consider these prerequisites:

  • Confirm you have privileges to alter the target table in the database.
  • Add appropriate default values or constraints if needed for data integrity
  • Understand implications for existing queries, indexes, triggers and application code

Now, we will discuss methods to add columns into a table and then the process to INSERT data in that column.

This article will discuss three different methods of adding columns in SQL.

Recommended online courses

Best-suited Database and SQL courses for you

Learn Database and SQL with these high-rated online courses

Free
4 weeks
4.24 K
6 weeks
– / –
2 months
Free
2 hours
– / –
15 hours
Free
1 hours
Free
9 hours
– / –
3 hours
Free
7 hours

Methods for Adding Columns in SQL

Adding a Column using ALTER TABLE Statement in SQL

To add a single column to an existing table in SQL, you can use the below syntax:

ALTER TABLE table_name
ADD column_name data_type;
Copy code

Example: Let you have to add a column 'email' to an employee table, then:

ALTER TABLE employee
ADD email VARCHAR(255);
Copy code

Adding Multiple Columns to a Table using ALTER TABLE Statement in SQL

Now, if you have to add multiple columns to a table, then you just have to ADD clause that many times.

Example: You have to add two columns, email and phone number, to the employee table, then:

ALTER TABLE employee
ADD email VARCHAR(255)
ADD Phone_Number INT(10);
Copy code

In the above two sections, you have seen how to add columns to an existing table. Now we will see how to INSERT data in these columns.

Adding Data to a Table Using INSERT Statement in SQL

To add data to the new columns, we will use INSERT INTO and VALUES clauses in SQL.
Let's take an example to get a better understanding of how to add data to a new table.
In the above example, we have added two columns: email and address; now, we will fill one entry in these two columns.

INSERT INTO employee(name, age, email, phone_number)
VALUES ('Vikram', '27', 'singh.vikram@shiksha.com', '9876543210');
Copy code

Note: If you are adding data (or records) to the existing table, you can use the UPDATE clause in SQL.

You may have a basic understanding of how to add new columns to a table. However, if you are still unsure, there is no need to worry. We have prepared a case study to provide a real-life example and help you better understand the concept.

Case Study: 

Let there be an 'employee' table having three columns: employee_id, employee_name, and employee_age.

id name age
1 Aarav 25
2 Vivaan 30
3 Aditya 22
4 Arjun 28
5 Aryan 27
6 Vihaan 32
7 Sai 24
8 Kabir 26
9 Ishaan 29
10 Rohan 31

Problem Statement: We need to add additional information about these users, specifically their email addresses and phone numbers. To accomplish this, we will:

  • Add a column for email addresses.
  • Add multiple columns, one for phone numbers and another for city of residence.
  • Insert data into these new columns.

Ans-1: Add a column to e-mail addresses

SQL Command

ALTER TABLE employee ADD email VARCHAR(255);
Copy code

Output

id

name

age

email

1

Aarav

25

None

2

Vivaan

30

None

3

Aditya

22

None

4

Arjun

28

None

5

Aryan

27

None

6

Vihaan

32

None

7

Sai

24

None

8

Kabir

26

None

9

Ishaan

29

None

10

Rohan

31

None

Ans 2: Add multiple columns, one for phone numbers and another for the city of residence.

SQL Command

ALTER TABLE employee ADD phone_number VARCHAR(20), ADD city TEXT;
Copy code

Output

id

name

age

email

phone_number

city

1

Aarav

25

None

None

None

2

Vivaan

30

None

None

None

3

Aditya

22

None

None

None

4

Arjun

28

None

None

None

5

Aryan

27

None

None

None

6

Vihaan

32

None

None

None

7

Sai

24

None

None

None

8

Kabir

26

None

None

None

9

Ishaan

29

None

None

None

10

Rohan

31

None

None

None

Now, for the last and final problem, we will use the UPDATE query as we have to update the record into the existing table. So, in the above table, we will update the first 2 records (i.e., for employee id 1 and 2).

Ans 3: Insert data into newly added columns.

SQL Command

UPDATE employee SET email = 'aarav@gmal.com, phone_number = '9876543210', city = 'Mumbai'
UPDATE employee SET email = 'vivaan@gmal.com, phone_number = '9876543211', city = 'Delhi';
Copy code

Output

id

name

age

email

phone_number

city

1

Aarav

25

aarav@gmail.com

9876543210

Mumbai

2

Vivaan

30

vivaan@gmail.com

9876543211

Delhi

3

Aditya

22

None

None

None

4

Arjun

28

None

None

None

5

Aryan

27

None

None

None

6

Vihaan

32

None

None

None

7

Sai

24

None

None

None

8

Kabir

26

None

None

None

9

Ishaan

29

None

None

None

10

Rohan

31

None

None

None

From the above case study, I hope you understand enough how to add columns to a table and data to the existing column.

Conclusion

The article discussed different methods to add single or multiple columns in a table with the help of a case study. The case study contains three problems,

  • Adding one column to the table.
  • Adding multiple columns to the table.
  • INSERT/UPDATE column to the table.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

Please Checkout More SQL Blogs

How to Find Nth Highest Salary in SQL
How to Find Nth Highest Salary in SQL
Finding out the N’th highest salary from a table is one of the most frequently asked SQL interview questions. In this article, we will discuss four different approaches to find...read more

Order of Execution in SQL
Order of Execution in SQL
An SQL query comprises of various clauses like SELECT, FROM, WHERE, GROUPBY, HAVING, and ORDERBY clauses. Each clause has a specific role in the query. The correct order of execution...read more

How to Find Second Highest Salary in SQL
How to Find Second Highest Salary in SQL
It's crucial to master SQL queries for managing and analyzing databases. This article focuses on finding the second-highest salary in SQL, which is a common yet important task in database...read more

Using Partition By SQL Clause
Using Partition By SQL Clause
The Partition By SQL clause is a subclause of OVER clause that is used in every invocation of window functions such as MAX(), RANK() and AVG(). In SQL, the “PARTITION...read more

Top 30 SQL Query Interview Questions
Top 30 SQL Query Interview Questions
Structured Query Language or most commonly known as SQL is used on a daily basis to handle, manipulate and analyze relational databases.

SQL ALTER TABLE : ADD, DROP, MODIFY, RENAME
SQL ALTER TABLE : ADD, DROP, MODIFY, RENAME
ALTER TABLE in SQL is used to change the structure of the existing table. In this article, we will briefly discuss how to add, modify, drop, rename columns, constraints, and...read more

How to Delete a Column in SQL?
How to Delete a Column in SQL?
Sometimes in the dataset, you have some columns that are not of your use but when you run a query it will also get executed that leads to you increase...read more

How to Use TRUNCATE Command in SQL?
How to Use TRUNCATE Command in SQL?
SQL TRUNCATE TABLE command is used to remove the record from the table. Confused, the SQL DELETE statement also removes the record from the table. So, why use another command...read more

How to use DROP command in SQL?
How to use DROP command in SQL?
In SQL, the DROP command is used to delete a database, table, index, or view. To use the DROP command, you must be very careful because it permanently deletes the...read more

How to Create, Update, Insert and Delete SQL Views?
How to Create, Update, Insert and Delete SQL Views?
This article will give you a detailed insight on SQL views. You will learn the method to create, update, insert, drop and delete SQL views. This article will give you...read more

All About Natural Joins in SQL
All About Natural Joins in SQL
Natural join is an inner join that automatically joins two or more tables with the same name and data type on all columns. But both inner join and natural join...read more

100+ SQL Interview Questions and Answers for 2023
100+ SQL Interview Questions and Answers for 2023
Are you wondering what SQL interview questions you will be asked? If you are preparing for an SQL interview, this article will provide you with 100+ most commonly asked SQL...read more

Difference Between SQL and PLSQL
Difference Between SQL and PLSQL
In this article, we will learn what SQL is, What PLSQL is, their types, features, applications, and their differences.

75 Most Popular MySQL Commands
75 Most Popular MySQL Commands
This article covers the most popular MySQL commands with examples to help you work more efficiently with MySQL databases. This article covers the most popular MySQL commands with examples to...read more

FAQs on How to ADD Columns in SQL

How do I add a single column to a table in SQL?

Use the ALTER TABLE statement. For example: ALTER TABLE table_name ADD column_definition;. This is a common approach across various SQL databases like Oracle, SQL Server, SQLite, and DB2​.

Can I add multiple columns in one statement?

Yes, in most SQL databases, you can add multiple columns in a single ALTER TABLE statement by listing the columns separated by commas. However, in SQLite, you must execute multiple ALTER TABLE ADD COLUMN statements for each column.

How to set default values when adding a column?

Specify the default value in the column definition. For example: ALTER TABLE customer ADD suburb VARCHAR2(100) DEFAULT 'Central';​​

Is it possible to add a column with a unique constraint?

Yes, you can add a column and enforce uniqueness. For example: ALTER TABLE users ADD username VARCHAR(50) UNIQUE;. Ensure no duplicated values exist before applying this constraint​

How do I check if a column exists before adding it?

Use an IF NOT EXISTS condition. For example: IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SampleTable' AND COLUMN_NAME = 'Email') BEGIN ALTER TABLE SampleTable ADD Email varchar(255) END;

Can I add a column at a specific position in the table?

While it's technically possible, it's not recommended due to the potential impact on existing queries and applications. The default behaviour is to add the new column at the end of the table​.

What are some common errors to watch out for when adding columns?

Common issues include syntax errors, data type mismatches, insufficient permissions, and conflicts with existing column names​.

How can I rename an existing column?

Use the sp_rename command. For instance, to rename a column: sp_rename 'table_name.old_col_name', 'new_col_name', 'COLUMN';

How do I remove a column from a table?

Use the ALTER TABLE statement with the DROP COLUMN option. For example: ALTER TABLE Employee DROP COLUMN E_Address;

About the Author