Back to problems

Top-5 Most Similar Rows Using MSE Across Multiple Features

SQL · Databricks · Hard

You have two tables, A and B. Both tables contain the same numeric feature columns, such as f1, f2, through fk, and each row has a unique identifier column, such as id. For each row in A, return the five rows from B with the greatest feature similarity. Define similarity using mean squared error across the k shared features: \[ MSE(a,b)=\frac{1}{k}\sum_{j=1}^{k}(a.f_j-b.f_j)^2 \] Requirements Produce five output rows for every A.id, including the selected B.id and its…

Checking your access…