Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 740491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:31:19+00:00 2026-05-14T08:31:19+00:00

I have test_scores table with following fields: Table schema: id (number) score1 (number) score2

  • 0

I have test_scores table with following fields:

Table schema:

  id (number)
  score1  (number)
  score2  (number)
  score3  (number)
  score4  (number)

Sample data:

id score1 score2 score3 score4
1  10     05      30    50
2  05     15      10    00
3  25     10      05    15

Expected result set:

id col_name col_value
1  score4   50
2  score2   15
3  score1   25

What is a good SQL for this?(I am using MySQL.)

Original requirement didn’t include the col names in the rowset. I was able to get the result using following SQL:

SELECT   A.id, MAX(A.score) AS max_score
FROM     (
            SELECT id, score1 as score FROM test_scores UNION
            SELECT id, score2 as score FROM test_scores UNION
            SELECT id, score3 as score FROM test_scores UNION
            SELECT id, score4 as score FROM test_scores
          ) AS A
GROUP BY A.id

But with the introduction of col_name in the result-set I am stumped..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:31:19+00:00Added an answer on May 14, 2026 at 8:31 am

    I solved this problem using the following SQL:

    SELECT id, GREATEST(score1, score3, score3, score4) AS col_value,
           CASE GREATEST(score1, score3, score3, score4) 
             WHEN score1 THEN 'score1'
             WHEN score2 THEN 'score2'
             WHEN score3 THEN 'score3'
             WHEN score4 THEN 'score4'
           END AS col_name
    FROM test_scores
    
    Please let me know if there is a better solution.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have a table that stores data from the county appraisal district. I want
So my situation is this. I have two tables. A main table where data
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
I have a table which stores test results like this: user | score |
I have the following three tables in a MySQL database in order to give
I have the following Django Model which retrieves 3 records from a database. The
I have a script that appends some rows to a table. One of the
Let say I have the following desire, to simplify the IConvertible's to allow me
I have a table I use for high scores of an iPhone game. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.