Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Advanced SQL With CTEs

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

Your question is Advanced SQL With CTEs. 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

Business Context

Microsoft Teams administrators need a monthly view of license utilization and employee engagement across departments.

Task

Write a PostgreSQL query using multiple CTEs to produce adoption metrics for January and February 2025. Include departments with active licenses even when they have no Teams activity.

Requirements

  1. Count active licensed employees and distinct active users by department and month.
  2. Calculate license utilization as active users divided by active licensed employees, expressed as a percentage.
  3. Calculate month-over-month percentage change in active users using LAG.
  4. Rank departments within each month by utilization, using active users as a tie-breaker.
  5. Treat activity from inactive licenses as invalid and avoid counting duplicate activity events.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee display name
departmentVARCHAR(50)Employee department
teams_licenses
ColumnTypeDescription
license_idPKINTUnique license record identifier
employee_idINTEmployee assigned the license
license_typeVARCHAR(50)Microsoft Teams license tier
license_statusVARCHAR(20)License lifecycle status
teams_activity
ColumnTypeDescription
activity_idPKINTUnique activity event identifier
employee_idINTEmployee generating the activity
activity_dateDATEDate of the Teams activity
activity_typeVARCHAR(40)Type of Teams activity
Tablesemployeesteams_licensesteams_activity
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results