2305. Messaging: Fetch the latest message in each conversation. — Python + FastAPI + SQLAlchemy

DBMS Published Updated 0 Views Verified
In a messaging module, solve this as a production problem using Python + FastAPI + SQLAlchemy: Fetch the latest message in each conversation. Provide the SQL, explain the logic, show safe application integration, and mention failure/edge cases.
Looking for the latest Sarkari Result 2026 updates? Check the latest government exam results, merit lists and result updates on Sarkari Resultess.
Sarkari Resultess
Questions & Answers

Answer & Explanation

4 Answers
Verified Correct Answer

Option A

SELECT * FROM (SELECT m.*,ROW_NUMBER() OVER(PARTITION BY conversation_id ORDER BY created_at DESC,id DESC) rn FROM messages m) x WHERE rn=1;
Additional Explanation
Partition by conversation and rank newest first.
Sarkari Resultess
Editorial Answer 1
Verified

Option B

UPDATE table_name SET status='active';
Additional Explanation
Incorrect: it does not satisfy the stated business rule and can return/change unintended data.
Sarkari Resultess
Editorial Answer 2
Verified

Option C

DELETE FROM table_name;
Additional Explanation
Incorrect: it does not satisfy the stated business rule and can return/change unintended data.
Sarkari Resultess
Editorial Answer 3
Verified

Option D

SELECT * FROM table_name;
Additional Explanation
Incorrect: it does not satisfy the stated business rule and can return/change unintended data.
Sarkari Resultess
Editorial Answer 4