Recall & Review
beginner
What does the FORMAT function do in MySQL?
The FORMAT function formats a number with a specified number of decimal places and adds commas as thousand separators. For example, FORMAT(12345.678, 2) returns '12,345.68'.
Click to reveal answer
beginner
How does LPAD work in MySQL?
LPAD adds characters to the left side of a string until it reaches a specified length. For example, LPAD('7', 3, '0') returns '007'.
Click to reveal answer
beginner
What is the difference between LPAD and RPAD?
LPAD pads characters on the left side of a string, while RPAD pads characters on the right side. Both make the string reach a specified length.
Click to reveal answer
beginner
Write a query to format the number 1234567.891 to 2 decimal places with commas.
SELECT FORMAT(1234567.891, 2); -- Result: '1,234,567.89'
Click to reveal answer
beginner
Write a query to pad the string 'abc' to length 6 by adding '*' on the right side.
SELECT RPAD('abc', 6, '*'); -- Result: 'abc***'
Click to reveal answer
What will FORMAT(98765.4321, 1) return?
✗ Incorrect
FORMAT adds commas and rounds to the specified decimal places. Here, 1 decimal place rounds 4321 to 4.
What does LPAD('9', 4, '0') return?
✗ Incorrect
LPAD pads on the left with '0' until length 4, so '9' becomes '0009'.
Which function pads characters on the right side of a string?
✗ Incorrect
RPAD adds characters to the right side of a string.
What will RPAD('cat', 5, '!') return?
✗ Incorrect
RPAD pads 'cat' on the right with '!' until length 5, resulting in 'cat!!'.
If you want to format a number without any decimal places but with commas, which FORMAT call is correct?
✗ Incorrect
FORMAT(number, 0) formats the number with commas and zero decimal places.
Explain how FORMAT works in MySQL and give an example.
Think about how money amounts are shown with commas and decimals.
You got /4 concepts.
Describe the difference between LPAD and RPAD with examples.
Imagine adding zeros before or after a number to make it longer.
You got /4 concepts.