cswork / libraryOpen practice workspace

Coding questions

Number Entries Within Each Conversation

Assign one-based entry numbers per conversation using timestamp and identifier ordering in a PostgreSQL window-function query.

The source relation is named entries for this exercise and has these columns:

ColumnMeaning
entry_uuidIdentifier of an entry.
context_uuidChat or conversation containing the entry.
visitor_idVisitor associated with the entry.
created_atEntry creation timestamp.
platformPlatform on which the entry was created.

A conversation can contain several entries. Write one read-only PostgreSQL query that assigns each entry its position within its own context_uuid.

Start numbering at 1 for each context. Within a context, order entries by created_at ascending and then by entry_uuid ascending. The entry identifier resolves a tie in creation timestamps.

Return the original columns followed by the new entry_number column, in this order:

entry_uuid, context_uuid, visitor_id, created_at, platform, entry_number.

The required order determines the numbering within each context. The final result rows may be returned in any order.

Number Entries Within Each Conversation

Table entries

columntypeconstraints
entry_uuidTEXT
context_uuidTEXT
visitor_idINTEGER
created_atTIMESTAMP
platformTEXT

Sample data:

context_uuidcreated_atentry_uuidplatformvisitor_id
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa2026-09-28 11:00:0000000000-0000-4000-8000-000000000501web11
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa2026-09-28 13:00:0000000000-0000-4000-8000-000000000101ios12
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa2026-09-28 09:00:0000000000-0000-4000-8000-000000000902web11
aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa2026-09-28 11:00:0000000000-0000-4000-8000-000000000301android13
bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb2026-09-28 12:00:0000000000-0000-4000-8000-000000000055web11
bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb2026-09-28 00:00:0000000000-0000-4000-8000-000000000999web11
bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb2026-09-27 23:59:5900000000-0000-4000-8000-000000000202ios14
cccccccc-cccc-4ccc-8ccc-cccccccccccc2025-12-31 23:59:5900000000-0000-4000-8000-000000000007web11
dddddddd-dddd-4ddd-8ddd-dddddddddddd2026-09-28 10:00:0000000000-0000-4000-8000-000000000801webNULL
dddddddd-dddd-4ddd-8ddd-dddddddddddd2026-09-28 10:00:0000000000-0000-4000-8000-000000000800NULL15

Expected output

entry_uuidcontext_uuidvisitor_idcreated_atplatformentry_number
00000000-0000-4000-8000-000000000501aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa112026-09-28 11:00:00web3
00000000-0000-4000-8000-000000000101aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa122026-09-28 13:00:00ios4
00000000-0000-4000-8000-000000000902aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa112026-09-28 09:00:00web1
00000000-0000-4000-8000-000000000301aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa132026-09-28 11:00:00android2
00000000-0000-4000-8000-000000000055bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb112026-09-28 12:00:00web3
00000000-0000-4000-8000-000000000999bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb112026-09-28 00:00:00web2
00000000-0000-4000-8000-000000000202bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb142026-09-27 23:59:59ios1
00000000-0000-4000-8000-000000000007cccccccc-cccc-4ccc-8ccc-cccccccccccc112025-12-31 23:59:59web1
00000000-0000-4000-8000-000000000801dddddddd-dddd-4ddd-8ddd-ddddddddddddNULL2026-09-28 10:00:00web2
00000000-0000-4000-8000-000000000800dddddddd-dddd-4ddd-8ddd-dddddddddddd152026-09-28 10:00:00NULL1