VIEWS in SQL

A view in SQL is a virtual table that is based on the result set of a SELECT statement. It contains rows and columns, just like a real table, and the fields in a view are fields from one or more real tables in the database. Views are used to simplify complex queries, enhance security by restricting access to a subset of data, and present data in a specific format without altering the underlying tables.

Creating a View Let's create a simple example using the employees table. Table: employees

employee_idfirst_namelast_namedepartmentsalary
1JohnDoeSales50000
2JaneSmithMarketing60000
3MichaelO'ConnorSales55000
4SarahO'BrienHR45000
5LauraWilsonSales70000

Example: Create a View Suppose we want to create a view that shows the full name and department of employees in the Sales department.

SQL Syntax

CREATE VIEW SalesEmployees AS
SELECT 
    CONCAT(first_name, ' ', last_name) AS full_name, 
    department, 
    salary
FROM 
    employees
WHERE 
    department = 'Sales'; 

Explanation:

Using the View Now, let's query the SalesEmployees view.

SQL

SELECT * FROM SalesEmployees; 
full_namedepartmentsalary
John DoeSales50000
Michael O'ConnorSales55000
Laura WilsonSales70000

This query retrieves all columns from the SalesEmployees view, showing the full name, department, and salary of employees in the Sales department.