PostgreSQL Database Quiz Questions

Course Name:PostgreSQL
Chapter Name:Chapter 9 - Advanced Topics
Lesson Content Link:Database View
Current Quiz Count:30
Progress
0%
Q1
True / False

A view in PostgreSQL is a stored query that you can reference like a table.

Q2
True / False

You can update the data in a view in PostgreSQL.

Q3
True / False

PostgreSQL views support triggers like tables do.

Q4
True / False

In PostgreSQL, you can create a view with a WITH CHECK OPTION clause to ensure the view only allows rows that conform to its WHERE clause.

Q5
True / False

A materialized view in PostgreSQL is updated automatically whenever the underlying data changes.

Q6
True / False

PostgreSQL views can include window functions in their definitions.

Q7
True / False

In PostgreSQL, you can define indexes on views to improve query performance.

Q8
True / False

PostgreSQL supports the creation of recursive views using the WITH RECURSIVE clause.

Q9
True / False

You can use the ORACLE-specific hint in PostgreSQL views to optimize query performance.

Q10
True / False

When using PostgreSQL views in a data migration project from ORACLE, all view definitions can be directly transferred without any modification.

Q11
Single Choice

What is a PostgreSQL view?

Q12
Single Choice

How do you create a view in PostgreSQL?

Q13
Single Choice

Can a PostgreSQL view be updated?

Q14
Single Choice

Which of the following SQL commands would create a view that only shows active employees from the employees table in PostgreSQL?

Q15
Single Choice

How can you delete a view named sales_view in PostgreSQL?

Q16
Single Choice

What happens if you try to create a view with the same name as an existing view in PostgreSQL?

Q17
Single Choice

Consider the following SQL code. What will be the result when the view employee_dept is queried?

SQL Code
CREATE VIEW employee_dept AS
SELECT e.name, d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;
Q18
Single Choice

How can you update a view to reflect changes made to its underlying tables in PostgreSQL?

Q19
Single Choice

Which of the following options ensures that the view employee_details is updatable in PostgreSQL?

Q20
Single Choice

Given the following SQL code, which modification will make the view sales_summary non-updatable in PostgreSQL?

SQL Code
CREATE VIEW sales_summary AS
SELECT salesperson_id, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY salesperson_id;
Q21
Multiple Choice

What is a database view in PostgreSQL?

SQL Code
Create a view named 'active_customers' that selects all customers with a status of 'active'. Write the SQL script for this view in PostgreSQL.

```sql
CREATE VIEW active_customers AS
SELECT customer_id, customer_name
FROM customers
WHERE status = 'active';
```
Q22
Multiple Choice

How can you update data through a view in PostgreSQL?

SQL Code
CREATE VIEW order_totals AS
SELECT customer_id, SUM(order_amount) AS total_amount
FROM orders
GROUP BY customer_id;

UPDATE order_totals
SET total_amount = 500
WHERE customer_id = 1;
Q23
Multiple Choice

What are the benefits of using database views?

SQL Code
Create a view named 'employee_salaries' that selects employee names and their corresponding salaries, but only for those earning more than $50,000 per year. Write the SQL script for this view in PostgreSQL.

```sql
CREATE VIEW employee_salaries AS
SELECT employee_name, salary
FROM employees
WHERE salary > 50000;
```
Q24
Multiple Choice

Can you create a view that includes data from multiple tables?

SQL Code
Create a view named 'customer_orders' that includes customer names and their corresponding order dates by joining the 'customers' and 'orders' tables. Write the SQL script for this view in PostgreSQL.

```sql
CREATE VIEW customer_orders AS
SELECT c.customer_name, o.order_date
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id;
```
Q25
Multiple Choice

How can you create a view that includes an aggregate function?

SQL Code
CREATE VIEW average_order_amount AS
SELECT customer_id, AVG(order_amount) AS avg_amount
FROM orders
GROUP BY customer_id;
Q26
Multiple Choice

What is the difference between a view and a materialized view in PostgreSQL?

SQL Code
Create a materialized view named 'monthly_sales' that shows the total sales for each month. Write the SQL script to create and refresh this materialized view in PostgreSQL.

```sql
CREATE MATERIALIZED VIEW monthly_sales AS
SELECT DATE_TRUNC('month', sale_date) AS month, SUM(sale_amount) AS total_sales
FROM sales
GROUP BY month;

REFRESH MATERIALIZED VIEW monthly_sales;
```
Q27
Multiple Choice

What is the purpose of the WITH CHECK OPTION clause in view creation?

SQL Code
Create a view named 'active_projects' that selects all projects with a status of 'active' and includes a WITH CHECK OPTION clause. Write the SQL script for this view in PostgreSQL.

```sql
CREATE VIEW active_projects AS
SELECT project_id, project_name, status
FROM projects
WHERE status = 'active'
WITH CHECK OPTION;
```
Q28
Multiple Choice

Can you create an indexed view in PostgreSQL?

SQL Code
CREATE MATERIALIZED VIEW customer_order_totals AS
SELECT customer_id, SUM(order_amount) AS total_amount
FROM orders
GROUP BY customer_id;
CREATE INDEX idx_customer_order_totals ON customer_order_totals (customer_id);
Q29
Multiple Choice

How do you refresh a materialized view in PostgreSQL?

SQL Code
Write a SQL script to refresh a materialized view named 'yearly_sales' in PostgreSQL.

```sql
REFRESH MATERIALIZED VIEW yearly_sales;
```
Q30
Multiple Choice

How can you drop a view in PostgreSQL?

SQL Code
Write a SQL script to drop a view named 'inactive_customers' in PostgreSQL.

```sql
DROP VIEW IF EXISTS inactive_customers;
```