Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
How to Delete Column in SQL
Next article icon

How to Delete Column in SQL

Last Updated : 16 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL, deleting a column from an existing table is a straightforward process, but it's important to understand the implications and the correct syntax involved. While there is no direct DELETE COLUMN command in SQL, we can achieve this by using the ALTER TABLE command combined with DROP COLUMN.

In this article, we'll explain how to delete a column in SQL, providing the syntax, practical examples, and best practices for safely removing unnecessary columns from your database.

Understanding Process of Deleting a Column in SQL

Deleting a column in SQL refers to the process of permanently removing a specific column from a table. This is done using the DROP COLUMN command, which is part of Data Definition Language (DDL) operations. Deleting columns that are no longer needed can significantly improve our database's performance, reduce storage consumption, and help maintain a clean and efficient schema.

Why Deleting a Column in SQL is Important

  1. Data Optimization: Dropping columns that are no longer needed reduces the size of our table and optimizes storage space.
  2. Improved Performance: Removing unnecessary columns can help improve the performance of SQL queries since the database engine doesn't have to retrieve and process unnecessary data.
  3. Cleaner Database Schema: It helps in maintaining a clean and organized schema, especially when dealing with evolving data models.

Syntax

ALTER TABLE  Name_of_the_table
DROP COLUMN Name_of_the_column ;

Key Terms

  • ALTER TABLE: This clause is used to modify the structure of an existing table.
  • DROP COLUMN: This specifies that a column should be deleted from the table.
  • table_name: The name of the table from which the column will be removed.
  • column_name: The name of the column that you want to delete.

Example 1: Deleting a Column from a Table

Let's walk through a practical example to demonstrate how to delete a column in SQL. In this scenario, we will create 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.

Step 1: Create the Sample Table

CREATE TABLE GeeksforGeeks (
rank int,
name varchar(100),
age int,
monthly_score int,
questions_solved int,
overall_score int
);

Step 2: Insert Data into the Table

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);

Step 3: View the Data in the Table

SELECT * FROM GeeksforGeeks;

Output

GeeksforGeeks Table
GeeksforGeeks Table

Explanation:

In the "GeeksforGeeks" table, the "age" column holds no value to the table data. We can remove it using the DROP COLUMN command without affecting the rest of the table.

Step 4: Delete the age Column

If the age column is no longer needed, it can be dropped using the DROP COLUMN command.

ALTER TABLE GeeksforGeeks
DROP COLUMN age;

Step 5: Verify the Changes

Check the table structure and data after dropping the column.

SELECT * FROM GeeksforGeeks;

Output

ranknamemonthly_scorequestions_solvedoverall_score
1Vishu2724151448
2Kuntal2714101446
3Priyam2704081440

Explanation:

The "age" column has been successfully removed from the table without affecting the other columns.

Example 2: Dropping Multiple Columns

In SQL, we can also drop multiple columns in a single query by separating column names with commas. This method simplifies queries by eliminating the need to write multiple ALTER TABLE statements, making the operation more efficient.

Step 1: Create a Table

CREATE TABLE Products (
product_id INT,
product_name VARCHAR(50),
category VARCHAR(50),
price DECIMAL(10, 2),
stock_quantity INT
);

Step 2: Insert Sample Data

INSERT INTO Products (product_id, product_name, category, price, stock_quantity) 
VALUES
(1, 'Laptop', 'Electronics', 800.00, 50),
(2, 'Desk', 'Furniture', 150.00, 20),
(3, 'Chair', 'Furniture', 85.00, 100);

Step 3: Drop "category" and "stock_quantity" Columns

ALTER TABLE Products
DROP COLUMN category, stock_quantity;

Step 4: View the Updated Table

SELECT * FROM Products;

Output

product_idproduct_nameprice
1Laptop800.00
2Desk150.00
3Chair85.00

Explanation:

Both the "category" and "stock_quantity" columns have been successfully removed, leaving only the relevant columns in the table.

Important Notes About Using DROP COLUMN in SQL

  • Irreversible Change: Once we drop a column using the DROP COLUMN command, the data is permanently lost, and the column cannot be recovered.
  • Foreign Key Constraints: If the column being dropped is referenced by foreign key constraints, we will need to drop those constraints before dropping the column.
  • Large Tables: Dropping a column in a large table might take some time, depending on the size of the table and the database system.

Conclusion

The DROP COLUMN command is a useful tool for database administrators and developers to clean up unnecessary data and optimize table structures in SQL. By removing redundant or irrelevant columns, we can reduce storage space, improve query performance, and maintain a cleaner schema. However, it's essential to carefully review the column before dropping it since the operation is permanent.


Next Article
How to Delete Column in SQL

V

vishuvaishnav3001
Improve
Article Tags :
  • SQL
  • Databases

Similar Reads

    How to Delete Blank Columns in Excel
    Empty columns in your Excel spreadsheet can make it look messy and also look unprofessional. Now, whether you are doing a budget analysis, sorting data, or making a report, removing blank columns is a way to make your report professional and cleaner. Now, if you are new to MS Excel and want to know
    5 min read
    How to delete columns in PySpark dataframe ?
    In this article, we are going to delete columns in Pyspark dataframe. To do this we will be using the drop() function. This function can be used to remove values from the dataframe. Syntax: dataframe.drop('column name') Python code to create student dataframe with three columns: Python3 # importing
    2 min read
    How to Get the Type of Columns in SQL
    In SQL, the types of columns in a database table play a fundamental role in data management and query execution. Each column is designed to store specific types of data, such as numbers, text, dates, or binary data. Understanding these column types is essential for effective database design, query o
    4 min read
    SQL Query to Delete Duplicate Columns
    Through this article, we will learn how to delete duplicate columns from a database table. As we know that duplicity in our database tends to be a waste of memory space. It records inaccurate data and is also unable to fetch the correct data from the database. To remove the duplicate columns we use
    2 min read
    How to Rename a Column in MySQL?
    Renaming columns in MySQL is a frequent task to keep data organized and flexible. It helps adjust database layouts to fit new needs without losing information. This article will show you different ways to rename columns in MySQL, making it easier to manage and update your database structure as your
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences