SQL LESSON 15 OF 32

Join three tables for real insights

A bridge table connects orders to products. Join orders to order_items, then products. Multiply quantity by unit price to calculate line value. Check the grain: one row per item is different from one row per order.

हिन्दी में समझें

order_items ऑर्डर और उत्पाद जोड़ती है। quantity और price से पंक्ति की रकम निकालें। एक item की पंक्ति और एक order की पंक्ति अलग हैं।

अभ्यास: order_id और SUM(quantity * price) AS order_total, order_id अनुसार समूह और क्रम।

Hinglish mein samjhein

order_items orders aur products ko connect karti hai. quantity * price line value hai. Grain samjho: ek item ki row aur ek order ki row alag hain.

Practice: order_id aur SUM(quantity * price) AS order_total, order_id group aur sorted.

A small SQL example

SELECT i.order_id, p.name, i.quantity * p.price AS line_total FROM order_items i JOIN products p ON p.id=i.product_id ORDER BY i.order_id,p.id;

Examples use the fictional shop database bundled with the SQL playground: customers, products, orders, order_items and employees. The engine is SQLite; other SQL dialects may differ.

Try this example in the SQL playground →

Practise this idea

Return order_id and SUM(quantity * price) as order_total, grouped and ordered by order_id.

Check answer & explanation
SELECT i.order_id, SUM(i.quantity*p.price) AS order_total FROM order_items i JOIN products p ON p.id=i.product_id GROUP BY i.order_id ORDER BY i.order_id;

Each item line contributes quantity * price. Grouping by order_id adds those line values into one total per order.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

To calculate a line total multiply…

  1. quantity by price
  2. id by name
  3. status by date
Check the quiz answer

quantity by price

A bridge table connects orders to products. Join orders to order_items, then products. Multiply quantity by unit price to calculate line value. Check the grain: one row per item is different from one row per order.

References & corrections

Use the official SQLite documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.