قابل تلاش، قابل پرنٹ SQL جوائنز حوالہ: inner، left، right، full، اینٹی اور سیمی جوائنز، سیلف جوائنز، ON بمقابلہ WHERE اور LATERAL۔ مفت۔

Basic join types

9
SELECT * FROM a INNER JOIN b ON a.id = b.a_id;
Only rows with a match in BOTH tables
SELECT * FROM a LEFT JOIN b ON a.id = b.a_id;
All left rows, NULLs where no match
SELECT * FROM a RIGHT JOIN b ON a.id = b.a_id;
All right rows, NULLs where no match
SELECT * FROM a FULL OUTER JOIN b ON a.id = b.a_id;
All rows from both, NULLs if unmatched
JOIN = INNER JOIN
Plain JOIN defaults to INNER
LEFT OUTER JOIN = LEFT JOIN
The OUTER keyword is optional
LEFT JOIN ... UNION ... RIGHT JOIN ...
Emulate FULL JOIN in MySQL (none native)
ON a.id = b.a_id AND a.type = b.type
Compound (multi-column) join condition
ON a.qty BETWEEN p.min_qty AND p.max_qty
Non-equi join: match on a range

Anti-joins (no match)

8
LEFT JOIN b ON a.id = b.a_id WHERE b.a_id IS NULL
Left rows with NO match on the right
WHERE NOT EXISTS (SELECT 1 FROM b WHERE b.a_id = a.id)
Anti-join; safe with NULLs
WHERE a.id NOT IN (SELECT a_id FROM b)
One NULL in the list = empty result!
WHERE a.id NOT IN (SELECT a_id FROM b WHERE a_id IS NOT NULL)
NOT IN made NULL-safe
RIGHT JOIN a ... WHERE a.id IS NULL
Right rows with no match on the left
SELECT id FROM a EXCEPT SELECT a_id FROM b;
Set difference (Postgres, MySQL 8.0.31+)
users without orders = LEFT JOIN + IS NULL
The classic anti-join use case
Prefer NOT EXISTS over NOT IN
Same plan, none of the NULL traps

Semi-joins (has a match)

8
WHERE EXISTS (SELECT 1 FROM b WHERE b.a_id = a.id)
Left rows having a match; never duplicates
WHERE a.id IN (SELECT a_id FROM b)
Same intent; fine for short value lists
JOIN b ... GROUP BY a.id
JOIN duplicates 1-to-many; dedupe needed
SELECT DISTINCT a.* FROM a JOIN b ON ...
Semi-join via JOIN; EXISTS is cleaner
EXISTS stops at the first match
Often faster than joining everything
EXISTS (SELECT 1 ...) vs (SELECT * ...)
Selected columns are ignored; 1 is idiomatic
IN: uncorrelated / EXISTS: correlated
Optimizers usually rewrite them the same
users WITH orders = EXISTS subquery
The classic semi-join use case

Self joins

8
FROM emp e JOIN emp m ON e.manager_id = m.id
Each employee paired with their manager
FROM emp e LEFT JOIN emp m ON e.manager_id = m.id
Keeps top-level rows (manager is NULL)
SELECT e.name, m.name AS manager FROM ...
Aliases are REQUIRED to tell copies apart
ON a.id < b.id
Unique pairs: no self-match, no mirrors
ON a.id <> b.id
All ordered pairs except self-matches
ON b.day = a.day + INTERVAL 1 DAY
Adjacent rows (gaps and islands)
ON a.city = b.city AND a.id <> b.id
Other rows sharing the same value
WITH RECURSIVE tree AS (... UNION ALL ...)
Whole hierarchy: recursive CTE, not a join

Cross & multi-table joins

