CASE
Categorize students based on their points using
CASE WHEN
conditional statement
SELECT *, 
    CASE 
      	WHEN points>90 THEN "Brilliant"
      	WHEN points>80 THEN "Gold"
      	WHEN points>60 THEN "Silver"
      	ELSE "Lazy"
    END as 'class'
FROM `students`
ORDER BY points DESC
Conditionally checking columns Using
CASE
SELECT * 
FROM `students` 
WHERE 
    (CASE
        WHEN LENGTH(mail) THEN mail
        ELSE mail2
    END) LIKE '%gmail.com%'
ORDER BY points
CASE
in
ORDER BY
clause (if the current day of month is odd, orders by
last_name
, otherwise orders by
first_name
SELECT *
FROM students
ORDER BY (
    CASE DAY(CURDATE())%2
      	WHEN 0 THEN first_name
      	WHEN 1 THEN last_name
    END
) DESC
by Valeri Tandilashvili
4 years ago
MySQL
CASE
1
Pro tip: use ```triple backticks around text``` to write in code fences