KatInterview

Cleaning Account Data in R and Selecting Top Half of Students in SQL

SQL & DataModel answer
MediumModel answerSQL & Datasqlrtidyverse

Last updated

Clean a customer CSV with ordered null-handling rules and compute two averages in tidyverse R, then write one MySQL 8 query returning the top half of students by average grade.

This problem has two independent parts. Both test whether you can apply data-quality rules in the right order and rank rows deterministically.

Part A: Account metrics in R (tidyverse)

You are given a CSV of subscription accounts with these columns:

ColumnTypeNotes
idINTmay be NULL or blank
signup_datedate (YYYY-MM-DD)
last_active_datedate (YYYY-MM-DD)
churned0 / 1 / NULL
paydecimal / NULL

Apply these rules:

  1. Remove rows whose id is NULL or blank.
  2. A NULL pay becomes 0.
  3. If churned is NULL and pay > 0, set churned = 0 (the account is still active).
  4. active_days is the number of days from signup_date to last_active_date. If last_active_date comes before signup_date, use 0.
  5. For accounts with churned = 1, last_active_date is the churn date.

Compute, on the cleaned data:

  • (a) the average active_days over all remaining accounts;
  • (b) the average pay over accounts with churned = 1.

Round both values to 2 decimals. Use idiomatic tidyverse code and briefly explain how your code enforces each rule.

Sample input:

idsignup_datelast_active_datechurnedpay
12025-01-102025-03-05199
22025-02-012025-02-20NULL15
32025-01-152025-01-180NULL
NULL2025-02-102025-02-12150
42025-02-112025-02-05125

Part B: Top half of students in MySQL 8.0

Schema:

  • Teachers(id PK, name, classroom)
  • Students(id PK, name, primary_teacher_id FK -> Teachers.id)
  • Assignments(id PK, teacher_id FK -> Teachers.id)
  • Grades(student_id FK -> Students.id, assignment_id FK -> Assignments.id, grade DECIMAL(4,1), PK(student_id, assignment_id))

Write one query that:

  1. computes each student's avg_score over all their grades;
  2. excludes students with no completed (graded) assignments;
  3. keeps only the top CEIL(N/2) students by average, where N is the number of students with at least one grade; ties are broken by student_id ascending;
  4. returns student_id, name, avg_score (rounded to 1 decimal) and a dense rank over the selected students ordered by avg_score DESC, student_id ASC;
  5. keeps working correctly as students and grades are added, so nothing may be hard-coded.

Sample data:

Students

idnameprimary_teacher_id
1Bobby4
2Susie8
3Deborah8
9BruceNULL

Grades

student_idassignment_idgrade
11100.0
1250.0
21100.0
22100.0
3140.0
328.0
9165.0
9265.0

(Teachers has rows 4 / Mr. Feeny / 301 and 8 / Mr. Cooper / 260; Assignments has rows 1 -> teacher 4 and 2 -> teacher 8.)

Hints

Hint 1

In Part A, rule order matters: the `churned` imputation depends on `pay` already being filled in, so impute `pay` first. Read `id` as text so whitespace-only values can be detected before converting to integer.

Hint 2

For metric (b), `churned == 1` yields NA for rows with an unknown status, which breaks subsetting. Use `churned %in% 1` or `which()` so unknown status counts as not churned.

Hint 3

In Part B, you cannot filter on a window function in WHERE, so compute `ROW_NUMBER()` and `COUNT(*) OVER ()` in a CTE and filter in the outer query. Do not join `Teachers`, or students with a NULL `primary_teacher_id` disappear.