Start learning free

LESSON 57 OF 173 · BACKEND: SQL

LEFT JOIN in C#

Practice

A plain JOIN keeps only the rows that find a match. That's a problem when you want a full list: a customer who never ordered has no matching order, so that customer vanishes from the result. A LEFT JOIN keeps every row of the left table, the one in FROM, even when the right table has nothing for it.

When nothing matches, the right table's columns are filled with NULL. For example, this query lists every product with the quantities it was ordered in, and a product nobody ordered still appears once, with NULL as its quantity:

var itemQuery = "SELECT products.name, order_items.quantity " +
    "FROM products " +
    "LEFT JOIN order_items " +
    "ON order_items.product_id = products.id ";

The ON rule works as in an ordinary join. A customer with two orders gets two rows; a customer with none gets one row, with NULL in the order's columns.

Your query lists each customer with the id and date of each of their orders. The check prints:

name | id | ordered
Ana Pop | 1 | 2026-03-02
Ana Pop | 3 | 2026-04-05
Ben Ortiz | 2 | 2026-03-15
Ben Ortiz | 6 | 2026-05-21
Chloe Ng | 4 | 2026-04-18
Chloe Ng | 7 | 2026-05-30
Dan Weber | 5 | 2026-05-07
Eva Rossi | NULL | NULL
(8 rows)

Your task: declare query selecting customers.name, orders.id and orders.ordered from customers, LEFT JOIN the orders, and match the rows with ON: the order's customer_id equals the customer's id.


Assignment
Follow the Coding Tutorial to list every customer with their orders, including Eva Rossi, who has none.


Hint
The table in FROM is the one whose rows are all kept, so start from customers. Every piece of the string ends with a space and +, except the ON line, which ends the statement with ;.


STEP 1 OF 4
    Ctrl + Enter to check

    Sql Left Join 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