← Back to MySQL Course | Chapter 9: Joins | Lesson 4 of 10

RIGHT JOIN

RIGHT JOIN mirror image है: यह दूसरी table से सबको रखता है और अगर exist करें तो पहली से matches add करता है।
Syntax
sql
SELECT columns
FROM table1
RIGHT JOIN table2
  ON table1.column = table2.column;

एक RIGHT JOIN क्या है?

एक RIGHT JOIN right table की सारी rows रखता है चाहे left पर एक match exist करे या नहीं। यह उन्हें possible होने पर left table की rows से match करता है। अगर कोई match नहीं है, left side NULL return करता है, mirror करते हुए कि LEFT JOIN right side को कैसे treat करता है।

उदाहरण: What is a RIGHT JOIN?

sql
CREATE TABLE customers (customer_id INT, name TEXT);
CREATE TABLE orders (order_id INT, customer_id INT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1), (102, 2);
SELECT customers.name, orders.order_id
FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

RIGHT JOIN बनाम LEFT JOIN Equivalence

हर RIGHT JOIN को एक equivalent LEFT JOIN की तरह फिर से लिखा जा सकता है। आप बस अपनी query में table order swap कर देते हैं। ज़्यादातर style guides consistency के लिए हर जगह LEFT JOIN पर टिके रहने को prefer करती हैं, क्योंकि RIGHT JOIN कहीं कम इस्तेमाल होता है और readers को confuse कर सकता है।

उदाहरण: RIGHT JOIN vs LEFT JOIN Equivalence

sql
CREATE TABLE customers (customer_id INT, name TEXT);
CREATE TABLE orders (order_id INT, customer_id INT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1);
-- RIGHT JOIN
SELECT customers.name FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
-- Equivalent LEFT JOIN (tables swapped)
SELECT customers.name FROM orders LEFT JOIN customers ON customers.customer_id = orders.customer_id;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

RIGHT JOIN से Orphans Identify करना

आप right table में ऐसी rows ढूँढ सकते हैं जिनके left side पर कोई matching records नहीं, जैसे वे products जो कभी order नहीं हुए। इसके लिए, join के बाद left-side NULLs check करें, LEFT JOIN orphan-finding pattern की mirror image।

उदाहरण: Identifying Orphans with RIGHT JOIN

sql
CREATE TABLE products (product_id INT, name TEXT);
CREATE TABLE orders (order_id INT, product_id INT);
INSERT INTO products VALUES (1, 'Keyboard'), (2, 'Mouse');
INSERT INTO orders VALUES (101, 1);
SELECT products.name
FROM orders RIGHT JOIN products ON orders.product_id = products.product_id
WHERE orders.order_id IS NULL;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

कई Tables Right Join करना

आप एक साथ कई tables से information इकट्ठा करने के लिए कई RIGHT JOIN statements chain कर सकते हैं। यह rightmost table के records preserve करते हुए information इकट्ठा करता है, यह ensure करते हुए कि उस final table से कुछ भी न छूटे।

उदाहरण: Right Joining Multiple Tables

sql
CREATE TABLE categories (category_id INT, name TEXT);
CREATE TABLE products (product_id INT, category_id INT, name TEXT);
CREATE TABLE orders (order_id INT, product_id INT);
INSERT INTO categories VALUES (1, 'Electronics');
INSERT INTO products VALUES (10, 1, 'Keyboard');
INSERT INTO orders VALUES (101, 10);
SELECT categories.name, products.name, orders.order_id
FROM orders
RIGHT JOIN products ON orders.product_id = products.product_id
RIGHT JOIN categories ON products.category_id = categories.category_id;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

RIGHT JOIN Aggregate Computations

एक RIGHT JOIN को aggregation के साथ combine करना आपको unmatched rows के लिए zeroes सहित total figures display करने देता है, जैसे कभी नहीं बिके products के लिए भी हर product का sales count दिखाना।

उदाहरण: RIGHT JOIN Aggregate Computations

sql
CREATE TABLE products (product_id INT, name TEXT);
CREATE TABLE orders (order_id INT, product_id INT);
INSERT INTO products VALUES (1, 'Keyboard'), (2, 'Mouse');
INSERT INTO orders VALUES (101, 1);
SELECT products.name, COUNT(orders.order_id) AS sales_count
FROM orders RIGHT JOIN products ON orders.product_id = products.product_id
GROUP BY products.name;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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. कौन सी table preserve होती है इसे mix up करना, जबकि RIGHT JOIN right पर table की हर row रखता है।
  2. एक RIGHT JOIN लिखना जहाँ एक LEFT JOIN clearer होता, और table order को confuse करना।
  3. WHERE में left table filter करना, जो बिना match वाली rows हटा देता है।
🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 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.