Open In App

SQL DROP COLUMN

Last Updated : 13 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Data Definition Language (DDL) command DROP COLUMN is used in SQL (Structured Query Language). The structure of database objects like tables can be defined, altered, or deleted using DDL statements in SQL. A column can be removed from a database table using the DROP COLUMN command. The table is altered permanently by this command. There is no going back after you make any modifications using this command. So it’s better to be sure before making any changes. Rearranging the database may benefit from dropping or deleting a column. Making the database more efficient involves removing any superfluous or unneeded columns that have no bearing on the table’s context.

The Syntax for the DROP COLUMN Command:

ALTER TABLE Name_of_the_table

DROP COLUMN Name_of_the_column ;

here “Name_of_the_table” and “Name_of_the_column” are placeholders.

Implementation of DROP COLUMN Command

Let’s consider a table named GeeksforGeeks. This table consists of rank, name, age, monthly score, questions solved, and overall score as the columns of the table GeeksforGeeks.

Query

CREATE TABLE GeeksforGeeks (
rank int,
name varchar(100),
age int,
monthly_score int,
questions_solved int,
overall_score int
);
---first we create a table name GeeksforGeeks

INSERT INTO GeeksforGeeks(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (01, 'Vishu', 20 ,272 ,415 ,1448);
INSERT INTO GeeksforGeeks(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (02, 'Kuntal', 20 ,271 ,410 ,1446);
INSERT INTO GeeksforGeeks(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (03, 'Priyam', 20 ,270 ,408 ,1440);
INSERT INTO GeeksforGeeks(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (04, 'Shailesh', 21 ,268 ,407 ,1438);
INSERT INTO GeeksforGeeks(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (05, 'Avirup', 20 ,267 ,406 ,1437);
INSERT INTO GeeksforGeeks(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (06, 'Neeraj', 21 ,265 ,405 ,1436);

---We insert values to the table

SELECT * FROM GeeksforGeeks;

---we are now displaying the values from the table

Output

GeeksforGeeks Table

GeeksforGeeks Table

Now, in the table “GeeksforGeeks,” we can figure out that the “age” column holds no value to the table data. It is irrelevant to the other columns data. So we decided to remove it from the table. With the DROP COLUMN command, we can remove the age column without hampering the table data.

Query

Here comes the DROP COLUMN command in the picture.

ALTER TABLE  GeeksforGeeks
DROP COLUMN age ;

---Dropping the column age

SELECT * FROM GeeksforGeeks;

--- Displaying the table

Output

output

output

Note:

Remember that SQL does not care about case. The way SQL commands are typically written, 
though, is as just described. But that does meant that you have to write it in that way though it is highly
recommended to use a standard way when you are dealing in professional scenarios.

Conclusion

DROP COLUMN is a Data Definition Language (DDL) command in SQL. For a variety of particular goals, such as database reorganization and deleting obsolete or unnecessary columns to boost overall productivity and data management, the DROP COLUMN command is used to remove a column from a table. This command permanently changes the structure of the table. Therefore, before doing the operation, it is imperative to confirm that you want to delete those particular columns.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads