SQL & Databases Medium technical 1 views 1 min read

UNION in relational algebra?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of SQL & Databases conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

In relational algebra, the UNION of two sets $A \cup B$ combines all distinct tuples from both relations into a single unified relation.

### UNION vs UNION ALL in SQL:

-- UNION removes duplicate records (performs sorting/deduplication)
SELECT city FROM customers
UNION
SELECT city FROM suppliers;

-- UNION ALL retains all rows including duplicates (much faster)
SELECT city FROM customers
UNION ALL
SELECT city FROM suppliers;

*Performance rule:* Always prefer UNION ALL unless business logic strictly requires deduplicating rows across the combined sets.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?