ਇੱਕ JOIN ਦੋ tables ਦੀਆਂ rows ਨੂੰ ਇੱਕ condition, ਆਮ ਤੌਰ 'ਤੇ ਇੱਕ foreign key, ਨਾਲ match ਕਰਕੇ ਜੋੜਦਾ ਹੈ। join ਦਾ type ਤੈਅ ਕਰਦਾ ਹੈ ਕਿ ਉਹਨਾਂ rows ਦਾ ਕੀ ਹੁੰਦਾ ਹੈ ਜਿਨ੍ਹਾਂ ਦਾ ਦੂਜੇ ਪਾਸੇ ਕੋਈ match ਨਹੀਂ ਹੁੰਦਾ।
ਇੱਕ JOIN ਦੋ tables ਦੀਆਂ rows ਨੂੰ ਇੱਕ condition, ਆਮ ਤੌਰ 'ਤੇ ਇੱਕ foreign key, ਨਾਲ match ਕਰਕੇ ਜੋੜਦਾ ਹੈ। join ਦਾ type ਤੈਅ ਕਰਦਾ ਹੈ ਕਿ ਉਹਨਾਂ rows ਦਾ ਕੀ ਹੁੰਦਾ ਹੈ ਜਿਨ੍ਹਾਂ ਦਾ ਦੂਜੇ ਪਾਸੇ ਕੋਈ match ਨਹੀਂ ਹੁੰਦਾ।
NULL ਵਜੋਂ ਵਾਪਸ ਆਉਂਦੀਆਂ ਹਨ।-- customers(id, name) orders(id, customer_id, total)
-- INNER: only customers who have at least one order
SELECT c.name, o.total
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;
-- LEFT: every customer, even those with zero orders (total = NULL)
SELECT c.name, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;
-- Classic use: find customers with NO orders
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL; -- the LEFT JOIN's NULL side reveals the gap
WHERE ਵਿੱਚ right table ਨੂੰ filter ਕਰਨਾ (ਜਿਵੇਂ WHERE o.total > 100) ਚੁੱਪ-ਚਾਪ ਇੱਕ LEFT JOIN ਨੂੰ ਵਾਪਸ INNER JOIN ਵਿੱਚ ਬਦਲ ਦਿੰਦਾ ਹੈ, ਕਿਉਂਕਿ NULL > 100 false ਹੁੰਦਾ ਹੈ। ਜੇ ਤੁਸੀਂ unmatched rows ਰੱਖਣਾ ਚਾਹੁੰਦੇ ਹੋ ਤਾਂ ਅਜਿਹੀਆਂ conditions ਨੂੰ ON clause ਵਿੱਚ ਪਾਓ।ਜ਼ਿਆਦਾਤਰ ਅਸਲ queries ਕਈ tables ਵਿੱਚ ਫੈਲਦੀਆਂ ਹਨ, ਅਤੇ ਗ਼ਲਤ join type ਚੁੱਪ-ਚਾਪ rows ਨੂੰ ਸੁੱਟ ਦਿੰਦਾ ਹੈ ਜਾਂ duplicate ਕਰ ਦਿੰਦਾ ਹੈ — ਇੱਕ ਅਜਿਹਾ bug ਜੋ ਵਾਜਬ ਲੱਗਦੇ ਨਤੀਜੇ ਵਾਪਸ ਕਰਦਾ ਹੈ। ਇਹ ਜਾਣਨਾ ਕਿ LEFT JOIN + IS NULL "ਗੁੰਮ" rows ਲੱਭਦਾ ਹੈ, ਅਤੇ ਕਿ null ਵਾਲੇ ਪਾਸੇ ਉੱਤੇ ਇੱਕ WHERE LEFT ਨੂੰ ਰੱਦ ਕਰ ਦਿੰਦਾ ਹੈ, reporting errors ਦੀ ਇੱਕ ਪੂਰੀ ਸ਼੍ਰੇਣੀ ਨੂੰ ਰੋਕਦਾ ਹੈ।
ਵਿਸਤ੍ਰਿਤ ਜਵਾਬਾਂ ਨਾਲ IT ਇੰਟਰਵਿਊ ਸਵਾਲਾਂ ਦੀ ਇੱਕ ਲਾਇਬ੍ਰੇਰੀ — ਜੂਨੀਅਰ ਤੋਂ ਸੀਨੀਅਰ ਤੱਕ।
ਦਾਨ ਕਰੋ