Math, Text & Date FunctionsOptional for Computer Science
These come up in a shape of their own: write the output of the following. There is no table to read and no query to design — just a function, an argument, and a value you either know or you do not.
📝Informatics Practices only
The single-row function families are on the Informatics Practices syllabus. Computer Science students will not be asked for them, so treat this page as optional — though round() is worth knowing whatever you take.
1Math functions
A select with no from is perfectly legal — it just evaluates whatever you give it and hands back one row.
MySQL command line client
mysql> select mod(17, 5), power(2, 5), sqrt(144);
+------------+-------------+-----------+
| mod(17, 5) | power(2, 5) | sqrt(144) |
+------------+-------------+-----------+
| 2 | 32 | 12 |
+------------+-------------+-----------+
1 row in set (0.00 sec)
round() and truncate() are the pair examiners like, because they look similar and are not:
round(…, 2) rounds to two decimals — 1234.57. A negative second argument rounds to the left of the point: round(…, -2) gives 1200. And truncate() does not round at all, it chops — 1234.56, even though the next digit is a 7.
mid(str, 4, 4) starts at the fourth character and takes four of them. SQL counts from 1, not from 0 as Python does — position 4 of LambdaLab is b, so the answer is bdaL. Getting this off by one is the commonest way to lose the mark.
instr() returns 7, not 1. The text does begin with Lam, but the search is for Lab exactly, and the first place that appears is position 7. A function returns 0 when the text is not found at all.
dayname() and monthname() give words; dayofmonth() gives the number, exactly as day() does. Republic Day 2024 really was a Friday — the server worked that out, it was not looked up.
💡curdate() and now()
curdate() gives today’s date and now() the date and time. Their output is deliberately not printed here, because it would be wrong tomorrow. In an exam, write the format — YYYY-MM-DD and YYYY-MM-DD HH:MM:SS — rather than a specific value.
✏️ Quick Check
What is the output of mid('LambdaLab', 4, 4)?
✏️ Quick Check
What does round(1234.567, -2) return?
✏️ Quick Check
What is the difference between round(1234.567, 2) and truncate(1234.567, 2)?