Start learning free

LESSON 73 OF 173 · BACKEND: APIS WITH A DATABASE

Orders in C#

Practice

The shop's database holds more than products. The table orders has one row per order: its id, the customer_id of the customer who placed it, the date it was placed (ordered) and its status. The customer's name is not in that row: it lives in another table, customers.

A JOIN reads two tables in one query. Its ON condition says which rows belong together, and table.column says which table a column comes from. Here is the idea on two other tables, books and their authors:

SELECT books.title, authors.name AS author
FROM books
JOIN authors ON authors.id = books.author_id

Each book is paired with the one author whose id is the book's author_id. AS author renames the column in the result, so RowToDict(reader) has the key "author". A long query can be written in pieces, one string per line joined with +: C# glues them into one string, so end each piece with a space.

Your route must give these answers:

GET /orders/1   →  200  {"id": 1, "ordered": "2026-03-02", "status": "paid", "customer": "Ana Pop"}
GET /orders/4   →  200  {"id": 4, "ordered": "2026-04-18", "status": "shipped", "customer": "Chloe Ng"}
GET /orders/6   →  200  {"id": 6, "ordered": "2026-05-21", "status": "cancelled", "customer": "Ben Ortiz"}
GET /orders/99  →  404  {"error": "Order not found"}

Your task: the route GET /orders/{id} is started for you, with its command cmd. It reads the order and its customer's name with one JOIN query and answers them as JSON, or 404 when no order has that id.


Assignment
Follow the Coding Tutorial to write the JOIN query of the /orders/{id} route and answer its row.


Hint
Both tables have a column id, so write orders.id or customers.id, never just id. reader.Read() gives false when no order has that id, as in the /products/{id} GET route.


STEP 1 OF 9
    Ctrl + Enter to check

    Db Orders in C#

    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free