Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: Complex Expression Result

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

Your question is SQL: Complex Expression Result. 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

HCA Healthcare's authorization audit needs a derived score for each completed request code. For a code such as 367, calculate 3^1 + 6^2 + 7^3 = 382, where each digit is raised to its one-based position.

Write a PostgreSQL query that joins HCA request, patient, and facility data, calculates the score, and ranks requests within each active facility.

Requirements

  1. Include only requests with status = 'Completed', an active facility, and a three-digit numeric request_code.
  2. Split each code into digits and raise each digit to its one-based position.
  3. Return the request, patient, facility, calculated score, and descending score rank within the facility.
  4. Use CTEs and a window function, and sort by facility name, score descending, and request ID.

Representative data

request_idrequest_codestatusfacility_id
101367Completed1
102153Completed1
103407Completed2
105222Pending3
107999Completed4
11212Completed1

Schema

authorization_requests
ColumnTypeDescription
request_idPKINTUnique authorization request identifier
patient_idINTReferenced patient
facility_idINTReferenced HCA Healthcare facility
request_codeVARCHAR(20)Authorization code to score
statusVARCHAR(20)Current request status
patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
patient_nameVARCHAR(100)Patient display name
facilities
ColumnTypeDescription
facility_idPKINTUnique facility identifier
facility_nameVARCHAR(120)HCA Healthcare facility name
is_activeBOOLEANWhether the facility is active for reporting
Tablesauthorization_requestspatientsfacilities
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results