TGViewer
Data Analytics Data Analytics @sqlspecialist · 111K subscribers
Post #2763 7.52K
Now, let’s move to the next topic:

Views (Virtual Tables)

🧠 1. What is a VIEW?

A VIEW is a virtual table based on a SQL query

👉 It does NOT store data
👉 It stores the query

Think like this 👇
👉 “Saved SQL query → reuse anytime”

⚡ 2. Why Use Views?

- Simplify complex queries
- Reuse logic
- Hide sensitive data
- Improve readability

⚡ 3. Create a VIEW

CREATE VIEW high_salary_emp AS
SELECT name, salary
FROM employees
WHERE salary > 50000;

🔍 4. Use a VIEW

SELECT FROM high_salary_emp;

✔ Works like a normal table

🔄 5. Update a VIEW

CREATE OR REPLACE VIEW high_salary_emp AS
SELECT name, salary, department
FROM employees
WHERE salary > 50000;

❌ 6. Drop a VIEW

DROP VIEW high_salary_emp;

🎯 7. Real Example

👉 Create view for department-wise average salary

CREATE VIEW dept_avg_salary AS
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

👉 Use it:
SELECT FROM dept_avg_salary;

⚡ 8. Important Points

- View does NOT store data
- Changes in table → reflect in view
- Can be used like a table

🎯 9. Practice Tasks

1. Create view for employees with salary > 40k
2. Create view for IT department employees
3. Create view for avg salary per department
4. Query data using created views
5. Drop a view

🔥 Here are the solutions for VIEW practice tasks

✅ 1. Create view for employees with salary > 40k
CREATE VIEW high_salary_emp AS
SELECT
FROM employees
WHERE salary > 40000;

✅ 2. Create view for IT department employees
CREATE VIEW it_employees AS
SELECT
FROM employees
WHERE department = 'IT';

✅ 3. Create view for avg salary per department
CREATE VIEW dept_avg_salary AS
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

✅ 4. Query data using created views
SELECT FROM high_salary_emp;

SELECT FROM it_employees;

SELECT FROM dept_avg_salary;

✅ 5. Drop a view
DROP VIEW high_salary_emp;

⚡ Mini Challenge 🔥

👉 Create a view to show top 3 highest salary employees

⚡ Mini Challenge Solution

CREATE VIEW top_3_salary AS
SELECT
FROM employees
ORDER BY salary DESC
LIMIT 3;

👉 Use the view:

SELECT FROM top_3_salary;

🔥 Pro Tip:
Views are heavily used in:
👉 Dashboards
👉 Reporting systems
👉 Data analytics projects

Because they simplify complex SQL 💯

👉 Table → stores data
👉 View → stores query

Double Tap ❤️ For More
  • ❤ 17
More from @sqlspecialist
  1. Oct 9, 2026“Here, the data is sorted by the second column in descending order and the first five rows…
  2. Oct 9, 2026📊 Data Analyst Interview Series — Part 6 Guys, let's continue our Data Analyst Interview…
  3. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  4. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  5. Oct 7, 2026📊 Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vid…
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions such…
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 →