MySQL Functions Quiz: String, Date and Numeric Practice Test
Test yourself: 11 questions and three queries on MySQL string functions, ROUND and MOD, date formatting, INTERVAL, DATEDIFF, TIMESTAMPDIFF, IF and NULLIF.
- Course: SQL study plan
- Module: Functions
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: MySQL 8 syntax on SQLite 3.49
Checkpoint — Functions is the checkpoint that closes the Functions module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: string functions for joining, cleaning, cutting and padding text; numeric functions for rounding, integer division and remainders; date types, date parts and DATE_FORMAT; date arithmetic with INTERVAL, DATEDIFF, TIMESTAMPDIFF and LAST_DAY; and the conditional functions IF, IFNULL, NULLIF, GREATEST, LEAST and FIELD.
How it works. Eleven questions and three queries. You need 70% on the questions and every query accepted to clear the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- What does
CONCATreturn when one argument is NULL, and what doesCONCAT_WSdo instead? - What do
SUBSTRING_INDEX(s, '@', 1)andSUBSTRING_INDEX(s, '@', -1)return for an email address? - What is
-7 DIV 2, and how doFLOOR,CEIL,ROUNDandTRUNCATEdiffer for negative numbers? - Why is
5 / 2equal to 2 in PostgreSQL and 2.5 in MySQL? - Which weekday is 1 in
DAYOFWEEK, and which is 0 inWEEKDAY? - What is
'2024-01-31' + INTERVAL 1 MONTH? - What is the argument order of
DATEDIFF, and ofTIMESTAMPDIFF? - Why is a half-open range better than
BETWEENfor a month ofDATETIMEvalues? - What do
NULLIF(x, 0)andFIELD(status, …)return, and what are they used for?
The three queries are a contact-card formatter that cleans names and splits emails, an invoice report that computes due dates, days overdue and a rounded late fee, and a shipment board with labels, a safe on-time rate and a custom sort order.
Common questions
What does CONCAT return when one argument is NULL?
In MySQL CONCAT returns NULL if any argument is NULL, so a missing middle name erases the whole full name. CONCAT_WS(' ', first, middle, last) skips NULL arguments, and wrapping a column in COALESCE(col, '') is the other fix.
What is the argument order of DATEDIFF and TIMESTAMPDIFF?
DATEDIFF(later, earlier) puts the later date first and returns days. TIMESTAMPDIFF(unit, earlier, later) puts the unit first and then the start date, so the order of the dates is reversed. Mixing them up flips the sign of the answer.
Why use NULLIF in a division?
x / NULLIF(y, 0) divides by NULL instead of zero when y is 0, so the result is NULL rather than an error in PostgreSQL and SQL Server. MySQL returns NULL for division by zero anyway, but the guard keeps a query portable and makes the intent visible.
Exercises
Contact cards
contacts.full_name was typed in any case with stray spaces; the first word is the first name and the last word the surname.
| id | full_name | |
|---|---|---|
| 1 | ' ada LOVELACE ' | Ada@Example.COM |
| 2 | 'grace hopper' | grace@NAVY.mil |
| 3 | 'Linus' | linus@kernel.org |
Return id, first_name and last_name — each with a capital first letter and the rest lower case — and domain, the lower-cased part of the email after @. A one-word name is both the first and the last name. Order by last_name, then id.
id | first_name | last_name | domain
2 | Grace | Hopper | navy.mil
3 | Linus | Linus | kernel.org
1 | Ada | Lovelace | example.comInvoice ageing with late fees
Report on invoices as of 2024-06-30. An invoice is due terms_days after it was issued; an unpaid one (paid_on NULL) is measured up to 2024-06-30.
| id | issued_on | terms_days | paid_on | amount |
|---|---|---|---|---|
| 1 | 2024-05-01 | 30 | NULL | 1200.00 |
| 2 | 2024-06-01 | 15 | 2024-06-10 | 500.00 |
| 3 | 2024-04-15 | 45 | 2024-06-05 | 250.00 |
| 4 | 2024-06-20 | 7 | NULL | 80.00 |
Return id, due_on, days_overdue — days from the due date to the payment date (or to 2024-06-30), never below 0 — and late_fee, 1% of the amount for every week or part-week overdue, rounded to 2 decimals. Order by days_overdue descending, then id.
id | due_on | days_overdue | late_fee
1 | 2024-05-31 | 30 | 60
3 | 2024-05-30 | 6 | 2.5
4 | 2024-06-27 | 3 | 0.8
2 | 2024-06-16 | 0 | 0Carrier scorecard
Grade each delivery carrier on its on-time rate.
| name | tier | delivered | on_time |
|---|---|---|---|
| BlueDart | express | 200 | 192 |
| Delhivery | standard | 500 | 410 |
| IndiaPost | economy | 300 | 210 |
| Ekart | standard | 0 | 0 |
| DTDC | express | 150 | 140 |
Return name, tier, rate — 100 * on_time / delivered rounded to 1 decimal, NULL when nothing was delivered — and grade: no data when the rate is NULL, excellent at 95 or above, ok at 80 or above, otherwise poor (grade on the rounded rate). Order by tier — express, standard, economy — then name.
name | tier | rate | grade
BlueDart | express | 96 | excellent
DTDC | express | 93.3 | ok
Delhivery | standard | 82 | ok
Ekart | standard | NULL | no data
IndiaPost | economy | 70 | poorIn this module: Functions
- String functions — CONCAT, SUBSTRING, TRIM and friends
- Numeric functions — ROUND, CEIL, FLOOR, MOD and division
- Dates and times — types, parts and DATE_FORMAT
- Date arithmetic — INTERVAL, DATEDIFF and TIMESTAMPDIFF
- Conditional functions — IF, IFNULL, NULLIF, GREATEST and FIELD
- Checkpoint — Functions (this lesson)
← Conditional functions — IF, IFNULL, NULLIF, GREATEST and FIELD · Aggregate functions — COUNT, SUM, AVG, MIN and MAX →