#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
Step 12
6 min read

Creating Views

Learn what Views are - like saved queries you can reuse!

What is a View?

A View is like a saved query. Instead of writing the same long query again and again, you save it as a View and use it like a table.

Simple analogy: Like a contact shortcut on your phone - instead of typing the full number every time, you just click the contact name.

Why Use Views?

Problem: You keep writing this long query:

SELECT name, email FROM users WHERE is_active = TRUE AND age >= 18;

Solution: Save it as a View once, use it forever:

CREATE VIEW active_adults AS SELECT name, email FROM users WHERE is_active = TRUE AND age >= 18;

Now just use:

SELECT * FROM active_adults;

Much easier!

How to Create a View

Example 1: Active Students

CREATE VIEW active_students AS SELECT student_id, name, grade FROM students WHERE is_active = TRUE;

Now you can query it like a table:

SELECT * FROM active_students;

Example 2: Product Summary

CREATE VIEW product_summary AS SELECT product_name, price, stock, price * stock AS inventory_value FROM products WHERE stock > 0;

Use it:

SELECT * FROM product_summary WHERE price > 100;

Example 3: Customer Orders

CREATE VIEW customer_orders AS SELECT c.name, o.order_id, o.total, o.order_date FROM customers c JOIN orders o ON c.customer_id = o.customer_id;

Real-Life Benefits

Without View (write every time):

SELECT customers.name, orders.total FROM customers JOIN orders ON customers.id = orders.customer_id WHERE orders.status = 'completed';

With View (write once, use many times):

CREATE VIEW completed_orders AS SELECT customers.name, orders.total FROM customers JOIN orders ON customers.id = orders.customer_id WHERE orders.status = 'completed'; -- Now just use: SELECT * FROM completed_orders;

Summary

View = Saved query

  • Write complex query once
  • Save it as a View
  • Use it like a table
  • Makes life easier

Finished this topic?

Mark it complete to track your progress and maintain your streak!