Insert a batch of notifications and grab the ids
You're broadcasting notifications to several users at once. Inserting one row at a time means four round-trips plus the risk of the service restarting between them. One INSERT with multiple values plus returning the generated ids solves both.
The table notifications(id SERIAL, user_id, msg) is empty. Add four rows in a single statement: (user_id = 1, msg = 'hello'), (user_id = 2, msg = 'hi'), (user_id = 1, msg = 'again'), (user_id = 3, msg = 'welcome') — and in the same statement return the generated id values along with the user_id.
Expected output sample
This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables.
| id | user_id |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign in