Search code examples
sqloracleoracle11goracle10g

Using Case Statement in SQL


I have been learning SQL,and I was trying to learn about the CASE on Oracle LiveSQL:

This is the query I am trying to use:

SELECT FIRST_NAME,LAST_NAME, CASE
WHEN SALARY>10000 THEN 'Salary is greater than 10000'
WHEN SALARY<10000 THEN 'Salary is less than 10000'
ELSE 'Salary is equal to 10000'
END AS 'SALARY STATEMENT'
FROM hr.employees;

This query gives me the output as:

ORA-00923: FROM keyword not found where expected

Can anyone explain what's wrong?


Solution

  • Oracle database object names, when escaped, should appear inside double quotes, not single quotes. Single quotes denote string literals, not aliases. The following version should work:

    SELECT FIRST_NAME, LAST_NAME,
           CASE WHEN SALARY > 10000 THEN 'Salary is greater than 10000'
                WHEN SALARY < 10000 THEN 'Salary is less than 10000'
                ELSE 'Salary is equal to 10000'
           END AS "SALARY STATEMENT"
    FROM hr.employees;