JournalSQLFunctions and dates3 min

Ways of using CASE with SELECT statement

The CASE statement always goes in the SELECT clause.

  • CASE
  • WHEN
  • THEN
  • ELSE

Based on Lesson 6.29 Video: CASE Statements:

CASE - Expert Tip

The CASE statement always goes in the SELECT clause.

  • CASE must include the following components: WHEN, THEN, and END. ELSE is an optional component to catch cases that didn’t meet any of the other previous CASE conditions.
  • You can make any conditional statement using any conditional operator (like WHERE) between WHEN and THEN. This includes stringing together multiple conditional statements using AND and OR.
  • You can include multiple WHEN statements, as well as an ELSE statement again, to deal with any unaddressed conditions.

Syntax

CASE 
      WHEN condition_1  THEN result_1
     WHEN condition_2  THEN result_2
     [WHEN ...]
     [ELSE result_n]
END

Example - Sample Table

 a
---
 1
 2
 3

Code:

SELECT a,
  CASE WHEN a = 1 THEN 'one'
       WHEN a = 2 THEN 'two'
       ELSE 'other'
  END
FROM test;

or Alternatively you can write that code:

SELECT a,
  CASE a WHEN 1 THEN 'one'
         WHEN 2 THEN 'two'
         ELSE 'other'
  END
FROM test;

Result:

a | case
--- + -------
 1 | one
 2 | two
 3 | Others

Example - Employee Table

 empno | emp_first_name  |  emp_last_name  |    designame    |  dt_birth  | mngr_no |  dt_join   |  salary  | commission | deduction | deptno
-------+-----------------+-----------------+-----------------+------------+---------+------------+----------+------------+-----------+--------
  8328 | DAMAS           | FORK            | SALESMAN        | 1983-10-12 |    8743 | 2006-06-14 |  7000.00 |    1500.00 |    600.00 |     25
  8450 | HARRY           | DIGGA           | MANAGER         | 1975-09-05 |    8640 | 1999-09-15 | 20000.00 |            |   1200.00 |     15
  8425 | JONE            | KALE            | CLERCK          | 1983-08-06 |    8653 | 2004-02-03 | 11000.00 |            |    700.00 |     45
  8640 | ZOLE            | NELSON          | PRESIDENT       | 1970-08-03 |         | 1996-03-28 | 35000.00 |            |   1200.00 |     15
  8639 | JAMES           | PETRO           | SALESMAN        | 1985-04-15 |    8653 | 2007-11-13 |  9000.00 |    1700.00 |    650.00 |     15
  8744 | DAWIZ           | DONALD          | CLERCK          | 1980-03-13 |    8743 | 2002-05-12 | 10000.00 |            |    700.00 |     35
  8752 | FARIN           | JAYOS           | MANAGER         | 1972-10-12 |    8640 | 1997-07-14 | 22500.00 |            |    900.00 |     25
  8743 | SCOTT           | NATIM           | OFFICER         | 1975-03-14 |    8861 | 2000-05-23 | 26000.00 |            |   1400.00 |     15
  8892 | WALLCOT         | HILL            | SALESMAN        | 1982-11-15 |    8653 | 2001-12-16 |  8000.00 |    1200.00 |    650.00 |     25
  8861 | TURPEO          | DOGS            | MANAGER         | 1975-07-06 |    8640 | 1998-09-17 | 21000.00 |            |   1000.00 |     25
  8875 | CAMLI           | PALIN           | CLERCK          | 1973-12-17 |    8653 | 1999-06-18 | 11000.00 |            |    600.00 |     45
  8869 | FASCO           | STEAVE          | ANALYTICS       | 1981-02-18 |    8752 | 2002-12-24 | 18000.00 |            |    800.00 |     15
  8950 | CADRIM          | KATEN           | CLERCK          | 1976-10-17 |    8653 | 2003-06-16 | 11500.00 |            |    500.00 |     25
  8653 | DANON           | FORGE           | OFFICER         | 1971-07-02 |    8752 | 1994-10-17 

Code:

SELECT salary,
     CASE WHEN department_id =90 THEN 'High Salary'             
	 WHEN department_id =100 THEN '2nd grade salary'            
	 ELSE 'Low Salary'        
	 END 
	 AS salary_status       
	 FROM employees       
	 LIMIT 15;

Result:

  salary  |  salary_status   
----------+------------------
 24000.00 | High Salary
 17000.00 | High Salary
 17000.00 | High Salary
  9000.00 | Low Salary
  6000.00 | Low Salary
  4800.00 | Low Salary
  4800.00 | Low Salary
  4200.00 | Low Salary
 12000.00 | 2nd grade salary
  9000.00 | 2nd grade salary
  8200.00 | 2nd grade salary
  7700.00 | 2nd grade salary
  7800.00 | 2nd grade salary
  6900.00 | 2nd grade salary
 11000.00 | Low Salary
(15 rows)

References: