TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2801 1.25K
๐Ÿš€ SQL Roadmap 2026 โ€” Part 17

SQL Views โ€” Creating Reusable Virtual Tables

A SQL View is a saved SQL query that behaves like a virtual table.

Instead of writing the same complex query repeatedly, you can create a View once and query it like a table.

This is especially useful in:

โ€ข Data analytics

โ€ข Reporting

โ€ข Business Intelligence

โ€ข Power BI datasets

โ€ข Data warehouses

โ€ข Reusable business logic

โ€ข Data security

1๏ธโƒฃ What Is a View?

Suppose you frequently need this query:

SELECT
customer_id,
customer_name,
country,
total_sales
FROM customers
WHERE country = 'India'
AND total_sales > 100000;


Instead of writing it every time, you can create a View:

CREATE VIEW high_value_indian_customers AS
SELECT
customer_id,
customer_name,
country,
total_sales
FROM customers
WHERE country = 'India'
AND total_sales > 100000;


Now you can simply write:

SELECT *
FROM high_value_indian_customers;


The View behaves like a table from the user's perspective.

2๏ธโƒฃ Why Do We Need Views?

Imagine your reporting query contains:

โ€ข 4 JOINs

โ€ข 3 CASE statements

โ€ข Multiple filters

โ€ข Several calculated columns

โ€ข Aggregations

You don't want every analyst or report to rewrite that logic.

A View allows you to centralize the logic.

3๏ธโƒฃ Basic CREATE VIEW Syntax

The general syntax is:

CREATE VIEW view_name AS
SELECT
column1,
column2,
column3
FROM table_name
WHERE condition;


Example:

CREATE VIEW active_customers AS
SELECT
customer_id,
customer_name,
country
FROM customers
WHERE status = 'Active';


Query the View:

SELECT *
FROM active_customers;


4๏ธโƒฃ A View Can Contain JOINs

Views aren't limited to simple SELECT statements.

For example:

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


Now:

SELECT *
FROM customer_orders;


returns the result of that JOIN.

You don't need to repeatedly write the JOIN.

5๏ธโƒฃ Views with Aggregation

You can also create aggregated Views.

Suppose management regularly needs regional sales:

CREATE VIEW regional_sales AS
SELECT
region,
SUM(order_amount) AS total_sales,
COUNT(*) AS total_orders
FROM orders
GROUP BY region;


Now:

SELECT *
FROM regional_sales;


Example result:

region     total_sales     total_orders
North 450000 1250
South 620000 1580
West 510000 1390
East 380000 980


You can further analyze the View:

SELECT *
FROM regional_sales
WHERE total_sales > 500000;


6๏ธโƒฃ Views with CASE Statements

Views are useful for standardizing business logic.

For example:

CREATE VIEW customer_segments AS
SELECT
customer_id,
customer_name,
total_sales,
CASE
WHEN total_sales >= 100000 THEN 'High Value'
WHEN total_sales >= 50000 THEN 'Medium Value'
ELSE 'Low Value'
END AS customer_segment
FROM customers;


Now every report can use the same segmentation logic:

SELECT
customer_segment,
COUNT(*) AS customers
FROM customer_segments
GROUP BY customer_segment;
  • โค 1
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
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 โ†’