DBMS and SQL · 1 min
Do you make this UNION vs UNION ALL mistake?
Learn how UNION removes duplicate rows while UNION ALL retains all rows and runs cheaper by skipping de-duplication.
Practise DBMS and SQLMore videos
The quiz
Table x holds 1, 2, 2 and table y holds 2, 3. How many rows does this return?
- 5
- 3
- 4
- 2
Show the answer
A. 5
Transcript
Galat SQL logic tumhe interview mein reject karwayega, three rows bolna totally wrong hai.
Query dekho: table x deta hai three rows aur table y deta hai two rows, isliye UNION ALL keeps all, yaani total five rows.
Ab sabse important part, plain UNION duplicate rows removes karta hai, leaving three rows, lekin UNION ALL keeps all five rows bina filtering.
Kabhi assume mat karna ki duplicates drop honge, comma lagane se cross join banega, hamesha columns count aur data types match karo.
Interview mein confident hokar bolo ki UNION ALL five rows dega, kyonki yeh duplicates retain karta hai aur de-duplication skip karke cheaper banta hai.
Screen par question dekho, video pause karo aur choose karo: kya answer A, B, C ya D hoga, turant screen par tap karo.
Correct answer A hai, UNION ALL pancho rows rakhta hai, jabki plain UNION duplicates hatakar sirf three rows return karta hai.
Placement interview revision ke liye yeh table save kar lo, aur aise tricky DBMS questions InternDoor par daily practice karte raho.