SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
A view in SQL is a virtual table based on the result-set of an SQL statement. A view consists of rows and columns, just like a regular table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE
, and JOIN
statements to a view and present the data as if the data were coming from one single table.
Here are some key points about views:
To create a view, you use the CREATE VIEW
statement. For example:
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
Suppose you have a table named "Employees" and you want a view that only shows the first name and last name:
CREATE VIEW EmployeeNames AS SELECT first_name, last_name FROM Employees;
You can then query the view as you would with a normal table:
SELECT * FROM EmployeeNames;
To update a view, you can use the CREATE OR REPLACE VIEW
statement:
CREATE OR REPLACE VIEW view_name AS SELECT columns FROM table_name WHERE condition;
To delete a view, you use the DROP VIEW
statement:
DROP VIEW view_name;
DISTINCT
keyword, etc.In summary, views are a powerful feature in SQL databases that offer a flexible and secure way to present data in different formats without altering the actual underlying tables.
How to Create Views in SQL:
-- Creating a simple view CREATE VIEW employee_view AS SELECT employee_id, first_name, last_name FROM employees WHERE department_id = 10;
Modifying and Dropping Views in SQL:
-- Modifying a view ALTER VIEW employee_view AS SELECT employee_id, first_name, last_name FROM employees WHERE department_id = 20; -- Dropping a view DROP VIEW IF EXISTS employee_view;
Views and JOIN Operations in SQL:
-- Creating a view with JOIN CREATE VIEW employee_department_view AS SELECT e.employee_id, e.first_name, e.last_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id;
Views and GROUP BY in SQL:
-- Creating a view with GROUP BY CREATE VIEW department_salary_summary AS SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;
Creating Indexed Views in SQL:
-- Creating an indexed view CREATE VIEW indexed_employee_view WITH SCHEMABINDING AS SELECT employee_id, first_name, last_name FROM dbo.employees; -- Creating an index on the view CREATE UNIQUE CLUSTERED INDEX idx_employee_id ON indexed_employee_view (employee_id);
Views with Calculated Columns in SQL:
-- Creating a view with a calculated column CREATE VIEW employee_full_name AS SELECT employee_id, first_name, last_name, first_name + ' ' + last_name AS full_name FROM employees;
Views and Permissions in SQL Server:
-- Granting SELECT permission on a view GRANT SELECT ON employee_view TO user1; -- Revoking permission REVOKE SELECT ON employee_view TO user1;
Views and Subqueries in SQL:
-- Creating a view with a subquery CREATE VIEW high_salary_employees AS SELECT employee_id, first_name, last_name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
Materialized Views in SQL:
-- Creating a materialized view CREATE MATERIALIZED VIEW mv_employee_salary AS SELECT employee_id, first_name, last_name, salary FROM employees WHERE department_id = 30;