Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Multi-Concept Query

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

Your question is SQL Multi-Concept Query. Start with the requirements and the four 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

Applied Data Finance needs a servicing report for loans originated through its lending platform. Write a PostgreSQL query that identifies loans with the second-highest distinct principal amount within each product and combines borrower, product, and payment information.

Requirements

  1. Rank distinct loan principal amounts within each product and retain every loan tied for second place.
  2. Display each origination date as YYYY-MM-DD using PostgreSQL date formatting.
  3. Return the borrower name, product name, principal amount, total non-null payments, and the most recent payment date and amount.
  4. Include loans without payments, showing a total payment amount of 0 and NULL for latest payment fields.
  5. Sort by product name, then loan ID.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique borrower identifier
customer_nameVARCHAR(100)Borrower full name
emailVARCHAR(255)Borrower email address
products
ColumnTypeDescription
product_idPKINTUnique lending product identifier
product_nameVARCHAR(100)Name of the Applied Data Finance lending product
loans
ColumnTypeDescription
loan_idPKINTUnique loan identifier
customer_idINTReferences customers.customer_id
product_idINTReferences products.product_id
principal_amountNUMERIC(12,2)Approved loan principal
origination_dateDATEDate the loan was originated
payments
ColumnTypeDescription
payment_idPKINTUnique payment identifier
loan_idINTReferences loans.loan_id
payment_dateDATEDate the payment was received
payment_amountNUMERIC(12,2)Payment amount received
Tablescustomersproductsloanspayments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results