What Is a View in SQL? Tables vs Views with Examples

Quick Answer: A view in SQL is a virtual table created from a saved SELECT query stored in the database. It holds no data itself — it pulls data from one or more base tables each time you query it. You define it once with CREATE VIEW and query it exactly like a regular table.

Beyond that one-line definition, views solve real problems: they hide sensitive columns, simplify complex multi-table joins, and give teams a consistent, reusable interface to the data. Here is everything you need to know, with working SQL examples.

Key Takeaways

  • A view is a virtual table built from a SELECT statement; it stores the query definition, not the data.
  • Views simplify complex queries, restrict column-level access, and keep sensitive data hidden from certain users.
  • Unlike a base table, a view updates automatically when the underlying data changes.
  • A materialized view does store data physically and must be refreshed manually or on a schedule.
  • Most standard views in MySQL and PostgreSQL can be updated, but only under specific conditions.

Tables vs Views in SQL

Understanding what is view in SQL gets easier when you compare it directly to a table. A table is a physical object. It occupies storage, holds rows and columns, and persists until you drop it. A view is a logical object — the database stores only the SELECT query you defined; no extra disk space is consumed for the result set.

Here is a practical way to think about it. Imagine a college database at a university like IIT Delhi. There is a students table with columns for roll number, name, branch, CGPA, and Aadhaar number. The admin team needs CGPA reports, but they cannot see Aadhaar numbers. You create a SQL view that exposes only the columns they need. The underlying table stays untouched. This pattern is used daily across Indian IT companies, from Infosys and TCS to fintech startups in Bengaluru.

Side-by-Side Comparison

Feature Table View
Stores data physically Yes No (query definition only)
Occupies disk space Yes Minimal (definition only)
Auto-reflects base table changes N/A Yes
Can be indexed directly Yes No (except materialized views)
Supports INSERT/UPDATE/DELETE Yes (always) Conditionally
Used for access control Limited Yes, very effectively
Typical use case Primary data storage Reporting, security, simplification

According to the Stack Overflow Developer Survey 2024, MySQL and PostgreSQL are the two most popular databases among professional developers globally, with MySQL used by roughly 40.3% and PostgreSQL by 49.7% of respondents. Both support SQL views natively, making this a truly universal skill. According to DB-Engines Ranking (2024), relational database systems — all of which support views — account for 8 of the top 10 most widely used database engines worldwide, underscoring how foundational SQL view knowledge is for any data professional. According to the official PostgreSQL documentation, materialized views are particularly useful for expensive aggregations that do not need to reflect live data, making them a standard tool in analytical pipelines.

If you are preparing for interviews in data roles, views come up often. Check out these data analyst interview questions to see how SQL concepts like views are tested in real hiring rounds.

SQL View Example: Creating a View

The syntax is straightforward. You use CREATE VIEW followed by the view name, then AS, then your SELECT statement. The database stores that query and assigns it the name you gave. From that point on, you can query the view name directly.

Basic Syntax

Here is the standard pattern used in both MySQL and PostgreSQL:

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

Say you have an employees table with columns: emp_id, name, department, salary, and pan_number. You want the HR dashboard to show only names and departments. Here is the SQL view example:

CREATE VIEW hr_dashboard AS SELECT name, department FROM employees WHERE department = 'Engineering';

Now anyone with access to hr_dashboard sees only names and departments for Engineering staff. The salary and pan_number columns are completely invisible to them.

How to Create a View in SQL Across Multiple Tables

Views really shine when you need to join tables. Say you have an orders table and a customers table. Writing that join every time in a report is tedious and error-prone. You create the view once:

CREATE VIEW order_summary AS SELECT c.customer_name, o.order_id, o.total_amount FROM customers c JOIN orders o ON c.customer_id = o.customer_id;

Every analyst on your team now runs SELECT * FROM order_summary instead of rewriting that join. This reduces mistakes significantly, especially in teams where not everyone has deep SQL experience.

Replacing or Dropping a View

To update an existing view’s definition, use CREATE OR REPLACE VIEW. To remove it entirely, use DROP VIEW view_name. Dropping a view never affects the underlying tables. That is a critical point many beginners miss — they worry they will delete real data. You will not.

If you are studying for a BSc Computer Science degree or an MCA, SQL views are typically covered in your database management systems (DBMS) module. Understanding the why behind views, not just the syntax, is what separates strong candidates in placements.

Querying and Updating Views

Querying a view in SQL looks identical to querying a table. You use a plain SELECT statement:

SELECT * FROM hr_dashboard WHERE department = 'Engineering';

The database engine replaces your view reference with its underlying stored query at execution time. This is called query rewriting. You do not see it happen, but it is why views are sometimes slightly slower than direct table queries on very large datasets — the database has to resolve the view definition first.

How to View Table Data in SQL

If you want to inspect what is inside a base table directly, use SELECT * FROM table_name; or SELECT * FROM table_name LIMIT 10; to preview the first ten rows. To see all views defined in your current MySQL database, run SHOW FULL TABLES WHERE Table_type = 'VIEW';. In PostgreSQL, query SELECT viewname FROM pg_views WHERE schemaname = 'public';.

Can You Update a View in SQL?

Yes, but with conditions. A SQL view is updatable in MySQL and PostgreSQL only if it meets these rules: it references exactly one base table, and it does not use DISTINCT, aggregate functions, GROUP BY, HAVING, UNION, or subqueries in the SELECT list. If your view is simple enough, you can run UPDATE, INSERT, or DELETE against it and the changes propagate to the underlying table.

In practice, most production teams treat views as read-only. Updating through a view adds cognitive overhead and can produce unexpected results if the view has a WHERE clause that filters out the rows you just inserted.

