SQL Views Practice Questions with Solutions

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_idemployee_namedepartmentsalary
101AmanHR45000
102RiyaIT70000
103VikasFinance85000
104NehaIT52000
105KaranHR60000
106SnehaMarketing90000

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_idemployee_namedepartmentsalary
101AmanHR45000
102RiyaIT70000
103VikasFinance85000
104NehaIT52000
105KaranHR60000
106SnehaMarketing90000

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_namesalary
Riya70000
Neha52000

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_namedepartment
AmanHR
RiyaIT
VikasFinance
NehaIT
KaranHR
SnehaMarketing

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_idproduct_namecategoryprice
101LaptopElectronics65000
102KeyboardAccessories1200
103MonitorElectronics18000
104TabletElectronics28000
105HeadphonesAccessories2500
106Gaming PCElectronics85000

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_namecategoryprice
LaptopElectronics65000
TabletElectronics28000
Gaming PCElectronics85000

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_idcustomer_nametotal_purchase
1Rahul8500
2Neha15000
3Amit9500
4Priya22000
5Sneha18000

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_nametotal_purchase
Neha15000
Priya22000
Sneha18000

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_idemployee_namedepartment_id
101Aman1
102Riya2
103Vikas3
104Neha2
105Karan1

departments

department_iddepartment_name
1HR
2IT
3Finance

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_namedepartment_name
AmanHR
RiyaIT
VikasFinance
NehaIT
KaranHR

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_namedepartmentsalary
AmanHR45000
KaranHR52000
RiyaIT70000
NehaIT72000
VikasFinance85000

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

departmentaverage_salary
HR48500
IT71000
Finance85000

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_idsale_monthamount
1January25000
2January18000
3February30000
4February15000
5March42000

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_monthtotal_sales
January43000
February45000
March42000

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_namedepartmentsalary
AmanHR45000
RiyaIT70000
NehaIT52000
VikasFinance85000
RohitIT82000

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_namesalary
Riya70000
Rohit82000

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

branchrevenue
Delhi250000
Delhi180000
Noida320000
Noida150000
Gurgaon280000

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

branchtotal_revenue
Delhi430000
Noida470000
Gurgaon280000

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_namedepartmentsalary
AmanHR45000
RiyaIT70000
VikasFinance85000
NehaIT52000

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_idemployee_namesalary
101Aman50000

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_namedepartment_idbranch_id
Aman1101
Riya2102
Vikas3101

departments

department_iddepartment_name
1HR
2IT
3Finance

branches

branch_idbranch_name
101Delhi
102Noida

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_namedepartment_namebranch_name
AmanHRDelhi
RiyaITNoida
VikasFinanceDelhi

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_idcustomer_name
1Rahul
2Neha

products

product_idproduct_name
101Laptop
102Tablet

orders

order_idcustomer_idproduct_idamount
5001110165000
5002210228000

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_nameproduct_nameamount
RahulLaptop65000
NehaTablet28000

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 VIEW creates a new view.
  • CREATE OR REPLACE VIEW modifies an existing view.
  • DROP VIEW removes 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?

TableView
Stores data physicallyStores only a SQL query
Occupies storageDoes not store data
Can exist independentlyDepends 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.

Scroll to Top