8
SELECT * FROM sizes CROSS JOIN colors;
Every combination: m x n rows, no ON
FROM sizes, colors
Implicit cross join; avoid this syntax
FROM users u JOIN orders o ON ... JOIN items i ON ...
Chain joins; each needs its own ON
JOIN dates d CROSS JOIN users u LEFT JOIN ...
Calendar x users grid, then fill data
u LEFT JOIN o ON ... LEFT JOIN i ON i.o_id = o.id
Keep LEFT down the chain to keep all users
u LEFT JOIN o ... INNER JOIN i ON i.o_id = o.id
INNER after LEFT drops the NULL rows!
FROM a JOIN (b LEFT JOIN c ON ...) ON ...
Parentheses control join grouping
Written order is logical, not execution order
The optimizer reorders inner joins freely

ON vs WHERE (the gotcha)

8
LEFT JOIN b ON a.id = b.a_id AND b.status = 1
Filters RIGHT side; all left rows kept
LEFT JOIN b ON a.id = b.a_id WHERE b.status = 1
NULL fails the test: LEFT becomes INNER!
WHERE b.a_id IS NULL OR b.status = 1
Keep unmatched rows despite a WHERE filter
LEFT JOIN b ON ... AND a.type = 'x'
Left-side filter in ON does NOT drop rows
WHERE a.type = 'x'
Filter the LEFT table in WHERE instead
INNER JOIN: ON and WHERE are equivalent
The trap only exists for outer joins
COALESCE(b.total, 0)
Replace outer-join NULLs with a default
COUNT(b.id) vs COUNT(*)
COUNT(col) skips NULLs: 0 for no match

USING, NATURAL & set ops

9
JOIN b USING (user_id)
Equi-join on a same-named column
USING outputs ONE shared column
ON keeps both a.user_id and b.user_id
NATURAL JOIN b
Auto-joins ALL same-named columns; fragile
Avoid NATURAL JOIN in production
A new column silently changes the join
SELECT ... UNION SELECT ...
Stack rows vertically, duplicates removed
UNION ALL
Keep duplicates; much faster than UNION
JOIN adds columns, UNION adds rows
Widen vs lengthen the result set
INTERSECT
Rows present in both result sets
UNION needs same column count and types
Names come from the first SELECT

LATERAL & performance

9
CROSS JOIN LATERAL (SELECT ... WHERE b.a_id = a.id LIMIT 3) t
Per-row subquery: top-N per group
LEFT JOIN LATERAL (...) t ON TRUE
Keep left rows whose subquery is empty
CROSS APPLY / OUTER APPLY
SQL Server spelling of LATERAL joins
LATERAL sees columns of preceding tables
A plain subquery in FROM cannot
CREATE INDEX idx ON orders (user_id);
Index the FK side of every frequent join
EXPLAIN SELECT ...;
Check join order, type, and index usage
Filter early: join the smallest set first
Less rows carried through each join
ON DATE(a.created_at) = b.day
Function on a column kills index use
Join on same-type, same-collation columns
Implicit casts also disable indexes

کوئی اندراج “:q” سے میل نہیں کھاتا۔


SQL جوائنز چیٹ شیٹ کے بارے میں

یہ SQL جوائنز چیٹ شیٹ ایک کام پوری طرح کرتی ہے: یہ جدولوں کو ملانے کے ہر طریقے کی وضاحت کرتی ہے۔ آٹھ حصے جوائن کی بنیادی اقسام، بغیر میچ والی قطاروں کے لیے اینٹی جوائنز، میچ رکھنے والی قطاروں کے لیے سیمی جوائنز، سیلف جوائنز، کراس اور کئی جدولوں کے جوائنز، ON بمقابلہ WHERE کا کلاسیکی جال، USING، NATURAL اور سیٹ آپریشنز، اور کارکردگی کے نوٹس کے ساتھ LATERAL جوائنز کا احاطہ کرتے ہیں۔

