How to calculate the average value of a row in SQL?

Hello, I wanted to know if we can find the average value of a row instead of a column for a select number of columns, and if yes, then how to do that. Here’s a brief example to make y’all understand what I mean:

Say I have a table of commodity prices (gold, silver, oil, etc) with maybe 10 columns, some containing names, dates, etc. And amongst those columns, say I have 4 similar columns which show the price of a commodity for 4 consecutive years. I want to find the average price of a commodity say Gold spread across the 4 years. Is it possible to find the average of the row containing Gold data only for those 4 specific columns?

@dsonigladiator,

Welcome to the forums!

You should be able to achieve this by selecting the sum of your columns divided by the number of columns:

SELECT (year_1 + year_2 + year_3 + year_4) / 4 AS average_price
FROM table_name;

Depending on your RDBMS you may have to divide by a float (such as 4.0) to avoid integer division if for whatever reason the prices are listed as integers.

1 Like