Bird
0
0

Given the table products(id SERIAL PRIMARY KEY, name TEXT UNIQUE, stock INT), what will be the result of this query?

medium📝 query result Q4 of 15
PostgreSQL - Set Operations and Advanced Queries
Given the table products(id SERIAL PRIMARY KEY, name TEXT UNIQUE, stock INT), what will be the result of this query?
INSERT INTO products(name, stock) VALUES ('Pen', 10) ON CONFLICT (name) DO UPDATE SET stock = products.stock + 5 RETURNING stock;

Assuming a row with name 'Pen' and stock 20 already exists.
A5
B10
C25
DError due to ambiguous reference
Step-by-Step Solution
Solution:
  1. Step 1: Understand the ON CONFLICT DO UPDATE action

    The existing row with name 'Pen' has stock 20. The update adds 5 to this stock.
  2. Step 2: Calculate the new stock value

    20 + 5 = 25. The RETURNING clause returns the updated stock value.
  3. Final Answer:

    25 -> Option C
  4. Quick Check:

    Updated stock = 25 [OK]
Quick Trick: DO UPDATE can use existing column values to calculate new ones [OK]
Common Mistakes:
  • Assuming insert overwrites without update
  • Ignoring RETURNING clause output
  • Thinking EXCLUDED is needed for existing row values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes