---
title: "Creating a View in DB2: Step-by-Step Guide"
description: "Here are examples of how to create Views in DB2 SQL. Also explained rules and purpose of views."
url: https://srinimf.com/2023/11/02/how-to-create-a-view-in-db2-an-example-sql/
date: 2023-11-02
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2016/10/pexels-photo-940881.jpeg?fit=1880%2C1253&ssl=1
categories: ["DB2"]
tags: ["SQL tips and tricks", "SQL Views"]
type: post
lang: en
---

# Creating a View in DB2: Step-by-Step Guide

Views are virtual tables that you can create based on a physical table. If there are no original tables, then the concept of views does not exist.

## SQL: Rules to create a View

- The error occurs when the physical table does not exist in the database.
- You cannot delete records in the view as it is a logical representation.

## SQL View Types

SQL views are virtual tables that are derived from the query result of a SELECT statement. They provide a way to represent data from one or more tables, or even other views, in a structured manner. Views in SQL come in different types, including read-only views, updatable views, and force views.

1. SQL Read-only views:
   - Read-only views are views that allow users to retrieve data but not perform any modifications.
   - These views are particularly useful when you want to restrict access to certain columns or rows of a table.
   - By defining read-only views, you can provide a simplified and controlled interface for end-users or applications.
2. SQL Updatable views:
   - Updatable views allow not only retrieving data but also modifying and inserting data into the underlying tables.
   - With updatable views, you can abstract complex join operations or calculations, making it easier to update or insert data using a more straightforward view structure.
3. SQL Force views:
   - Force views, also known as materialized views or indexed views, are special types of views that store the results of the underlying query on disk.
   - These views are precomputed and stored, allowing for faster data retrieval in certain cases where there are complex calculations or aggregations involved.
   - Force views are especially helpful when dealing with large datasets or frequently executed queries, as they can significantly improve query performance.

![Types of SQL Views](https://i0.wp.com/srinimf.com/wp-content/uploads/2020/04/sql-views.png?resize=351%2C281&ssl=1)

## SQL Views: Purpose

- Control who can access the data in a table.
- Make the data more user-friendly
- Easily grant access to only specific views instead of the entire table
- Display only certain parts of the table data
- Show a summary of a table
- Combine two or more [tables in](https://srinimf.com/2023/07/24/how-to-retrieve-two-tables-data-using-db2-join/) a meaningful manner
- Display only the relevant rows that are important for a particular process

Read: [Instant SQL format Tool for Your Projects](http://www.dpriver.com/pp/sqlformat.htm)

## Examples to Create a View

This section provides examples to create a view in your application.

**Example – 1**

```
CREATE VIEW ma_proj AS
SELECT *
FROM   project
WHERE  substr(projno, 1, 2) = 'MA'
```

**Example -2**

```
CREATE VIEW ma_proj AS selectprojno,
projname, respemp
FROM        project
WHERE       substr (projno,1, 2) = 'MA'
```

Related Posts

- [8 Top Rules to Write Updatable-view](https://srinimf.com/2015/11/22/2-types-of-view-updatable-and-read-only-advanced-sql/)DateNovember 22, 2015
- [SQL- How to Create Updatable and Read-only Views](https://srinimf.com/2016/08/28/avoid-updatable-views-in-db2-with-fantastic-sql/)DateAugust 28, 2016
- [DB2 Questions on Primary Key, View , Static SQL and Dynamic SQL](https://srinimf.com/2015/08/18/db2-questions-on-primary-key-view-static-sql-and-dynamic-sql/)DateAugust 18, 2015

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F11%2F02%2Fhow-to-create-a-view-in-db2-an-example-sql%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2016%2F10%2Fpexels-photo-940881.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Creating%20a%20View%20in%20DB2%3A%20Step-by-Step%20Guide)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/11/02/how-to-create-a-view-in-db2-an-example-sql/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/11/02/how-to-create-a-view-in-db2-an-example-sql/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/11/02/how-to-create-a-view-in-db2-an-example-sql/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/11/02/how-to-create-a-view-in-db2-an-example-sql/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Creating%20a%20View%20in%20DB2%3A%20Step-by-Step%20Guide&body=https%3A%2F%2Fsrinimf.com%2F2023%2F11%2F02%2Fhow-to-create-a-view-in-db2-an-example-sql%2F&share=email)
-

### Like this:

Like Loading…
