cswork / libraryOpen practice workspace

Coding questions · Fannie Mae

Understand SQL Aggregations and Joins: Key Differences Explained

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.

Employees

+----+--------+--------+---------+
| id | name   | salary | dept_id |
+----+--------+--------+---------+
| 1  | Alice  | 70000  | 1       |
| 2  | Bob    | 90000  | 2       |
| 3  | Carol  | 80000  | 1       |
+----+--------+--------+---------+

Departments

+----+------------+
| id | dept_name  |
+----+------------+
| 1  | Finance    |
| 2  | Operations |
| 3  | Marketing  |
+----+------------+
Scenario

Querying and manipulating enterprise data stored in relational databases and pandas data frames.

Question

Explain the purpose and differences of COUNT, SUM, AVG, MIN, and MAX aggregation functions in SQL. Describe INNER, LEFT, RIGHT, and FULL OUTER joins and give a use-case for each. Contrast UNION and UNION ALL. When is each preferable? What is a window function in SQL and how does it differ from a standard aggregation? Compare SQL views to physical tables. List advantages and disadvantages. How can you hide duplicate rows in a result set without deleting them? How do you permanently remove duplicate rows from a table? Write an SQL query to solve LeetCode #177 (Nth Highest Salary). List practical techniques to improve SQL query efficiency. In pandas, compare merge, join, and concat. When should each be used?

Hints

Expect to write efficient SQL, reason about duplicates, and pick the right pandas operation.

Aggregation basics overview

Compute the overall COUNT, SUM, AVG, MIN, and MAX of salaries across all employees.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Expected output

Single-row aggregates over all employees.

avg_salarycount_employeesmax_salarymin_salarytotal_salary
8000039000070000240000

Hints

  1. Use aggregate functions across the entire Employees table.

LEFT JOIN with counts

Return each department with the number of employees, including departments with zero employees.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Expected output

Departments with employee counts, including zero.

dept_nameemployee_count
Finance2
Operations1
Marketing0

Hints

  1. LEFT JOIN from Departments to Employees, then GROUP BY department.

RIGHT JOIN via swap

Show all departments and any employee names in them, equivalent to Employees RIGHT JOIN Departments.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Expected output

All departments listed; employees shown where present (RIGHT JOIN semantics).

dept_nameemployee_name
FinanceAlice
FinanceCarol
MarketingNULL
OperationsBob

Hints

  1. Simulate RIGHT JOIN by LEFT JOIN from Departments to Employees.

FULL OUTER JOIN emulate

Return all employee–department pairs where matched, plus departments with no employees (FULL OUTER JOIN behavior for this data).

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Expected output

Inner-joined rows plus unmatched departments.

dept_nameemployee_name
FinanceAlice
FinanceCarol
MarketingNULL
OperationsBob

Hints

  1. Combine INNER JOIN with unmatched rows from a LEFT JOIN using UNION ALL.

UNION vs UNION ALL

Show the number of rows produced when combining salaries with UNION versus UNION ALL.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Expected output

UNION removes duplicates; UNION ALL keeps them.

row_countset_type
3UNION
6UNION ALL

Hints

  1. Use UNION for distinct rows, UNION ALL to keep duplicates.

Window functions demo

For each employee, show their department's average salary and their rank by salary within the department (1 = highest).

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Expected output

Per-row window metrics alongside each employee.

dept_avg_salarydept_namedept_salary_ranknamesalary
75000Finance1Carol80000
75000Finance2Alice70000
90000Operations1Bob90000

Hints

  1. Use AVG() OVER (PARTITION BY ...) for the department average.
  2. Use DENSE_RANK() OVER (PARTITION BY ... ORDER BY ... DESC) for ranking.

View-like CTE usage

Using a CTE named EmployeeDepartments, list employee id, name, and department name (demonstrating view-like behavior).

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Expected output

CTE produces a reusable result set like a temporary view.

dept_nameidname
Finance1Alice
Operations2Bob
Finance3Carol

Hints

  1. Use WITH to define a named subquery and then select from it.

Hide duplicates with DISTINCT

List distinct department names that have at least one employee.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Expected output

Distinct department names appearing in Employees.

dept_name
Finance
Operations

Hints

  1. DISTINCT removes duplicate department names produced by the join.

Identify duplicates to delete

