Understand SQL Aggregations and Joins: Key Differences Explained
Company: Fannie Mae
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates a candidate's competency in SQL aggregations (COUNT, SUM, AVG, MIN, MAX), join semantics (INNER, LEFT, RIGHT, FULL OUTER), set operations (UNION vs UNION ALL), window functions, view versus table trade-offs, duplicate detection and removal, query optimization techniques, and pandas DataFrame operations (merge, join, concat) within the Data Manipulation (SQL/Python) domain. It is commonly asked because it assesses both conceptual understanding (function purposes and join semantics) and practical application (writing efficient queries, deduplicating data, and translating patterns between SQL and pandas), thereby probing correctness and performance reasoning.
Aggregation basics overview
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Hints
- Use aggregate functions across the entire Employees table.
LEFT JOIN with counts
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Departments(id INTEGER, dept_name VARCHAR)
Hints
- LEFT JOIN from Departments to Employees, then GROUP BY department.
RIGHT JOIN via swap
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Departments(id INTEGER, dept_name VARCHAR)
Hints
- Simulate RIGHT JOIN by LEFT JOIN from Departments to Employees.
FULL OUTER JOIN emulate
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Departments(id INTEGER, dept_name VARCHAR)
Hints
- Combine INNER JOIN with unmatched rows from a LEFT JOIN using UNION ALL.
UNION vs UNION ALL
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Hints
- Use UNION for distinct rows, UNION ALL to keep duplicates.
Window functions demo
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Departments(id INTEGER, dept_name VARCHAR)
Hints
- Use AVG() OVER (PARTITION BY ...) for the department average.
- Use DENSE_RANK() OVER (PARTITION BY ... ORDER BY ... DESC) for ranking.
View-like CTE usage
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Departments(id INTEGER, dept_name VARCHAR)
Hints
- Use WITH to define a named subquery and then select from it.
Hide duplicates with DISTINCT
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Departments(id INTEGER, dept_name VARCHAR)
Hints
- DISTINCT removes duplicate department names produced by the join.
Identify duplicates to delete
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Hints
- Use ROW_NUMBER over a partition to mark duplicates and filter rn > 1.
Nth Highest Salary
Tables
Employees(id INTEGER, name VARCHAR, salary INTEGER, dept_id INTEGER)
Hints
- DENSE_RANK() (not RANK or ROW_NUMBER) collapses duplicate salaries into one rank with no gaps.
- Filter the ranked rows to rk = 2, then aggregate so you always return exactly one row.