PostgreSQL Database Quiz Questions

Course Name:PostgreSQL
Chapter Name:Chapter 6 - DML (Data Manipulation Language)
Lesson Content Link:UPSERT
Current Quiz Count:30
Progress
0%
Q1
True / False

In PostgreSQL, the UPSERT feature allows you to either insert a new record or update an existing record if a conflict occurs.

Q2
True / False

The UPSERT operation is performed using the INSERT ON CONFLICT clause in PostgreSQL.

Q3
True / False

The ON CONFLICT clause can only be used with primary keys in PostgreSQL.

Q4
True / False

In PostgreSQL, you can use the ON CONFLICT clause to perform a conditional update based on the values of other columns.

Q5
True / False

PostgreSQL's UPSERT operation can only handle conflicts on a single column.

Q6
True / False

The DO NOTHING action in the ON CONFLICT clause will ignore the insert operation entirely if a conflict occurs in PostgreSQL.

Q7
True / False

In PostgreSQL, you can use the ON CONFLICT clause to perform complex updates involving multiple tables.

Q8
True / False

PostgreSQL's UPSERT mechanism guarantees that no other transactions will see an inconsistent state during the operation.

Q9
True / False

The ON CONFLICT clause can be combined with the RETURNING clause to retrieve the updated or inserted values in PostgreSQL.

Q10
True / False

PostgreSQL's UPSERT functionality can be used with the INSERT ... SELECT statement to handle conflicts arising from a bulk insert.

Q11
Single Choice

What does the UPSERT operation in PostgreSQL do?

Q12
Single Choice

Which PostgreSQL clause is used to perform an UPSERT operation?

Q13
Single Choice

What must be specified in a PostgreSQL UPSERT operation to handle conflicts?

Q14
Single Choice

Given the following SQL code, what will the UPSERT operation do?

SQL Code
INSERT INTO employees (id, name, salary)
VALUES (1, 'John Doe', 50000)
ON CONFLICT (id) DO UPDATE SET salary = EXCLUDED.salary + 1000;
Q15
Single Choice

In PostgreSQL, which keyword is used within an UPSERT operation to refer to the values that caused the conflict?

Q16
Single Choice

Which of the following PostgreSQL SQL code snippets correctly performs an UPSERT operation on a table named products?

SQL Code
INSERT INTO products (product_id, product_name, price)
VALUES (101, 'Laptop', 1500)
ON CONFLICT (product_id) DO UPDATE SET price = EXCLUDED.price;
Q17
Single Choice

Consider the following SQL code:

SQL Code
INSERT INTO orders (order_id, customer_id, order_date)
VALUES (10, 1001, '2024-08-18')
ON CONFLICT (order_id) WHERE customer_id = 1001
DO UPDATE SET order_date = EXCLUDED.order_date + INTERVAL '1 day';
Q18
Single Choice

How would you perform an UPSERT operation in PostgreSQL that only updates the record if a condition is met?

SQL Code
INSERT INTO inventory (item_id, stock)
VALUES (50, 200)
ON CONFLICT (item_id) DO UPDATE SET stock = EXCLUDED.stock WHERE inventory.stock < 100;
Q19
Single Choice

What does the following SQL code do in PostgreSQL?

SQL Code
INSERT INTO accounts (account_id, balance)
VALUES (1001, 5000)
ON CONFLICT (account_id) DO NOTHING;
Q20
Single Choice

Given the following table structure and SQL code, what will the outcome be?

SQL Code
CREATE TABLE vendors (
 vendor_id SERIAL PRIMARY KEY,
 vendor_name VARCHAR(100) UNIQUE,
 address VARCHAR(200)
);

INSERT INTO vendors (vendor_name, address)
VALUES ('Tech Corp', '123 Tech Street')
ON CONFLICT (vendor_name) DO UPDATE SET address = '456 Tech Avenue';
Q21
Multiple Choice

Which SQL command performs an UPSERT operation by inserting a new row or updating an existing one based on a conflict in PostgreSQL?

SQL Code
INSERT INTO users (user_id, username)
VALUES (1, 'jdoe')
ON CONFLICT (user_id)
DO UPDATE SET username = EXCLUDED.username;
Q22
Multiple Choice

Which SQL command performs an UPSERT operation with a condition in the DO UPDATE clause in PostgreSQL?

SQL Code
INSERT INTO products (product_id, price)
VALUES (101, 19.99)
ON CONFLICT (product_id)
DO UPDATE SET price = EXCLUDED.price
WHERE products.price < EXCLUDED.price;
Q23
Multiple Choice

Which SQL command performs an UPSERT operation using a composite key in PostgreSQL?

SQL Code
INSERT INTO order_items (order_id, product_id, quantity)
VALUES (1, 1001, 2)
ON CONFLICT (order_id, product_id)
DO UPDATE SET quantity = order_items.quantity + EXCLUDED.quantity;
Q24
Multiple Choice

Which SQL command performs an UPSERT operation with a RETURNING clause in PostgreSQL?

SQL Code
INSERT INTO customers (customer_id, email)
VALUES (42, 'john.doe@example.com')
ON CONFLICT (customer_id)
DO UPDATE SET email = EXCLUDED.email
RETURNING customer_id, email;
Q25
Multiple Choice

Which SQL command performs an UPSERT operation by inserting default values if the row doesn't exist in PostgreSQL?

SQL Code
INSERT INTO inventory (product_id, stock)
VALUES (100, 50)
ON CONFLICT (product_id)
DO UPDATE SET stock = inventory.stock + EXCLUDED.stock;
Q26
Multiple Choice

Which SQL command uses a WHERE clause in the UPSERT operation to conditionally update an existing row in PostgreSQL?

SQL Code
INSERT INTO employees (employee_id, salary)
VALUES (1, 50000)
ON CONFLICT (employee_id)
DO UPDATE SET salary = EXCLUDED.salary
WHERE employees.salary < EXCLUDED.salary;
Q27
Multiple Choice

Which SQL command uses an UPSERT operation to update multiple columns in PostgreSQL?

SQL Code
INSERT INTO users (user_id, username, email)
VALUES (1, 'jdoe', 'jdoe@example.com')
ON CONFLICT (user_id)
DO UPDATE SET username = EXCLUDED.username, email = EXCLUDED.email;
Q28
Multiple Choice

Which SQL command performs an UPSERT operation and logs the old values using the RETURNING clause in PostgreSQL?

SQL Code
INSERT INTO orders (order_id, total)
VALUES (10, 100)
ON CONFLICT (order_id)
DO UPDATE SET total = EXCLUDED.total
RETURNING order_id, old_total = orders.total;
Q29
Multiple Choice

Which SQL command performs an UPSERT operation with a condition on the conflict target in PostgreSQL?

SQL Code
INSERT INTO sales (sale_id, amount)
VALUES (101, 500)
ON CONFLICT (sale_id)
DO UPDATE SET amount = EXCLUDED.amount
WHERE sales.amount < EXCLUDED.amount;
Q30
Multiple Choice

Which SQL command performs an UPSERT operation using a subquery to determine the value to be inserted or updated in PostgreSQL?

SQL Code
INSERT INTO inventory (product_id, stock)
VALUES (100, (SELECT MAX(stock) FROM inventory) + 10)
ON CONFLICT (product_id)
DO UPDATE SET stock = inventory.stock + EXCLUDED.stock;