Rename Column Name in SQL

The use of column names is to identify specific columns in a table. It is essential that the names of the columns must be relevant to the data present in the column for better clarity and understanding.

Sometimes, we see a requirement to change the name of the column in the database to maintain the relevance of the data contained in it. SQL allows changing column names using the following command:

  • ALTER TABLE command that can change data types, column names, and other properties of a table.
  • The RENAME COLUMN command is used along with the ALTER TABLE command to rename a column in the database.

Benefits of renaming a column in SQL:

  • Clear names that relate to the data in the column help improve clarity and becomes easy to differentiate between different columns and avoid confusion.
  • More descriptive column names help improve readability and make the data in the table easier to understand.
  • It helps to organize the data in the database efficiently.
  • It simplifies column names for easier understanding.
  • It helps avoid errors that can arise from using a similar name for columns in a table.

The syntax for renaming a column name in SQL will depend on the database you are using, but the basic syntax for renaming a column name in SQL is as follows:

We will look at the examples to understand how to change the column names in databases such as MySQL, PostgreSQL, Microsoft SQL Server, MariaDB, Oracle, etc.

Examples of renaming a column name in MySQL, PostgreSQL, MariaDB, and Oracle:

Example 1:

Let us consider a table named student_table, which contains the fields of stu_number, stu_name, stu_age, stu_address, stu_stream, and stu_marks.

stu_numberstu_namestu_agestu_addressstu_streamstu_marks
1Inaya20FaridabadScience95
2Jai22NoidaCommerce80
3Ishaan20NoidaArts74
4Milan18RoorkeeScience72
5Pari21GurugramScience81
6Khushi20GoaCommerce82
7Himanshu23MeerutCommerce75
8Rohit22MeerutCommerce86
9Sadaf20GoaArts74
10Asha19GurugramArts70

We will rename the column name from stu_number to stu_id. The query given below will change the column name:

Query:

Output:

It is evident from the output that the column name has been changed from stu_number to stu_id.

stu_idstu_namestu_agestu_addressstu_streamstu_marks
1Inaya20FaridabadScience95
2Jai22NoidaCommerce80
3Ishaan20NoidaArts74
4Milan18RoorkeeScience72
5Pari21GurugramScience81
6Khushi20GoaCommerce82
7Himanshu23MeerutCommerce75
8Rohit22MeerutCommerce86
9Sadaf20GoaArts74
10Asha19GurugramArts70

Example 2:

Let us consider another table named product_table, which contains the fields of id, name, quantity, and money.

idnamequantitymoney
1Chocolates20200
2Candies22110
3Lollipops20100
4Chips1890
5Apples21600
6Biscuits20200
7Chochos23230
8Waffle Chips22220
9Jelly Candies20400
10Popcorn19380

We will rename the column name from money to price. The query given below will change the column name:

Query:

Output:

It is clear from the output that the column name has been changed from money to price.

idnamequantityprice
1Chocolates20200
2Candies22110
3Lollipops20100
4Chips1890
5Apples21600
6Biscuits20200
7Chochos23230
8Waffle Chips22220
9Jelly Candies20400
10Popcorn19380

Example 3:

Let us consider a table named student_table, which contains the fields of stu_number, stu_name, stu_age, stu_address, stu_stream, and marks.

stu_numberstu_namestu_agestu_addressstu_streammarks
11Robin19SeattleCommerce85
12Jack20WashingtonArts95
13Michael21AustinScience84
14Justin19ColumbusCommerce75
15Ariana20AustinCommerce80
16Selena21WashingtonArts90
17Katrina22ColumbusArts89
18Tom23ColumbusCommerce85
19Henry21AustinScience79
20Alexander20SeattleScience90

We will rename the column name from marks to stu_marks. The query given below will change the column name:

Query:

Output:

It is evident from the output that the column name has been changed from marks to stu_marks.

stu_numberstu_namestu_agestu_addressstu_streamstu_marks
11Robin19SeattleCommerce85
12Jack20WashingtonArts95
13Michael21AustinScience84
14Justin19ColumbusCommerce75
15Ariana20AustinCommerce80
16Selena21WashingtonArts90
17Katrina22ColumbusArts89
18Tom23ColumbusCommerce85
19Henry21AustinScience79
20Alexander20SeattleScience90

Examples of renaming a column name in Microsoft SQL Server:

Example 1:

Let us consider one more table named employee_table, which contains the fields of emp_id, emp_id, emp_address, department, and emp_salary.

emp_idemp_nameemp_addressdepartmentemp_salary
50JohnWashingtonSales25000
51ShirleyAustinSoftware Testing50000
52DanielColumbusSales40000
53AnthonyAustinContent Writing20000
54JamesChicagoHR45000
55HelenPhoenixSoftware Testing55000
56NicoleChicagoSales35000
57NickColumbusHR40000
58PeterPhoenixContent Writing25000
59AshleyWashingtonContent Writing35000

We will rename the column name from department to emp_department. The query given below will change the column name:

Query:

Output:

It is clear from the output that the column name has been changed from department to emp_department.

emp_idemp_nameemp_addressemp_departmentemp_salary
50JohnWashingtonSales25000
51ShirleyAustinSoftware Testing50000
52DanielColumbusSales40000
53AnthonyAustinContent Writing20000
54JamesChicagoHR45000
55HelenPhoenixSoftware Testing55000
56NicoleChicagoSales35000
57NickColumbusHR40000
58PeterPhoenixContent Writing25000
59AshleyWashingtonContent Writing35000

Example 2:

Let us consider one more table named book_table, which contains the fields of id, book_name, author, pages, and price.

idbook_nameauthorpagesprice
1A Place Called HomePreeti Shenoy324250
2Pride, Prejudice, and PunditryShashi Tharoor600630
3Lal SalamSmriti Irani25665
4The Fault in Our StarsJohn Green352250
5The Book ThiefMarkus Zusak624295
6Animal FarmGeorge Orwell88120
7The Ten Trillion DreamSubhash Chandra Garg712650
8UnfinishedPriyanka Chopra Jonas280420
9The Princess BrideWilliam Goldman512780
10Life of PiYann Martel352240

We will rename the column name from book_name to book_list. The query given below will change the column name:

Query:

Output:

It is clear from the output that the column name has been changed from book_name to book_list.

idbook_listauthorpagesprice
1A Place Called HomePreeti Shenoy324250
2Pride, Prejudice, and PunditryShashi Tharoor600630
3Lal SalamSmriti Irani25665
4The Fault in Our StarsJohn Green352250
5The Book ThiefMarkus Zusak624295
6Animal FarmGeorge Orwell88120
7The Ten Trillion DreamSubhash Chandra Garg712650
8UnfinishedPriyanka Chopra Jonas280420
9The Princess BrideWilliam Goldman512780
10Life of PiYann Martel352240

Example 3:

Let us consider one more table named employee_table, which contains the fields of emp_id, emp_name, emp_address, department, and salary.

emp_idemp_nameemp_addressdepartmentsalary
50JohnWashingtonSales25000
51ShirleyAustinSoftware Testing50000
52DanielColumbusSales40000
53AnthonyAustinContent Writing20000
54JamesChicagoHR45000
55HelenPhoenixSoftware Testing55000
56NicoleChicagoSales35000
57NickColumbusHR40000
58PeterPhoenixContent Writing25000
59AshleyWashingtonContent Writing35000

We will rename the column name from salary to emp_salary. The query given below will change the column name:

Query:

Output:

It is clear from the output that the column name has been changed from salary to emp_salary.

emp_idemp_nameemp_addressdepartmentemp_salary
50JohnWashingtonSales25000
51ShirleyAustinSoftware Testing50000
52DanielColumbusSales40000
53AnthonyAustinContent Writing20000
54JamesChicagoHR45000
55HelenPhoenixSoftware Testing55000
56NicoleChicagoSales35000
57NickColumbusHR40000
58PeterPhoenixContent Writing25000
59AshleyWashingtonContent Writing35000

Conclusion:

In this article, you have learned how to rename column names in SQL. You have understood that there is a slightly different way of renaming column names in different SQL databases. You have learned different ways to rename column names in SQL databases such as MySQL, Oracle, MariaDB, PostgreSQL, and Microsoft SQL Server.


Next TopicSQL EXISTS