Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Monthly Facility Usage

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL for Monthly Facility Usage. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Stanford University wants to understand which campus facilities are used most often by different student cohorts. Write a PostgreSQL query that ranks facilities by completed utilization events for each month and student cohort.

Requirements

  1. Join usage events to Stanford students and campus facilities.
  2. Include only completed events from January and February 2024, excluding rows with no student cohort.
  3. Aggregate utilization counts by calendar month, cohort year, and facility.
  4. Return the top five facilities per month and cohort, using utilization count descending and facility name ascending as the deterministic tie-breaker.
  5. Return results ordered by month, cohort year, rank, and facility name.

Schema

students
ColumnTypeDescription
student_idPKINTUnique Stanford student identifier
student_nameVARCHAR(100)Student full name
cohort_yearINTYear the student entered Stanford University
campus_facilities
ColumnTypeDescription
facility_idPKINTUnique facility identifier
facility_nameVARCHAR(150)Official Stanford campus facility name
facility_usage
ColumnTypeDescription
usage_idPKINTUnique facility usage event identifier
student_idINTStudent associated with the usage event
facility_idINTFacility associated with the usage event
used_atTIMESTAMPTimestamp when the facility was used
statusVARCHAR(20)Usage event status
Tablesstudentscampus_facilitiesfacility_usage
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results