What is a view?
A view in SQL is a virtual table based on the result of a SQL query. It doesn’t store data itself but provides a stored SELECT statement that you can query like a regular table.
🔹 Why Use a View?
- Simplify complex queries
- Enhance security by exposing only certain columns or rows
- Reuse common queries
- Abstract away the complexity of joins or calculated columns
✅ Syntax to Create a View:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
🔹 Example: Creating a View
Let’s say you have an employees
table, and you want to show only employees from the Sales department.
CREATE VIEW sales_employees AS
SELECT name, department, salary
FROM employees
WHERE department = 'Sales';
Now you can run:
SELECT * FROM sales_employees;
…and it behaves like a table, showing only the filtered data.
🔹 Update a View
You can recreate a view using CREATE OR REPLACE VIEW
:
CREATE OR REPLACE VIEW sales_employees AS
SELECT name, department, salary, hire_date
FROM employees
WHERE department = 'Sales';
🔹 Drop a View
DROP VIEW sales_employees;
🔹 Can You Update Data Through a View?
- Yes — if the view is simple (based on one table, no group by, no aggregation).
- But not possible (or limited) with complex views (with
JOIN
,GROUP BY
,DISTINCT
, etc.)
🔹 Benefits of Views:
Feature | Benefit |
---|---|
Reusability | Save and reuse complex logic |
Security | Hide sensitive columns or rows |
Simplification | Abstract joins or filters |
Maintainability | Update view logic in one place |
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.