Skip to content
We areTOP 1%
DBMS & SQL · Intermediate · Topic 4 of 4

SQL: CASE, String & Date Functions

6 questions.
Your progress0%
1st Visit
0/6
2nd Visit
0/6
Revise
0/6

Overview

Many SQL problems need values to be transformed before they are compared or grouped. CASE WHEN ... THEN ... ELSE ... END is SQL's if-else expression; placed inside SUM or AVG it becomes conditional aggregation, which computes several filtered totals or a percentage in a single pass. String functions include UPPER, LOWER, SUBSTRING, CONCAT, LENGTH, TRIM and REPLACE, together with pattern matching through LIKE or REGEXP. Date functions extract parts (YEAR, MONTH), format dates and do arithmetic (DATEDIFF, DATE_ADD). Function names differ between engines, for example DATE_FORMAT in MySQL and TO_CHAR in PostgreSQL, so state which dialect you use.

-- MySQL
SELECT DATE_FORMAT(order_date, '%Y-%m') AS month,
       COUNT(*) AS orders,
       SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled
FROM orders
GROUP BY month;

Questions

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account