sqloracle-databaseformattinganonymize

Oracle SQL - How to format output without showing the explicit value


I'm making a query on a table of exam results. A student passes the exam if the score is above 50 %. I want the output of the query to show 1 if the exam score is above 50% (i.e. exam is passed) and 0 if the score is below 50% (i.e. exam s failed). I do not want the actual exam score to be shown.

    TURN THIS               INTO                 THIS

Exam no. Score Exam no. Score 1 37% => 1 0 2 84% 2 1 3 76% 3 1

How do I do this? I've found several articles and pages on formatting the result with spaces, linebraks, renaming of coloumns etc., but I cannot figure out how to use "representational values".

Is there a way to write the query so that the result will look like the one in my example above?


Solution

  • You can use a CASE expression:

    select exam_no, 
           case 
              when score >= 50 then 1
              else 0
           end as score
    from the_table;