Orders: List Orders With Customer Names (INNER JOIN)

SQL coding challenge · Difficulty: easy · +50 XP

Problem

The billing system generates invoices that need each order linked to the customer's name. Only include orders with a valid matching customer.

Tables

Table: customers

| customer_id | customer_name | status | register_date |
| --- | --- | --- | --- |
| 1 | Krishna | Active | 2024-05-01 |
| 2 | Parvati | Active | 2024-05-10 |
| 3 | Atul | Inactive | 2024-04-01 |
| 4 | Pinki | Active | 2024-05-20 |
| 5 | Sudheer | Active | 2024-04-01 |

Table: orders

| order_id | customer_id | order_date | order_amount |
| --- | --- | --- | --- |
| 101 | 1 | 2024-05-01 | 500 |
| 102 | 1 | 2024-05-05 | 800 |
| 103 | 1 | 2024-05-10 | 200 |
| 104 | 2 | 2024-05-02 | 1200 |
| 105 | 2 | 2024-05-08 | 600 |
| 106 | 3 | 2024-05-03 | 300 |
| 107 | 4 | 2024-05-04 | 200 |
| 108 | 4 | 2024-05-09 | 400 |
| 109 | 4 | 2024-05-11 | 700 |
| 110 | 4 | 2024-05-12 | 350 |
| 111 | 4 | 2024-05-13 | 450 |
| 112 | 4 | 2024-05-14 | 600 |
| 113 | 1 | 2024-06-05 | 1000 |
| 114 | 2 | 2024-06-10 | 900 |

Expected Output

| order_id | order_date | customer_name |
| --- | --- | --- |
| 101 | 2024-05-01 | Krishna |
| 102 | 2024-05-05 | Krishna |
| 103 | 2024-05-10 | Krishna |
| 104 | 2024-05-02 | Parvati |
| 105 | 2024-05-08 | Parvati |
| 106 | 2024-05-03 | Atul |
| 107 | 2024-05-04 | Pinki |
| 108 | 2024-05-09 | Pinki |
| 109 | 2024-05-11 | Pinki |
| 110 | 2024-05-12 | Pinki |
| 111 | 2024-05-13 | Pinki |
| 112 | 2024-05-14 | Pinki |
| 113 | 2024-06-05 | Krishna |
| 114 | 2024-06-10 | Parvati |
  • Return: order_id, order_date, customer_name
  • Only orders with a matching customer (INNER JOIN)
  • Sort by order_id ascending

Solve this challenge on PySpark.in