Home > Enterprise >  The sum of two columns and only show the third column
The sum of two columns and only show the third column

Time:11-14

I am trying to write and execute a SQL query that returns the top three records with the highest "score", where "score" is the sum of two columns (lets call them X and Y). The result should have one column named score.

Here is what I did

%%sql
select X,Y,(X   Y) as score from survey
ORDER BY score DESC
LIMIT 3

I got the right answer but I only want the score column, not the x and y column too. Done. X Y score 4 9 13 4 8 12 3 7 10

CodePudding user response:

SELECT X, Y, (X Y) ... 

gives the 3 columns since you have SELECT 3 things. Instead just SELECT what you need in your case,

SELECT (X   Y) as score from survey
ORDER BY score DESC
LIMIT 3
  • Related