EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Views

PostgreSQL Views

Updated May 3, 2023

PostgreSQL Views

Introduction to PostgreSQL Views

In these articles, we will learn about PostgreSQL Views. Views are basically a query with a name; therefore, a view is useful for wrapping a commonly used complex query; we can represent data in the database tables using views named query, we can define views using one or more tables known as base tables, the view is a logical table which represents data from one or many tables using the SELECT command.

ADVERTISEMENT
Popular Course in this category
POSTGRESQL Course Bundle - 5 Courses in 1 | 1 Mock Test

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The query’s complexity can be simplified with the help of view, as users can query a view using a SELECT command. Like a table, we can add permission to the users using a view that stores data for which we need authorized users only.

How to Create PostgreSQL Views?

Let us see how to create the views:

Syntax:

CREATE [OR REPLACE][TEMP OR TEMPORARY] [RECURSIVE]
VIEW view_name [(column_name [, ...])]
[ WITH (view_options_name [= view_options_value] [, ... ])]

Explanation: If a view with the same name already exists, it is replaced. The view name must be unique. It should not be the same as any other view, sequence, table, foreign table, or index in the same schema.

  • TEMP / TEMPORARY: If the view is created as a temporary view, it is automatically removed at the end of the session.
  • RECURSIVE: Creates a recursive view.
  • Name: The name of a view to be
  • column_name: The user can define a list of column names of the view. Only defined columns will get considered in the query others are
  • WITH: ( view_options_name [= view_options_value]…)We can specify optional parameters for a view.

Creating PostgreSQL Views

We can create PostgreSQL views using various ways:

Consider the following tables to understand the PostgreSQL views:

To understand the examples of considering the following ‘student’ and ‘Branch’ table structures

  • Student: rollno, firstname, lastname, branch_id, result, joining_date
  • Branch: branch_id, branch

1. WHERE clause

Code:

CREATE VIEW student_view
AS SELECT rollno, firstname, lastname, result, joining_date
FROM student
WHERE branch_id = 4;

It will create a view’ student _view’ taking records (for rollno, firstname, lastname, result, joining_date columns) of the student table if those records contain the value 4 for the branch_id column.

Code:

select * from student_view

Output:

PostgreSQL Views - 1

2. AND and OR

Code:

CREATE VIEW my_student_view
AS SELECT *
FROM student
WHERE(branch_id = 1 AND result = false)
OR(branch_id = 2 AND firstname='Jacob');
  • It will create a view ‘my_student_view’ taking records for all student table columns
  • if branch_id is 1 and the result is false.
  • Or branch_id is 2, and the firstname is ‘Jacob’.

Code:

select * from student_view

Output:

PostgreSQL Views - 2

3. GROUP BY

Code:

CREATE VIEW my_student_view
AS SELECT branch_id, count (*)
FROM student
GROUP BY branch_id;

It will create a view ‘my_student_view’ taking all records grouped with respect to branch_id and stored branch_id and several students for each Branch (branch_id) from the student table.

Code:

select * from student_view

Output:

PostgreSQL Views - 3

4. ORDER BY

Code:

CREATE VIEW my_student_view
AS SELECT branch_id, count (*)
FROM student
GROUP BY branch_id
order by branch_id;

It will create a view of my_student_view, taking all the records grouped with respect to branch_id and sorted against branch_id and the number of student tables for each department (branch_id) from the student table.

Code:

select * from student_view

Output:

PostgreSQL Views - 4

5. BETWEEN and IN

Code:

CREATE VIEW my_student_view AS
SELECT * FROM student
WHERE firstname BETWEEN 'G' AND 'K'
AND rollno IN (101,102,103,105);

It will create a view ‘my_student_view’ taking all the records of the employee’s table; if the firstname column of the student starts with any of the characters from ‘G’ through ‘K’ and rollno are any of the following 101,102,103,105

Code:

select * from student_view

Output:

PostgreSQL Views - 5

6. LIKE

Code:

CREATE VIEW my_student_view
AS SELECT *
FROM student
WHERE firstname NOT LIKE 'J%' AND lastname NOT LIKE
'C%';

It will create a view my_student_view taking all the records of the student table. If the student’s firstname does not start with ‘J’ and lastname of the student does not start with ‘C’.

Code:

select * from student_view

Output:

 LIKE

7. Subqueries

Code:

CREATE VIEW my_student_view
AS SELECT rollno,firstname,lastname
FROM student
WHERE branch_id IN(
SELECT branch_id
FROM Branch
WHERE branch_id IN (1,2)
);

It will create a view my_student_view taking all the records of rollno, firstname, and lastname of the student table. The subquery retrieves those branch_ids from the Branch table, which branch_id are any of the 1 and 2.

Code:

select * from student_view

Output:

Subqueries

8. JOIN

Code:

CREATE VIEW my_student_view
AS SELECT s.rollno, s.firstname, s.lastname, b.branch
FROM student s, branch b
WHERE s.branch_id = b.branch_id;

It will create a view of my_student_view along with a JOIN statement. The JOIN statement here retrieves rollno, firstname, lastname, from student table and branch_id from the branch table if the branch_id of the student table and that of the Branch are the same.

Code:

select * from student_view

Output:

JOIN

How to Modify PostgreSQL Views?

ALTER VIEW statement modifies the definition of an existing view.

1. ALTER with SET DEFAULT

ALTER VIEW [ IF EXISTS] views_name ALTER [ COLUMN ] column_name SET DEFAULT expression

2. ALTER with DROP

ALTER VIEW [ IF EXISTS] views_name ALTER [ COLUMN ] column_name DROP DEFAULT

3. ALTER with OWNER

ALTER VIEW [ IF EXISTS] views_name OWNER TO new_view_owner

4. ALTER with RENAME

ALTER VIEW [ IF EXISTS] views_name RENAME TO new_view_name

5. ALTER with SET SCHEMA

ALTER VIEW [ IF EXISTS] views_name SET SCHEMA new_view_schema

6. ALTER with SET

ALTER VIEW [ IF EXISTS] views_name SET ( view_options_name [= view_options_value] ... )

7. ALTER with RESET

ALTER VIEW [ IF EXISTS ] views_name RESET ( view_options_name ... )

Explanation:

  • views_name: The name of the
  • IF EXISTS: Do not throw an error if the view does not exist.
  • SET/DROP DEFAULT: Set or remove the default value for a column.
  • new_view_owner: Defines view’s new owner’s username.
  • new_view_name: The new name for the view.
  • new_view_schema: The new schema for the view.
  • view_options_name : Defines view options to be set/
  • view_options_value : Defines the new value for associated view options.

To rename the view ABC to XYZ.

ALTER VIEW test_view RENAME TO testview;

How to Drop PostgreSQL Views?

  • To remove a view, we must use the DROP VIEW statement.
  • Users must have the DROP privilege for each view to drop a view.

Syntax:

DROP VIEW [IF EXISTS]
view_name [, view_name] ...
[ CASCADE | RESTRICT]

Explanation:

  • IF EXISTS: Do not throw an error if the view does not exist.
  • view_name: The name (optionally schema-qualified) of the view to remove.
  • CASCADE: It will automatically remove all view-dependent objects.
  • RESTRICT: It will not allow you to delete the view if any dependent object exists.

The following command will remove the view called ‘postgresql_view’:

DROP VIEW 'postgresql_view';

Conclusion

We hope you have understood the concept of PostgreSQL views. And also learned about how to create, modify, and remove PostgreSQL views.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Views” benefited you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Data Types
  2. PostgreSQL WHERE Clause
  3. Relational Database
  4. What is PostgreSQL?
ADVERTISEMENT
PROGRAMMING LANGUAGES Course Bundle - 54 Courses in 1 | 4 Mock Tests
338+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SELENIUM Course Bundle - 15 Courses in 1 | 9 Mock Tests
39+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
IOT System Course Bundle - 7 Courses in 1
43+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
JENKINS Course Bundle - 6 Courses in 1
15+ Hour of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW