Introduction
Views help us see data in a simple way without changing the original tables. They make complex data easier to understand and use.
Jump into concepts and practice - no test required
Views help us see data in a simple way without changing the original tables. They make complex data easier to understand and use.
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
CREATE VIEW SimpleView AS SELECT name, age FROM Employees WHERE age > 30;
CREATE VIEW SalesSummary AS SELECT product_id, SUM(quantity) AS total_sold FROM Sales GROUP BY product_id;
This example creates a table of employees, inserts some data, then creates a view showing only employees older than 30. Finally, it selects all data from the view.
CREATE TABLE Employees ( id INT, name VARCHAR(50), age INT, salary INT ); INSERT INTO Employees VALUES (1, 'Alice', 28, 50000), (2, 'Bob', 35, 60000), (3, 'Charlie', 40, 70000); CREATE VIEW OlderEmployees AS SELECT name, age FROM Employees WHERE age > 30; SELECT * FROM OlderEmployees;
Views do not store data themselves; they show data from the original tables.
Updating data through views can be limited depending on the database system.
Views simplify complex data by showing only what you need.
They help protect sensitive data by hiding columns or rows.
Views make it easier to reuse queries without rewriting them.
SELECT * FROM view_name; What is the main purpose of this?EmployeeView that shows only name and salary from Employees table?Sales with columns product, region, and amount, and the view:CREATE VIEW RegionalSales AS SELECT region, SUM(amount) AS total FROM Sales GROUP BY region;SELECT * FROM RegionalSales WHERE total > 1000; return?CREATE VIEW MyView AS SELECT id, password FROM Users;password column for security. What is the best fix?Orders table has millions of rows. Which approach best uses views to improve performance and security?