A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

SQL Views and Materialized Views

College Depth 72 in the knowledge graph I know this Set as goal
5topics build on this
333prerequisites beneath it
See this on the map →
SQL: SELECT Statement and Basic QueriesSQL JoinsDatabase SecurityQuery Optimization
SQL views materialized views virtual tables abstraction

Core Idea

A view is a named, stored SQL query that appears to users as a virtual table — querying a view executes the underlying SELECT each time without storing data. Views simplify complex queries, enforce security by exposing only certain columns or rows, and provide a stable interface when the underlying schema changes. Materialized views physically store the query result and must be refreshed periodically; they trade freshness for dramatically faster reads on expensive aggregations or joins.

How It's Best Learned

Create a view over a multi-table join, then query it as if it were a plain table. Experiment with updatable vs. non-updatable views. Compare query execution time between a complex query run directly vs. through a materialized view with pre-computed aggregates.

Common Misconceptions

Explainer

You know how to write SELECT queries and join tables together. A view is simply a saved SELECT query that you can reference by name as though it were a table. When you write `CREATE VIEW active_customers AS SELECT id, name, email FROM customers WHERE status = 'active'`, you are not creating a new table or copying any data. You are storing the query text under the name `active_customers`. Every time someone writes `SELECT * FROM active_customers`, the database substitutes in the underlying query and executes it fresh against the current data.

This indirection provides three practical benefits. First, simplification: a complex multi-table join with filters can be wrapped in a view, and downstream users query a single "table" without needing to understand the join logic. Second, security: by granting access to a view instead of the underlying tables, you can expose only certain columns or rows. A view on the employees table that excludes salary and SSN columns lets HR assistants look up contact information without seeing compensation data. Third, schema stability: if the underlying table structure changes (a column is renamed, a table is split), you can update the view definition while keeping the view's interface unchanged — queries that depend on the view continue to work.

Materialized views are a fundamentally different tool despite the similar name. A regular view stores a query; a materialized view stores the *result* of a query. When you create a materialized view, the database executes the query and writes the output to disk, just like a table. Subsequent reads hit this pre-computed result instead of re-executing the query. This is enormously valuable for expensive aggregations — if a dashboard query joins five tables and computes monthly revenue breakdowns, a materialized view can serve that result in milliseconds. The tradeoff is staleness: the materialized view reflects the data at the time it was last refreshed, not necessarily the current state. You must explicitly refresh it (manually or on a schedule) to pick up changes.

Not all views are updatable. If you INSERT, UPDATE, or DELETE through a view, the database must be able to map those changes back to the underlying table unambiguously. Simple views over a single table with no aggregation are typically updatable. But views involving GROUP BY, DISTINCT, joins across multiple tables, or computed columns are generally read-only — the database cannot determine which base row to modify. When designing views, decide upfront whether the view is for reading only or whether it must support writes, because this constrains how you can define it.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesSet Operations: Union, Intersection, and ComplementRelational AlgebraSQL: SELECT Statement and Basic QueriesSQL JoinsSQL Views and Materialized Views

Longest path: 73 steps · 333 total prerequisite topics

Prerequisites (2)

Leads To (2)