Bird
0
0

Given the table products(id, name, stock) with rows: (1, 'Pen', 10), (2, 'Pencil', 5), what will be the result of this query?

medium📝 query result Q13 of 15
PostgreSQL - Common Table Expressions
Given the table products(id, name, stock) with rows: (1, 'Pen', 10), (2, 'Pencil', 5), what will be the result of this query?
WITH updated AS (UPDATE products SET stock = stock + 10 WHERE name = 'Pen' RETURNING *) SELECT * FROM updated;
A[{id:1, name:'Pen', stock:20}]
B[{id:1, name:'Pen', stock:10}]
C[{id:2, name:'Pencil', stock:15}]
DEmpty result set
Step-by-Step Solution
Solution:
  1. Step 1: Identify updated rows

    The UPDATE increases stock by 10 only for product with name 'Pen' (id=1), changing stock from 10 to 20.
  2. Step 2: Understand RETURNING and SELECT

    The RETURNING * returns the updated row, and the SELECT outputs it.
  3. Final Answer:

    [{id:1, name:'Pen', stock:20}] -> Option A
  4. Quick Check:

    Updated Pen stock = 10 + 10 = 20 [OK]
Quick Trick: RETURNING shows updated rows immediately [OK]
Common Mistakes:
  • Assuming all rows are updated
  • Ignoring RETURNING clause output
  • Confusing stock before and after update

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes