001Level 1

Chapter 8 · Window Functions

55. Highest and Lowest Paid Doctor per Department

Hard40 XPHealthcare

For each doctor, show their department name, salary, the name of the highest-paid doctor in their department (top_earner), and the name of the lowest-paid (lowest_earner). Use FIRST_VALUE and LAST_VALUE window functions. Order by department name, salary descending.

Tables

  • PKidINT
  • first_nameTEXT
  • last_nameTEXT
  • specialtyTEXT
  • FKdepartment_id→ departments.idINT
  • emailTEXT
  • phoneTEXT
  • hire_dateTEXT
  • salaryREAL
  • PKidINT
  • nameTEXT
  • floorINT
  • FKhead_doctor_id→ doctors.idINT
  • budgetREAL

Need help?

Ask Sol

AI tutor — looks at your current query and nudges you, without spoiling the answer.

Solution

Try the hints first — solving it yourself is what makes it stick.

Peek at the solution?

You can still earn the XP afterwards by submitting a working query — but you'll learn more if you give it one more try.

Chapter 8 · Window Functions

0/62
  1. 1Rank Products by Price Within Category
  2. 2Customer Order Sequence Numbers
  3. 3Rank Products by Revenue Within Category
  4. 4Running Total of Payments
  5. 5Days Between Consecutive Customer Orders
  6. 6Category Share of Total Revenue
  7. 7Cumulative Monthly Revenue
  8. 8Cumulative Loan Disbursements by Month
  9. 9Rank Doctors by Salary Within Specialty
  10. 10Cumulative Billing Report
  11. 11Doctor Salary Quartiles with NTILE
  12. 12Compare Each Bill to the Previous One
  13. 13Dense Rank Patients by Visit Frequency
  14. 14Monthly Revenue with Month-over-Month Change
  15. 15Running Total of Daily Revenue
  16. 16Compare Order Value to Previous Order
  17. 17Dense Rank Customers by Total Spending
  18. 183-Month Moving Average of Order Value
  19. 19Price Rank per Category ID
  20. 20Running Total of Daily Order Revenue
  21. 21Customer Order Sequence Number
  22. 22Top 3 Products per Category by Revenue
  23. 237-Day Moving Average of Order Amounts
  24. 24Rank Customers by Order Value with RANK and DENSE_RANK
  25. 25Rank Accounts by Balance Within Type
  26. 26Running Transaction Total Per Account
  27. 27Identify Duplicate Customer Emails
  28. 28Previous Transaction Amount
  29. 29Account Balance Quartiles
  30. 30Dense Rank Customers by Total Balance
  31. 31Each Account as Percentage of Customer Total
  32. 32Next Payment Date for Each Loan Payment
  33. 333-Transaction Moving Average
  34. 34Percent Rank of Account Balances
  35. 35Highest Balance Account per Type
  36. 36Top 3 Accounts by Balance Per Type
  37. 37Detect Payment Gaps Over 60 Days
  38. 38Account Balance Quartile Classification
  39. 39Running Balance After Each Transaction
  40. 40Dense Rank Transactions by Amount per Account Type
  41. 41Rank Customers by Net Worth
  42. 42Detect Three Consecutive Deposits
  43. 43Year-over-Year Account Balance Growth
  44. 44Per-Customer 3-Transaction Moving Average
  45. 45Account Balance Volatility Ranking
  46. 46NTILE Customer Segmentation
  47. 47Rank Doctors by Salary Within Department
  48. 48Running Total of Billing Amounts
  49. 49Days Between Consecutive Visits
  50. 50Top Billing Doctor per Department
  51. 517-Day Moving Average of Daily Billing
  52. 52Latest Visit per Patient Using ROW_NUMBER
  53. 53Percentile Rank of Doctor Salaries
  54. 54Moving Average of Daily Bill Totals
  55. 55Highest and Lowest Paid Doctor per Department
  56. 56Cumulative Visit Count per Doctor Over Time
  57. 57Days Until Next Visit per Patient
  58. 58Each Doctor's Share of Department Salary Budget
  59. 59Rank Doctors by Patient Count Within Each Department
  60. 60Visit Gap Analysis with Window Functions
  61. 61Doctor Workload Percentile Ranking
  62. 62Cumulative Abnormal Lab Results per Patient Over Time
See the full learning path →