Identify duplicate employee rows by (name, dept_id, salary) and return only the extra duplicates (beyond the first) that you would delete.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Expected output

Rows that would be considered duplicates (none in the sample).

Hints

  1. Use ROW_NUMBER over a partition to mark duplicates and filter rn > 1.

Nth Highest Salary

2nd-Highest Distinct Salary

You are given an Employees table:

columntypenotes
idINTEGERprimary key
nameVARCHARemployee name
salaryINTEGERemployee salary
dept_idINTEGERdepartment the employee is in

Write a query that returns the 2nd-highest distinct salary across all employees.

Rules:

  • Rank salaries by distinct value, so duplicate salaries count as a single rank (e.g. if two people earn the top salary, the next lower salary is still the 2nd-highest distinct value).
  • Return exactly one row with a single column named getNthHighestSalary holding that salary.
  • If fewer than 2 distinct salaries exist, the query should return one row whose value is NULL.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000
24Dave80000
15Eve60000

Expected output

One row, one column getNthHighestSalary = 80000 (the 2nd-highest distinct salary; 90000 is highest, the tied 80000 pair is 2nd).

getNthHighestSalary
80000

Hints

  1. DENSE_RANK() (not RANK or ROW_NUMBER) collapses duplicate salaries into one rank with no gaps.
  2. Filter the ranked rows to rk = 2, then aggregate so you always return exactly one row.
  3. Wrapping the result in MAX(...) yields NULL automatically when there is no 2nd distinct salary.

Top N salaries efficiently

Top 2 Distinct Salaries

You are given an Employees table holding one row per employee, with their salary.

Write a query that returns the 2 highest distinct salary values in the company, in descending order. (The original prompt asked for the "top N" distinct salaries; here N is pinned to 2 so the question is concretely answerable.)

Requirements:

  • Treat duplicate salaries as a single value (e.g. if two employees both earn 90000, that salary counts once).
  • The result must have a single column named salary.
  • Return at most 2 rows: the highest distinct salary first, then the second-highest.
  • Order the output by salary in descending order.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000
24Dave90000
15Eve70000

Expected output

The 2 highest distinct salary values in descending order: 90000 then 80000.

salary
90000
80000

Hints

  1. Use DISTINCT so duplicate salaries collapse into one value before you rank them.
  2. ORDER BY salary DESC then LIMIT 2 gives you the top two without scanning beyond what you need.
  3. In Postgres the row cap is a literal: write LIMIT 2, never LIMIT :N.

Pandas merge equivalent (INNER JOIN)

Produce the same result as pandas merge(Employees, Departments, left_on='dept_id', right_on='id', how='inner'). Return id, name, salary, dept_id, dept_name.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Expected output

Inner join result equivalent to pandas merge with how='inner'.

dept_iddept_nameidnamesalary
1Finance1Alice70000
2Operations2Bob90000
1Finance3Carol80000

Hints

  1. Pandas merge with how='inner' maps to SQL INNER JOIN.

Pandas join equivalent (LEFT JOIN)

Produce the same result as df_employees.join(df_departments, on='dept_id', how='left'): return all employees and their dept_name if available.

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Expected output

Left join of employees to departments.

dept_nameidname
Finance1Alice
Operations2Bob
Finance3Carol

Hints

  1. Pandas DataFrame.join with how='left' maps to SQL LEFT JOIN.

Pandas concat equivalent with UNION ALL

Vertically concatenate two employee subsets without removing duplicates: (a) employees in the Finance department, and (b) employees with salary >= 80000. Return a source label, id, name, and dept_name. Use UNION ALL so an employee who appears in both subsets appears twice with different source labels.

Table Departments

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
dept_nameVARCHARNOT NULL

Sample data:

dept_nameid
Finance1
Operations2
Marketing3

Table Employees

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
nameVARCHARNOT NULL
salaryINTEGERNOT NULL
dept_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Departments(id)

Sample data:

dept_ididnamesalary
11Alice70000
22Bob90000
13Carol80000

Expected output

Union-all style vertical stacking with a source label.

dept_nameidnamesource
Finance1Alicefinance
Finance3Carolfinance
Operations2Bobhigh_salary
Finance3Carolhigh_salary

Hints

  1. Use UNION ALL to stack two SELECT results without deduplication.
  2. Make sure both SELECT branches return the same columns in the same order.