How do you compute a linear regression slope and intercept, or a weighted average, in SQL with aggregate functions?

Quick Overview

Asks how to compute a least-squares slope and intercept, or a weighted average, inside SQL using only aggregate functions. Tests closed-form regression formulas, built-in regression aggregates, NULL and integer-division pitfalls, and per-group or rolling fits.

How do you compute a linear regression slope and intercept, or a weighted average, in SQL with aggregate functions?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How would you compute a linear regression slope and intercept, or a weighted average, directly in SQL using aggregate functions? Assume a table `points` with numeric columns `x` and `y` and a weight column `w`. ```hint Think in sums Write the least-squares slope and intercept in terms of counts and sums of products, then ask how many passes over the table those quantities need. ``` ```hint Test the edge cases Check what your query does with integer columns, with a NULL in only one of the columns, and when every `x` value is the same. ``` ### Clarifying Questions to Ask - Which database engine is this, and does it have regression aggregates such as `REGR_SLOPE`? - Should rows with a NULL in `x`, `y` or `w` be dropped, and should every sum skip the same rows? - Is one line for the whole table enough, or is a fit needed per segment or over a moving window? ### What a Strong Answer Covers - The closed-form least-squares formulas written as aggregates, and a weighted average with a guard for zero total weight - A query that runs as written, which rules out reusing a select-list alias inside the same SELECT - Consistent handling of NULLs, integer division and a zero denominator - Built-in regression aggregates, their argument order, and a fallback for engines that lack them - How to fit per group or over a window ### Follow-up Questions - How would you compute a weighted least-squares line in SQL? - The `x` values are Unix timestamps around 1.7 billion. What goes wrong with the one-pass formula in floating point, and how do you fix it? - How would you add R² to the same query? - How would you compute a 30-day rolling slope for each user?

Overview: Asks how to compute a least-squares slope and intercept, or a weighted average, inside SQL using only aggregate functions. Tests closed-form regression formulas, built-in regression aggregates, NULL and integer-division pitfalls, and per-group or rolling fits.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How would you compute a linear regression slope and intercept, or a weighted average, directly in SQL using aggregate functions? Assume a table points with numeric columns x and y and a weight column w.

Clarifying Questions to Ask Guidance

  • Which database engine is this, and does it have regression aggregates such as REGR_SLOPE ?
  • Should rows with a NULL in x , y or w be dropped, and should every sum skip the same rows?
  • Is one line for the whole table enough, or is a fit needed per segment or over a moving window?

What a Strong Answer Covers Guidance

  • The closed-form least-squares formulas written as aggregates, and a weighted average with a guard for zero total weight
  • A query that runs as written, which rules out reusing a select-list alias inside the same SELECT
  • Consistent handling of NULLs, integer division and a zero denominator
  • Built-in regression aggregates, their argument order, and a fallback for engines that lack them
  • How to fit per group or over a window

Follow-up Questions Guidance

  • How would you compute a weighted least-squares line in SQL?
  • The x values are Unix timestamps around 1.7 billion. What goes wrong with the one-pass formula in floating point, and how do you fix it?
  • How would you add R² to the same query?
  • How would you compute a 30-day rolling slope for each user?