Materialized Views

A materialized view stores the query result physically on disk. PostgreSQL supports them natively. MySQL does not have a native materialized view feature, though you can simulate one with a table and a scheduled job. You refresh a materialized view with REFRESH MATERIALIZED VIEW view_name; in PostgreSQL.

Materialized views are used when query performance matters more than real-time accuracy. A daily sales summary report is a classic example — compute it once at midnight, store it, and let hundreds of analysts query the stored result all day instead of hammering the transactions table.

Data engineers use materialized views constantly in analytical pipelines. If you are thinking about that career path, read our guide on how to become a data engineer to see where SQL fits into the bigger picture.

When to Use Views in SQL

SQL views solve three real problems: complexity, security, and consistency. Once you see how they are used on actual projects, you will stop thinking of them as a textbook concept and start reaching for them instinctively.

Access Control and Security

This is where views genuinely earn their place. Database administrators grant users SELECT privileges on a view rather than the underlying table. The user can only see what the view exposes. This is a core security pattern in any organisation handling sensitive data, whether it is a fintech startup in Bengaluru, a hospital system in Mumbai, or a government RDBMS storing citizen records.

If you are interested in how data security connects to broader cybersecurity skills, our CEH v13 AI online course covers access control, privilege escalation, and database attack vectors in detail.

Simplifying Complex Queries

A five-table join with three subqueries is a nightmare to maintain. Wrap it in a view, name it clearly (like monthly_revenue_by_region), and every team member can query it without understanding the internals. This is especially useful in data teams where data analysts and data scientists share the same database but have different SQL skill levels.

Logical Data Independence

When your underlying table structure changes, you can often update the view definition instead of rewriting every dependent query. Say you rename a column in a base table. Update the view to alias the new column name back to the old one, and no downstream report breaks. This kind of defensive design saves hours in production incidents.

The top data science tools companies use in production almost always sit on top of a well-designed SQL layer, and views are a core part of that layer. If you are coming from an MCA background and want to understand what salaries and roles look like in this space, our article on jobs and salary after MCA breaks that down clearly.

When Not to Use Views

Views are not always the right choice. Do not use a standard view for heavy aggregations that run thousands of times a day — the performance cost adds up. Do not chain views inside views (nesting views three or four levels deep is a known anti-pattern that makes debugging extremely painful). And do not use views as a substitute for proper table design; if your base schema is poorly normalised, a view just hides the problem.

For students building their SQL foundation as part of a broader data career, exploring SQL questions asked in data analyst interviews will show you exactly how views, joins, and subqueries are tested under pressure.

Frequently Asked Questions

What is a view in SQL?

A view in SQL is a virtual table created from a saved SELECT query stored in the database. It does not store data itself; it retrieves data from one or more base tables each time it is queried. You define it with CREATE VIEW and query it exactly like a table. Views are used to simplify queries, enforce security, and present a consistent interface to the data.

What is the difference between a table and a view in SQL?

A table physically stores data on disk and holds rows and columns permanently. A SQL view stores only a query definition and generates its result set dynamically from underlying tables. Tables consume storage proportional to their data; a standard view consumes almost none. The key practical difference is that a view can restrict which columns and rows a user sees, while a base table exposes everything to anyone with table-level access.

How do I view a table in SQL?

To see data inside a table, run SELECT * FROM table_name; or use SELECT * FROM table_name LIMIT 10; to preview the first ten rows. To list all views in a MySQL database, run SHOW FULL TABLES WHERE Table_type = 'VIEW';. In PostgreSQL, query SELECT viewname FROM pg_views WHERE schemaname = 'public'; to list views in the public schema.

Can I update a view in SQL?

Yes, but only if the view meets specific conditions: it must reference a single base table, and it cannot use DISTINCT, aggregate functions, GROUP BY, HAVING, UNION, or subqueries in its select list. When those conditions are met, an UPDATE or INSERT on the view writes through to the underlying table. Most production teams still treat views as read-only to avoid confusion around filtered rows not appearing after an insert.

What is a materialized view?

A materialized view stores the actual query result on disk, unlike a standard SQL view which recomputes on every query. PostgreSQL supports materialized views natively; you refresh them with REFRESH MATERIALIZED VIEW view_name;. They are ideal for expensive aggregations where real-time data is not needed, such as overnight sales summaries or weekly analytics dashboards. MySQL does not support them natively, but you can simulate them using a regular table populated by a scheduled job.

Are SQL views used in Indian IT companies?

Yes. SQL views are standard practice across Indian IT companies including TCS, Infosys, Wipro, and product startups in Bengaluru and Hyderabad. They are used in banking systems, healthcare databases, and e-commerce platforms to enforce data access policies and simplify reporting. NASSCOM consistently lists SQL as a core skill for data and backend roles in India’s technology sector.

Put your SQL knowledge to work. Views are one of those fundamentals that separate candidates who just know syntax from those who understand database design. Practice creating views on a real schema, test the access control use case, and try a materialized view in PostgreSQL. Here are three concrete next steps for this week:

  1. Set up a free PostgreSQL instance (ElephantSQL or Supabase’s free tier works), create a two-table schema, and build both a standard view and a materialized view to see the difference firsthand.
  2. Review the data analyst interview questions on 3.0 University to see how views, joins, and subqueries appear in real interviews at Indian companies.
  3. If you want to go deeper into how SQL security connects to ethical hacking and secure system design, explore 3.0 University’s CEH v13 AI online course or browse the full 3.0 University course catalogue for certifications in Cybersecurity, AI, Blockchain and Web3.

Last updated: June 2025. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles