Sobes.tech
Senior

Tell us about views in SQL and the two main types.

sobes.tech AI

Answer from AI

A view in SQL is a virtual table that is formed based on the result of a query. It does not physically store data but represents a saved query that can be used like a regular table.

The main two types of views:

  1. Simple view — created based on a single table, does not contain aggregate functions, subqueries, or joins. Such views are usually updatable.

  2. Complex view — includes joins, aggregate functions, subqueries, and can be based on multiple tables. Often, such views are not directly updatable.

Example of creating a simple view:

CREATE VIEW active_users AS
SELECT id, name, email FROM users WHERE status = 'active';

Using views simplifies the reuse of complex queries and improves code readability.