جوائنز وہ جگہ ہیں جہاں SQL کے نتائج خاموشی سے غلط ہو جاتے ہیں: WHERE شق میں فلٹر کیا گیا LEFT JOIN خاموشی سے INNER JOIN بن جاتا ہے، اور NULL کے ساتھ NOT IN کچھ نہیں لوٹاتا۔ ان جالوں کو یہاں اپنی قطاریں ملتی ہیں، ساتھ ہی وہ درست پیٹرن بھی جو اس کی جگہ استعمال کرنا چاہیے۔

اس گروپ کی ہر چیٹ شیٹ کی طرح یہ مفت اور کلائنٹ سائیڈ ہے: سرچ باکس سے قطاروں کو لائیو فلٹر کریں، چپکے رہنے والی فہرست مضامین سے حصوں کے درمیان کودیں، ایک کلک سے کوئی بھی سٹیٹمنٹ کاپی کریں اور اپنی میز پر حوالے کے لیے صفحہ پرنٹ کریں۔

SQL جوائنز چیٹ شیٹ استعمال کرنے کا طریقہ

  1. شیٹ کھولیں اور حصوں کا جائزہ لیں، Basic join types سے لے کر LATERAL & performance تک۔
  2. LEFT JOIN، EXISTS یا NOT IN جیسا کوئی کلیدی لفظ تلاش کریں تاکہ ہر قطار لائیو فلٹر ہو جائے۔
  3. بہت کم قطاریں لوٹانے والے LEFT JOIN کو ڈیبگ کرنے سے پہلے ON vs WHERE کا حصہ پڑھیں۔
  4. SQL کو اپنی کلپ بورڈ پر کاپی کرنے کے لیے کسی سٹیٹمنٹ یا اس کے کاپی آئیکن پر کلک کریں۔
  5. مکمل جوائنز حوالے کی کاغذی کاپی کے لیے Print استعمال کریں۔

اکثر پوچھے جانے والے سوالات

Inner، left، right اور full outer جوائنز، ساتھ اینٹی جوائنز، سیمی جوائنز، سیلف جوائنز، کراس جوائنز، کئی جدولوں کے جوائنز، USING اور NATURAL جوائنز، سیٹ آپریشنز کا UNION خاندان، اور LATERAL جوائنز۔

ON میں شرط جڑی ہوئی جدول کو اس سے پہلے فلٹر کرتی ہے کہ آؤٹر جوائن غیر میچ قطاریں رکھے؛ وہی شرط WHERE میں بعد میں چلتی ہے اور NULL قطاریں ہٹا دیتی ہے، جس سے LEFT JOIN، INNER JOIN بن جاتا ہے۔ شیٹ میں بالکل اسی پر ایک حصہ ہے۔

اینٹی جوائن کا حصہ LEFT JOIN … WHERE right.id IS NULL اور NOT EXISTS پیٹرنز دکھاتا ہے، اور بتاتا ہے کہ جب سب کوئری NULL لوٹا سکتی ہو تو NOT IN غلط برتاؤ کیوں کرتا ہے۔

جی ہاں۔ سنٹیکس معیاری SQL ہے اور MySQL، PostgreSQL، SQL Server اور SQLite میں کام کرتا ہے، اور چند انجن مخصوص نوٹس قطار پر ہی بتا دیے گئے ہیں۔

جی ہاں، یہ مکمل طور پر مفت ہے اور بغیر لاگ ان کے آپ کے براؤزر میں چلتا ہے۔

شیئر کریں

مقبول تلاشیں
sql جوائنز چیٹ شیٹ sql جوائن کی اقسام inner join بمقابلہ left join sql anti join full outer join sql self join مثال جوائنز میں on بمقابلہ where
مدد چاہیے؟
اس ٹول میں کوئی مسئلہ ملا؟ ہماری ٹیم کو بتائیں۔
مسئلہ رپورٹ کریں

اس مفت ٹول کو اپنی ویب سائٹ پر شامل کریں: نیچے دیا گیا کوڈ کاپی اور پیسٹ کریں۔