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:
SQL - DROP View
Next article icon

SQL - DROP View

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

SQL Views provide a powerful way to simplify complex queries and present data in a more understandable format. However, there may be times when we need to remove a view from our database schema. In SQL, deleting a view is straightforward using the DROP VIEW command.

In this article, we will explain the process of deleting views in SQL, why and when to remove a view, and provide examples and outputs to demonstrate how to effectively manage views in our database.

SQL DROP VIEW Command

In SQL, there is no direct DELETE VIEW command. To remove a view from a database, you must use the DROP VIEW command. This command deletes the view definition from the database. However, DROP VIEW does not affect any underlying data in the base tables that the view was created from. It only removes the virtual table (view) from the database schema.

Syntax:

DROP VIEW view_name;

Creating Views

Consider the following EMPLOYEES table that stores data about employees. This table contains important information such as employee names, positions, salaries, and departments. We can create views to simplify the querying process and present data in a more structured way, focusing on specific subsets of the data.

Employees-table
Employees Table

1. View for High Salary Employees

The HighSalaryEmployees view filters the EMPLOYEES table to include only those employees with a salary greater than 50,000. The query retrieves columns such as ID, Name, Position, Salary, and Department, and returns records for employees who meet the salary condition.

Query:

CREATE VIEW HighSalaryEmployees AS
SELECT * FROM EMPLOYEES WHERE Salary > 50000;

Output

IDNamePositionSalaryDepartment
1RohanManager57000HR
3ArpitSenior Developer60000IT

2. View for Developers

The Developers view filters the EMPLOYEES table to retrieve employees whose Position contains the word "Developer". The view returns the Name, Position, and Department of employees who have roles related to development within the IT department.

Query:

CREATE VIEW Developers AS
SELECT Name, Position, Department FROM EMPLOYEES WHERE Position LIKE '%Developer%';

Output

NamePositionDepartment
AryanDeveloperIT
ArpitSenior DeveloperIT
TaraDeveloperIT

3. View for Employees in IT Department

The ITEmployees view filters the EMPLOYEES table to include only those employees who belong to the IT department. It retrieves all columns (ID, Name, Position, Salary, and Department) for employees working in the IT department, making it easier to query and manage IT-related employee data.

Query:

CREATE VIEW ITEmployees AS
SELECT * FROM EMPLOYEES WHERE Department = 'IT';

Output

IDNamePositionSalaryDepartment
2AryanDeveloper45000IT
3ArpitSenior Developer60000IT
5TaraDeveloper55000IT

To check the Created Views

SELECT TABLE_SCHEMA, 
EMPLOYEES FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA='GFG';

To confirm that our views are created we can use the query mentioned above it will show us so our the views that we have created in form of table something like this:

TABLE_SCHEMA

EMPLOYEES

GFG

HighSalaryEmployees

GFG

GOOD_EMPLOYEE

GFG

TOP_EMPLOYEE

Deleting a View

Once we no longer need a view, we can delete it using the DROP VIEW command. For example, let’s delete the HighSalaryEmployees and ITEmployees views.

DROP VIEW HighSalaryEmployees;
DROP VIEW TOP_EMPLOYEE;

After executing this, these views will no longer exist in the database schema. We can confirm by querying the INFORMATION_SCHEMA.VIEWS table to see the remaining views:

SELECT TABLE_SCHEMA, EMPLOYEES 
FROM
INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA='GFG';

Output:

TABLE_SCHEMA

EMPLOYEES

GFG

GOOD_EMPLOYEE

Explanation:

This query retrieves information about views from the INFORMATION_SCHEMA.VIEWS table for a specific TABLE_SCHEMA ('GFG'). It selects the TABLE_SCHEMA and EMPLOYEES columns from the VIEWS table, filtering the results to only include views from the 'GFG' schema.

Verifying the Deletion

To confirm the views have been successfully deleted, we can run the following query again. The HighSalaryEmployees and ITEmployees views should no longer appear in the result.

Query:

SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'your_schema_name';

Explanation:

The above query retrieves all the views in the specified schema. After deleting the views, only the Developers view will remain.

Why and When Should You Use DROP VIEW?

  1. Outdated Views: If a view is no longer relevant because the underlying data structure has changed or the view is based on obsolete requirements, it’s a good idea to drop it to keep the database clean.
  2. Performance Optimization: Removing unnecessary views can help optimize query performance and maintain database health.
  3. Security: Views may sometimes expose sensitive data or provide too much access. Dropping a view when it's no longer needed helps maintain the principle of least privilege.
  4. Database Maintenance: As our database grows, managing views becomes critical. Keeping only the necessary views can improve overall maintainability.

Conclusion

The DROP VIEW command in SQL is a simple and effective way to remove views that are no longer needed in the database schema. It is essential for maintaining a clean and efficient database by eliminating outdated or unused views. Understanding when to use DROP VIEW helps optimize database performance, reduce clutter, and enhance security. By mastering the DROP VIEW command, we ensure that our database remains organized and easy to manage, ensuring only relevant views are present and that we comply with data access control best practices.


Next Article
SQL - DROP View

W

writer01
Improve
Article Tags :
  • SQL
  • Databases

Similar Reads

    MySQL - Drop View
    MySQL is a powerful open-source relational database management system that is widely used for building scalable and high-performance databases. Developed by MySQL AB, which is currently owned by Oracle Corporation, MySQL has been around since 1995. It is known for its robust, easy-to-use, and reliab
    5 min read
    SQLite DROP VIEW
    SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. It is written in C language and is used by developers who embed a lightweight database over the existing application, browser, or embedded systems. The main features of SQLite are that it is a tiny,
    4 min read
    PL/SQL DROP VIEW
    Views serve as an important tool for simplifying complex queries, enhancing data security, and streamlining database management. They provide a virtual layer that presents data from underlying tables in a structured format. However, views like other database objects need to be managed effectively to
    3 min read
    PL/SQL VIEW
    In Oracle PL/SQL, views are a powerful way to manage data access and simplify complex queries. A view is essentially a virtual table that presents data from one or more tables using a stored query. Unlike physical tables, views do not store the data themselves; they dynamically retrieve data based o
    4 min read
    SQL DROP TABLE
    The DROP TABLE command in SQL is a powerful and essential tool used to permanently delete a table from a database, along with all of its data, structure, and associated constraints such as indexes, triggers, and permissions. When executed, this command removes the table and all its contents, making
    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