Query
Instructions written in a query language (such as SQL) that retrieve and manipulate data from a database.
View
A virtual table created by a query that presents data in a specific format without modifying the underlying tables.
- Views are virtual tables that do not store data themselves.
- Instead, they display the results of a query.
How Queries Provide Views
- Selection: Queries can filter rows based on specific criteria using the WHERE clause.
- Projection: Queries can select specific columns to display , reducing the amount of data shown.
- Join: Queries can combine data from multiple tables using JOIN operations.
- Aggregation: Queries can summarize data using functions like COUNT, SUM, AVG, etc.
Creating Views with SQL
- To create a view, use the CREATE VIEW statement:
CREATE VIEW HighPerformingStudents AS
SELECT FirstName, LastName
FROM Students
WHERE Grade > 90;
- This query creates a view of students with grades above 90.
- The result is a virtual table showing only the FirstName and LastName of high-performing students.
Benefits of Using Views
- Security : Views can restrict access to sensitive data by showing only specific columns or rows.
- Simplicity : Views can simplify complex queries by abstracting the logic into a single virtual table.
- Consistency : Views ensure that users see consistent data , even if the underlying tables change.