codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

30 / 67 topics
28Subqueries Basics29Correlated Subqueries30Views
Tutorials/SQL & Databases/Views
🗄️SQL & Databases

Views

Updated 2026-05-15
10 min read

Views

Introduction

In SQL, views are virtual tables that are derived from one or more base tables. They provide a way to simplify complex queries and enhance data security by restricting access to the underlying tables. Views can be thought of as stored SELECT statements that return rows from one or more tables. Unlike physical tables, views do not store data; instead, they derive their data from the tables on which they are based.

Views offer several benefits:

  • Data Abstraction: They allow users to interact with a database without needing to know the underlying table structures.
  • Security: Views can be used to restrict access to sensitive data by exposing only specific columns or rows.
  • Simplified Queries: Complex queries can be encapsulated within views, making them easier to manage and reuse.

Concept

A view is essentially a stored query that can be treated as a virtual table. When you create a view, you define a SELECT statement that retrieves the data you want to expose through the view. The view does not store any data itself; instead, it dynamically generates the result set based on the underlying tables whenever it is queried.

Creating a View

To create a view in SQL, you use the CREATE VIEW statement followed by the name of the view and the SELECT statement that defines the view's content. Here is the basic syntax:

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example

Let's consider a simple example to illustrate how views work. Suppose we have a table named employees with the following structure:

employee_idfirst_namelast_namedepartmentsalary
1JohnDoeHR5000
2JaneSmithFinance6000
3AliceJohnsonIT7000

We want to create a view that shows only the employee_id, first_name, and last_name of employees in the IT department. Here's how you can do it:

CREATE VIEW IT_Employees AS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department = 'IT';

Now, whenever you query the IT_Employees view, it will return only the rows from the employees table where the department is IT.

Querying a View

To query a view, you use the same SELECT statement as you would for a regular table. Here's how you can retrieve data from the IT_Employees view:

SELECT * FROM IT_Employees;

The output will be:

employee_idfirst_namelast_name
3AliceJohnson

Modifying a View

If you need to modify an existing view, you can use the ALTER VIEW statement. For example, if we want to add the salary column to the IT_Employees view, we can do it like this:

ALTER VIEW IT_Employees AS
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department = 'IT';

Dropping a View

If you need to delete a view, you can use the DROP VIEW statement. Here's how you can drop the IT_Employees view:

DROP VIEW IT_Employees;

What's Next?

In this tutorial, we covered the basics of creating and using views in SQL. Views are a powerful tool for data abstraction and security, allowing you to simplify complex queries and control access to sensitive data.

Next, we will explore stored procedures, which provide another level of abstraction by encapsulating procedural logic within the database. Stored procedures can include multiple SQL statements and control structures, making them more flexible than views.


PreviousCorrelated SubqueriesNext Stored Procedures

Recommended Gear

Correlated SubqueriesStored Procedures