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
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Triangle Judgement | LeetCode | Easy | Solve Triangle Judgement on LeetCode (opens in a new tab) | |||
| 2 | Fix Names in a Table | LeetCode | Easy | Solve Fix Names in a Table on LeetCode (opens in a new tab) | |||
| 3 | Rising Temperature | LeetCode | Easy | Solve Rising Temperature on LeetCode (opens in a new tab) | |||
| 4 | Group Sold Products By The Date | LeetCode | Easy | Solve Group Sold Products By The Date on LeetCode (opens in a new tab) | |||
| 5 | Monthly Transactions I | LeetCode | Medium | Solve Monthly Transactions I on LeetCode (opens in a new tab) | |||
| 6 | Confirmation Rate | LeetCode | Medium | Solve Confirmation Rate on LeetCode (opens in a new tab) |