0
0
SQLquery~5 mins

Querying through views in SQL

Choose your learning style9 modes available
Introduction
Views let you look at data like a saved question. You can ask the view for data without writing the full question again.
You want to simplify complex queries for easier use.
You need to share specific data with others without giving full table access.
You want to reuse the same query many times without rewriting it.
You want to hide some columns or rows from users for security.
You want to organize data in a way that makes sense for your reports.
Syntax
SQL
SELECT column1, column2 FROM view_name WHERE condition;
A view acts like a virtual table based on a saved SELECT query.
You query a view just like a regular table.
Examples
Get all columns and rows from the view named employee_view.
SQL
SELECT * FROM employee_view;
Get names and salaries of employees earning more than 50000 from the view.
SQL
SELECT name, salary FROM employee_view WHERE salary > 50000;
Count how many rows are in the view.
SQL
SELECT COUNT(*) FROM employee_view;
Sample Program
First, we create a view called employee_view that shows active employees. Then, we ask the view for names and departments of employees who earn more than 60000.
SQL
CREATE VIEW employee_view AS
SELECT id, name, department, salary FROM employees WHERE active = 1;

SELECT name, department FROM employee_view WHERE salary > 60000;
OutputSuccess
Important Notes
Views do not store data themselves; they show data from the original tables.
If the original data changes, the view shows the updated data automatically.
Some views cannot be updated directly depending on how they are created.
Summary
Views are saved queries that act like tables.
You query views just like normal tables.
Views help simplify and secure data access.