Q1
True / FalseIn PostgreSQL, the UPSERT feature allows you to either insert a new record or update an existing record if a conflict occurs.
The UPSERT feature in PostgreSQL provides a way to either insert a new record or update an existing one if there is a conflict on a unique constraint or primary key.
Q2
True / FalseThe UPSERT operation is performed using the INSERT ON CONFLICT clause in PostgreSQL.
PostgreSQL uses the INSERT ON CONFLICT clause to handle conflicts that arise during an insert operation, enabling the UPSERT functionality.
Q3
True / FalseThe ON CONFLICT clause can only be used with primary keys in PostgreSQL.
The ON CONFLICT clause in PostgreSQL can be used with any unique constraint, not just primary keys.
Q4
True / FalseIn PostgreSQL, you can use the ON CONFLICT clause to perform a conditional update based on the values of other columns.
PostgreSQL allows conditional updates in the ON CONFLICT clause using the WHERE condition to specify criteria for updating the record.
Q5
True / FalsePostgreSQL's UPSERT operation can only handle conflicts on a single column.
PostgreSQL's ON CONFLICT clause can handle conflicts on multiple columns by specifying a unique constraint that involves multiple columns.
Q6
True / FalseThe DO NOTHING action in the ON CONFLICT clause will ignore the insert operation entirely if a conflict occurs in PostgreSQL.
The DO NOTHING action instructs PostgreSQL to ignore the insert operation if a conflict occurs, effectively preventing any changes.
Q7
True / FalseIn PostgreSQL, you can use the ON CONFLICT clause to perform complex updates involving multiple tables.
While the ON CONFLICT clause itself is used within a single table, the accompanying UPDATE statement can involve subqueries and references to other tables for complex update operations.
Q8
True / FalsePostgreSQL's UPSERT mechanism guarantees that no other transactions will see an inconsistent state during the operation.
PostgreSQL's transactional model ensures that the UPSERT operation is atomic, meaning other transactions will not see an inconsistent state during the operation.
Q9
True / FalseThe ON CONFLICT clause can be combined with the RETURNING clause to retrieve the updated or inserted values in PostgreSQL.
PostgreSQL allows the use of the RETURNING clause with ON CONFLICT to return the updated or inserted values as part of the UPSERT operation.
Q10
True / FalsePostgreSQL's UPSERT functionality can be used with the INSERT ... SELECT statement to handle conflicts arising from a bulk insert.
PostgreSQL supports the use of the INSERT ... SELECT statement with the ON CONFLICT clause to handle conflicts during bulk insert operations, providing flexibility in handling large data sets.
Q21
Multiple ChoiceWhich 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;
This command tries to insert a new row into the 'users' table. If the 'user_id' already exists, it updates the 'username' instead of inserting a new row.
Q22
Multiple ChoiceWhich 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;
This command inserts a new row into the 'products' table or updates the 'price' of an existing row only if the new price is higher than the current price.
Q23
Multiple ChoiceWhich 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;
This command tries to insert a new row into the 'order_items' table. If a row with the same 'order_id' and 'product_id' exists, it updates the 'quantity' by adding the existing and new quantities.
Q24
Multiple ChoiceWhich 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;
This command performs an UPSERT on the 'customers' table and returns the 'customer_id' and 'email' of the inserted or updated row.
Q25
Multiple ChoiceWhich 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;
This command inserts a new row into the 'inventory' table or updates the 'stock' of an existing row by adding the new stock to the current stock.
Q26
Multiple ChoiceWhich 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;
This command inserts a new row or updates the 'salary' of an existing row in the 'employees' table, but only if the new salary is higher than the current one.
Q27
Multiple ChoiceWhich 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;
This command performs an UPSERT on the 'users' table, updating both the 'username' and 'email' if a conflict occurs on 'user_id'.
Q28
Multiple ChoiceWhich 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;
This command performs an UPSERT on the 'orders' table and returns the 'order_id' and old 'total' values if an update occurs.
Q29
Multiple ChoiceWhich 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;
This command inserts a new row into the 'sales' table or updates the 'amount' if a row with the same 'sale_id' exists and the new amount is higher than the current amount.
Q30
Multiple ChoiceWhich 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;
This command inserts a new row into the 'inventory' table with a stock value based on a subquery or updates the existing stock by adding the new stock value to the current one.