PostgreSQL:
select
id,
(SUM(score) - MAX(score) - MIN(score)) / (COUNT(score) - 2) AS avg_score
from (select
id,
UNNEST(array[score1, score2, score3, score4, score5, score6, score7]) AS score
from score_table) AS scores_per_row
group by id
order by id;
array function bundles all the scores into an array. Then, UNNEST flattens the array into rows, making it easier to calculate the sum, maximum, and minimum values.Output: