A SQL View is a virtual table created from the result of one or more SQL queries.
Unlike a regular table, a view does not store data physically. Instead, it stores the SQL query, and whenever you access the view, SQL executes the stored query and displays the latest data. SQL Views practice questions with solutions help to understand the concepts.
Views are commonly used to:
- Simplify complex SQL queries
- Hide sensitive information
- Improve query readability
- Reuse frequently used queries
- Restrict user access to specific columns
- Create reporting dashboards
Views are widely used in:
- Banking Systems
- HR Management
- Business Intelligence
- Financial Reporting
- Sales Dashboards
- Inventory Management
Why Use SQL Views?
Views provide several advantages:
- Simplify complex queries
- Improve database security
- Hide confidential columns
- Reduce repeated SQL code
- Create reusable reports
- Improve application development
Syntax
Create a View
CREATE VIEW view_name AS
SELECT
column1,
column2
FROM table_name;
Display Data from a View
SELECT *
FROM view_name;
Sample Table Used in This Chapter
employees
| employee_id | employee_name | department | salary |
|---|---|---|---|
| 101 | Aman | HR | 45000 |
| 102 | Riya | IT | 70000 |
| 103 | Vikas | Finance | 85000 |
| 104 | Neha | IT | 52000 |
| 105 | Karan | HR | 60000 |
| 106 | Sneha | Marketing | 90000 |
1. SQL CREATE VIEW to Display Employee Information
Problem Statement
A company frequently generates employee reports.
Instead of writing the same query repeatedly, create a View.
SQL Solution
CREATE VIEW employee_view AS
SELECT
employee_id,
employee_name,
department,
salary
FROM employees;
Display the View
SELECT *
FROM employee_view;
Sample Output
| employee_id | employee_name | department | salary |
|---|---|---|---|
| 101 | Aman | HR | 45000 |
| 102 | Riya | IT | 70000 |
| 103 | Vikas | Finance | 85000 |
| 104 | Neha | IT | 52000 |
| 105 | Karan | HR | 60000 |
| 106 | Sneha | Marketing | 90000 |
Explanation
The view stores the SQL query.
Whenever you execute:
SELECT * FROM employee_view;
the database automatically retrieves the latest employee information.
Concepts Covered
- CREATE VIEW
- Virtual Table
- SELECT
2. SQL View to Display Only IT Department Employees
Problem Statement
The HR team frequently checks only employees from the IT department.
Create a View for IT employees.
SQL Solution
CREATE VIEW it_employee_view AS
SELECT
employee_name,
salary
FROM employees
WHERE department = 'IT';
Display the View
SELECT *
FROM it_employee_view;
Sample Output
| employee_name | salary |
|---|---|
| Riya | 70000 |
| Neha | 52000 |
Explanation
The View permanently stores the filtering logic.
Users only need:
SELECT * FROM it_employee_view;
instead of writing the complete SQL query every time.
Concepts Covered
- Views
- WHERE Clause
- Department Reports
3. SQL View to Hide Confidential Salary Information
Problem Statement
A company wants employees to view only names and departments.
Salary information should remain hidden.
Create an SQL View.
SQL Solution
CREATE VIEW public_employee_view AS
SELECT
employee_name,
department
FROM employees;
Display the View
SELECT *
FROM public_employee_view;
Sample Output
| employee_name | department |
|---|---|
| Aman | HR |
| Riya | IT |
| Vikas | Finance |
| Neha | IT |
| Karan | HR |
| Sneha | Marketing |
Explanation
The salary column is excluded from the View.
Users accessing the View cannot see confidential salary information.
This is one of the biggest advantages of SQL Views for database security.
Concepts Covered
- Views
- Security
- Column Restriction
4. SQL View to Display Premium Products
Problem Statement
An e-commerce company wants a reusable report that displays only premium products priced above ₹20,000.
Instead of writing the same filter every time, create a View.
Sample Table
products
| product_id | product_name | category | price |
|---|---|---|---|
| 101 | Laptop | Electronics | 65000 |
| 102 | Keyboard | Accessories | 1200 |
| 103 | Monitor | Electronics | 18000 |
| 104 | Tablet | Electronics | 28000 |
| 105 | Headphones | Accessories | 2500 |
| 106 | Gaming PC | Electronics | 85000 |
SQL Solution
CREATE VIEW premium_products AS
SELECT
product_name,
category,
price
FROM products
WHERE price > 20000;
Display the View
SELECT *
FROM premium_products;
Sample Output
| product_name | category | price |
|---|---|---|
| Laptop | Electronics | 65000 |
| Tablet | Electronics | 28000 |
| Gaming PC | Electronics | 85000 |
Explanation
The view stores only products costing more than ₹20,000.
Whenever management needs a premium product report, they can simply execute:
SELECT *
FROM premium_products;
This avoids rewriting the filtering condition repeatedly.
Concepts Covered
- CREATE VIEW
- WHERE Clause
- Product Reports
5. SQL View to Display High-Value Customers
Problem Statement
A retail company wants to generate a reusable report containing customers whose total purchases exceed ₹10,000.
Create a View.
Sample Table
customers
| customer_id | customer_name | total_purchase |
|---|---|---|
| 1 | Rahul | 8500 |
| 2 | Neha | 15000 |
| 3 | Amit | 9500 |
| 4 | Priya | 22000 |
| 5 | Sneha | 18000 |
SQL Solution
CREATE VIEW high_value_customers AS
SELECT
customer_name,
total_purchase
FROM customers
WHERE total_purchase > 10000;
Display the View
SELECT *
FROM high_value_customers;
Sample Output
| customer_name | total_purchase |
|---|---|
| Neha | 15000 |
| Priya | 22000 |
| Sneha | 18000 |
Explanation
The view permanently stores the query for high-value customers.
Instead of writing the condition each time, users only need:
SELECT *
FROM high_value_customers;
This makes customer reports faster and easier to generate.
Concepts Covered
- SQL Views
- Customer Reports
- Business Analytics
Advantages of SQL Views
Views offer several practical benefits:
- Simplify complex SQL queries.
- Hide sensitive columns such as salary or passwords.
- Create reusable reports.
- Improve database security.
- Reduce duplicate SQL code.
- Make applications easier to maintain.
Real-World Use Cases of SQL Views
SQL Views are commonly used for:
- Employee reports
- Payroll dashboards
- Sales reports
- Customer analytics
- Inventory monitoring
- Financial statements
- Business Intelligence dashboards
- Hospital management systems
- Banking applications
- E-commerce reporting
6. SQL View Using JOIN to Display Employee and Department Details
Problem Statement
A company stores employee information and department information in separate tables.
Create a View that displays employee names along with their department names.
Sample Tables
employees
| employee_id | employee_name | department_id |
|---|---|---|
| 101 | Aman | 1 |
| 102 | Riya | 2 |
| 103 | Vikas | 3 |
| 104 | Neha | 2 |
| 105 | Karan | 1 |
departments
| department_id | department_name |
|---|---|
| 1 | HR |
| 2 | IT |
| 3 | Finance |
SQL Solution
CREATE VIEW employee_department_view AS
SELECT
e.employee_name,
d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;
Display the View
SELECT *
FROM employee_department_view;
Sample Output
| employee_name | department_name |
|---|---|
| Aman | HR |
| Riya | IT |
| Vikas | Finance |
| Neha | IT |
| Karan | HR |
Explanation
This view combines two tables using an INNER JOIN.
Instead of writing the join repeatedly, users can directly query the view.
Concepts Covered
- CREATE VIEW
- INNER JOIN
- Multi-table Reports
7. SQL Aggregate View for Department-wise Average Salary
Problem Statement
The HR department wants a reusable report showing the average salary of each department.
Create a View.
Sample Table
employees
| employee_name | department | salary |
|---|---|---|
| Aman | HR | 45000 |
| Karan | HR | 52000 |
| Riya | IT | 70000 |
| Neha | IT | 72000 |
| Vikas | Finance | 85000 |
SQL Solution
CREATE VIEW department_average_salary AS
SELECT
department,
AVG(salary) AS average_salary
FROM employees
GROUP BY department;
Display the View
SELECT *
FROM department_average_salary;
Sample Output
| department | average_salary |
|---|---|
| HR | 48500 |
| IT | 71000 |
| Finance | 85000 |
Explanation
This view calculates department-wise average salaries.
The aggregation logic is stored inside the view, making salary reports much simpler.
Concepts Covered
- GROUP BY
- AVG()
- Aggregate Views
8. SQL View to Display Monthly Sales Summary
Problem Statement
A company records daily sales transactions.
Create a View that displays monthly sales totals.
Sample Table
sales
| sale_id | sale_month | amount |
|---|---|---|
| 1 | January | 25000 |
| 2 | January | 18000 |
| 3 | February | 30000 |
| 4 | February | 15000 |
| 5 | March | 42000 |
SQL Solution
CREATE VIEW monthly_sales_summary AS
SELECT
sale_month,
SUM(amount) AS total_sales
FROM sales
GROUP BY sale_month;
Display the View
SELECT *
FROM monthly_sales_summary;
Sample Output
| sale_month | total_sales |
|---|---|
| January | 43000 |
| February | 45000 |
| March | 42000 |
Explanation
The view summarizes daily sales into monthly totals.
This type of report is commonly used by finance and management teams.
Concepts Covered
- SUM()
- GROUP BY
- Monthly Reports
9. SQL View Using Multiple Conditions
Problem Statement
Display employees who:
- Work in the IT department
- Earn more than ₹60,000
Create a reusable View.
Sample Table
employees
| employee_name | department | salary |
|---|---|---|
| Aman | HR | 45000 |
| Riya | IT | 70000 |
| Neha | IT | 52000 |
| Vikas | Finance | 85000 |
| Rohit | IT | 82000 |
SQL Solution
CREATE VIEW senior_it_employees AS
SELECT
employee_name,
salary
FROM employees
WHERE department = 'IT'
AND salary > 60000;
Display the View
SELECT *
FROM senior_it_employees;
Sample Output
| employee_name | salary |
|---|---|
| Riya | 70000 |
| Rohit | 82000 |
Explanation
This view combines multiple filtering conditions using AND.
It allows HR to quickly retrieve senior IT employees without rewriting the query.
Concepts Covered
- WHERE
- AND
- Filtered Views
10. SQL View to Display Branch-wise Revenue
Problem Statement
A company wants a reusable report showing total revenue generated by each branch.
Create a View.
Sample Table
branch_sales
| branch | revenue |
|---|---|
| Delhi | 250000 |
| Delhi | 180000 |
| Noida | 320000 |
| Noida | 150000 |
| Gurgaon | 280000 |
SQL Solution
CREATE VIEW branch_revenue_report AS
SELECT
branch,
SUM(revenue) AS total_revenue
FROM branch_sales
GROUP BY branch;
Display the View
SELECT *
FROM branch_revenue_report;
Sample Output
| branch | total_revenue |
|---|---|
| Delhi | 430000 |
| Noida | 470000 |
| Gurgaon | 280000 |
Explanation
The view calculates total revenue for each branch.
Managers can retrieve the latest branch performance simply by querying the view.
Concepts Covered
- GROUP BY
- SUM()
- Revenue Reports
- Aggregate Views
11. SQL CREATE OR REPLACE VIEW
Problem Statement
A company already has an employee view.
Management now wants the view to also display the employee’s salary.
Instead of dropping the existing view, modify it using CREATE OR REPLACE VIEW.
Existing View
CREATE VIEW employee_view AS
SELECT
employee_name,
department
FROM employees;
SQL Solution
CREATE OR REPLACE VIEW employee_view AS
SELECT
employee_name,
department,
salary
FROM employees;
Display the Updated View
SELECT *
FROM employee_view;
Sample Output
| employee_name | department | salary |
|---|---|---|
| Aman | HR | 45000 |
| Riya | IT | 70000 |
| Vikas | Finance | 85000 |
| Neha | IT | 52000 |
Explanation
CREATE OR REPLACE VIEW modifies an existing view without deleting it first.
It is useful when report requirements change.
Concepts Covered
- CREATE OR REPLACE VIEW
- View Modification
- Report Maintenance
12. SQL Update Data Through a View
Problem Statement
The HR department wants to update an employee’s salary using a View.
Sample View
CREATE VIEW hr_employee_view AS
SELECT
employee_id,
employee_name,
salary
FROM employees;
SQL Solution
UPDATE hr_employee_view
SET salary = 50000
WHERE employee_id = 101;
Verify the Update
SELECT *
FROM hr_employee_view;
Sample Output
| employee_id | employee_name | salary |
|---|---|---|
| 101 | Aman | 50000 |
Explanation
Some SQL databases allow updates through a View if:
- The View references only one table.
- No aggregate functions are used.
- No
GROUP BY,DISTINCT, or complex joins exist.
Views containing joins or aggregates are generally not updatable.
Concepts Covered
- UPDATE
- Updatable Views
- Data Modification
13. SQL DROP VIEW
Problem Statement
A report is no longer required.
Remove the existing View from the database.
SQL Solution
DROP VIEW employee_view;
Explanation
DROP VIEW permanently removes the View definition.
The original table and its data remain unchanged.
Only the virtual table is deleted.
Concepts Covered
- DROP VIEW
- Database Objects
- View Management
14. SQL Complex View Using Multiple JOINs
Problem Statement
A company wants a dashboard showing:
- Employee Name
- Department Name
- Branch Name
The information is stored in three different tables.
Create a View.
Sample Tables
employees
| employee_name | department_id | branch_id |
|---|---|---|
| Aman | 1 | 101 |
| Riya | 2 | 102 |
| Vikas | 3 | 101 |
departments
| department_id | department_name |
|---|---|
| 1 | HR |
| 2 | IT |
| 3 | Finance |
branches
| branch_id | branch_name |
|---|---|
| 101 | Delhi |
| 102 | Noida |
SQL Solution
CREATE VIEW employee_dashboard AS
SELECT
e.employee_name,
d.department_name,
b.branch_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
INNER JOIN branches b
ON e.branch_id = b.branch_id;
Display the View
SELECT *
FROM employee_dashboard;
Sample Output
| employee_name | department_name | branch_name |
|---|---|---|
| Aman | HR | Delhi |
| Riya | IT | Noida |
| Vikas | Finance | Delhi |
Explanation
This View combines data from three related tables.
It is useful for dashboards where users need complete information without writing multiple joins.
Concepts Covered
- CREATE VIEW
- Multiple INNER JOINs
- Dashboard Reports
15. SQL Business Dashboard View Combining Multiple Tables
Problem Statement
An online retail company wants a dashboard showing:
- Customer Name
- Product Name
- Order Amount
The data is stored in different tables.
Create a reusable View.
Sample Tables
customers
| customer_id | customer_name |
|---|---|
| 1 | Rahul |
| 2 | Neha |
products
| product_id | product_name |
|---|---|
| 101 | Laptop |
| 102 | Tablet |
orders
| order_id | customer_id | product_id | amount |
|---|---|---|---|
| 5001 | 1 | 101 | 65000 |
| 5002 | 2 | 102 | 28000 |
SQL Solution
CREATE VIEW sales_dashboard AS
SELECT
c.customer_name,
p.product_name,
o.amount
FROM orders o
INNER JOIN customers c
ON o.customer_id = c.customer_id
INNER JOIN products p
ON o.product_id = p.product_id;
Display the View
SELECT *
FROM sales_dashboard;
Sample Output
| customer_name | product_name | amount |
|---|---|---|
| Rahul | Laptop | 65000 |
| Neha | Tablet | 28000 |
Explanation
This View creates a business dashboard by combining customer, product, and order information into a single reusable query.
It reduces development time and simplifies reporting.
Concepts Covered
- SQL Views
- Multiple JOINs
- Dashboard Reporting
- Business Intelligence
Chapter Summary
In this chapter, you learned how SQL Views help simplify database queries by creating virtual tables based on SQL statements.
Unlike physical tables, views do not store data. They store only the SQL query definition and always display the latest data from the underlying tables.
Throughout this chapter, you practiced:
- Creating a simple SQL View
- Displaying data using a View
- Creating filtered Views
- Hiding confidential columns
- Creating Views using
JOIN - Creating aggregate Views
- Creating monthly reporting Views
- Using multiple filtering conditions
- Replacing an existing View
- Updating data through an updatable View
- Dropping a View
- Creating business dashboard Views
SQL Views are widely used in reporting systems, ERP software, CRM applications, HR management systems, and Business Intelligence dashboards.
Key Takeaways
- A View is a virtual table based on a SQL query.
- Views do not store data physically.
- Views always return the latest data from the base tables.
CREATE VIEWcreates a new view.CREATE OR REPLACE VIEWmodifies an existing view.DROP VIEWremoves a view from the database.- Views improve database security by hiding sensitive columns.
- Views simplify complex queries involving joins and aggregations.
- Some simple Views are updatable, while Views using joins or aggregate functions are generally read-only.
- Views improve code reusability and reduce repetitive SQL.
Frequently Asked Questions (FAQs)
1. What is a SQL View?
A SQL View is a virtual table created from the result of a SQL query.
Example:
CREATE VIEW employee_view AS
SELECT
employee_name,
department
FROM employees;
2. Why are SQL Views used?
Views are used to:
- Simplify complex queries
- Improve security
- Hide confidential data
- Create reusable reports
- Reduce duplicate SQL code
3. Do SQL Views store data?
No.
A View stores only the SQL query.
Whenever the View is accessed, the database executes the stored query and returns the latest data.
4. What is the difference between a Table and a View?
| Table | View |
|---|---|
| Stores data physically | Stores only a SQL query |
| Occupies storage | Does not store data |
| Can exist independently | Depends on one or more tables |
5. Can a SQL View be updated?
Yes, but only under certain conditions.
A View is generally updatable when:
- It is based on a single table.
- It does not use
GROUP BY. - It does not use aggregate functions.
- It does not use
DISTINCT. - It does not contain complex joins.
6. What does CREATE OR REPLACE VIEW do?
It modifies an existing View without dropping it first.
Example:
CREATE OR REPLACE VIEW employee_view AS
SELECT
employee_name,
salary
FROM employees;
7. How do you delete a View?
Use the DROP VIEW statement.
Example:
DROP VIEW employee_view;
Only the View is deleted. The original table remains unchanged.
8. Where are SQL Views used in real-world applications?
SQL Views are widely used in:
- HR Dashboards
- Banking Systems
- Sales Reporting
- Customer Analytics
- Financial Reports
- ERP Software
- CRM Applications
- Inventory Management
- Business Intelligence
- Data Warehousing
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
