TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2501 2.8K
๐Ÿš€ Now, Letโ€™s move to the next topic:

โœ… SQL Views vs Materialized Views

๐Ÿง  1. What is a View?

A View is a virtual table created from a query.

๐Ÿ‘‰ Stores only the SQL query

๐Ÿ‘‰ Does NOT store actual data

๐Ÿ‘‰ Always shows latest data from underlying tables

CREATE VIEW employee_view AS

SELECT * FROM employees;

๐Ÿง  2. What is a Materialized View?

A Materialized View stores the query result physically.

๐Ÿ‘‰ Stores actual data

๐Ÿ‘‰ Faster for reporting queries

๐Ÿ‘‰ Needs refresh to get latest data

CREATE MATERIALIZED VIEW employee_summary AS

SELECT department,

AVG(salary) AS avg_salary

FROM employees

GROUP BY department;

โšก 3. View vs Materialized View

Feature : View : Materialized View

Stores Data : โŒ No : โœ… Yes

Storage Space : Very Low : Higher

Query Speed : Slower : Faster

Real-Time Data : โœ… Yes : โŒ Needs Refresh

Best For : OLTP Systems : Reporting & Analytics

๐Ÿ”ฅ 4. Example

View

SELECT * FROM employee_view;

Every execution runs the underlying query again.

Materialized View

SELECT * FROM employee_summary;

Reads precomputed data directly.

๐Ÿ”„ 5. Refresh Materialized View

REFRESH MATERIALIZED VIEW employee_summary;

Updates stored results with latest data.

๐ŸŽฏ 6. Real-World Usage

Views Used In:

โœ” Banking Applications

โœ” HR Systems

โœ” Transaction Systems

Materialized Views Used In:

โœ” BI Dashboards

โœ” Data Warehouses

โœ” Reporting Systems

๐ŸŽฏ 7. Practice Tasks

1. Create a view for high-salary employees

2. Query data from a view

3. Create department salary summary view

4. Create materialized view for sales summary

5. Refresh materialized view

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Create a materialized view showing:

โ€ข department

โ€ข total employees

โ€ข average salary

Then query it to find the department with the highest average salary.

Most asked interview question:

๐Ÿ‘‰ When would you choose a Materialized View over a View?

โœ… Answer: "When query execution is expensive and data changes less frequently, Materialized Views improve performance significantly."

Double Tap โค๏ธ For More
  • โค 8
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’