← Back to PostgreSQL Course | Chapter 6: Joins | Lesson 7 of 7

कई Tables के JOIN

आप एक query में तीन या ज़्यादा tables combine करने के लिए कई joins chain कर सकते हैं।

In this page:

  1. Multiple Table Joins
Syntax
sql
SELECT columns
FROM table1
JOIN table2 ON table1.key = table2.key
JOIN table3 ON table2.key = table3.key;

Multiple Table Joins

हर extra table के लिए एक JOIN ... ON add करें, हर एक पहले से joined किसी चीज़ से link करते हुए। Readability के लिए order matter करता है लेकिन planner actual execution order चुनता है।

Join fan-out के लिए watch करें: one-to-many tables join करना rows multiply करता है, जो aggregates inflate कर सकता है।

Note: results debug करते वक्त हर join के बाद row counts check करें।

उदाहरण: Multiple table joins

sql
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER);
CREATE TABLE order_items (order_id INTEGER, product_id INTEGER, qty INTEGER);
CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price INTEGER);
INSERT INTO customers VALUES (1, 'Ada'), (2, 'Bob');
INSERT INTO orders VALUES (10, 1), (11, 2);
INSERT INTO products VALUES (100, 'Pen', 3), (101, 'Lamp', 45);
INSERT INTO order_items VALUES (10, 100, 5), (10, 101, 1), (11, 100, 2);
SELECT c.name, p.name AS product, oi.qty, oi.qty * p.price AS line_total
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
ORDER BY o.id, p.id;

-- Output:
-- name | product | qty | line_total
-- Ada | Pen | 5 | 15
-- Ada | Lamp | 1 | 45
-- Bob | Pen | 2 | 6
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. कई one-to-many tables join करके rows multiply करना
  2. एक ON condition missing होना
  3. Column names को ambiguous छोड़ना
चैप्टर सारांश
  • JOIN ... ON clauses chain करें
  • हर join एक joined table से link करता है
  • Row multiplication के लिए watch करें
  • हर table alias करें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.