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.
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?
A'98,765.4'
B'98765.4'
C'98,765.43'
D'98765.43'
✗ 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?
A'009'
B'9000'
C'9'
D'0009'
✗ 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?
ALPAD
BCONCAT
CRPAD
DFORMAT
✗ Incorrect
RPAD adds characters to the right side of a string.
What will RPAD('cat', 5, '!') return?
A'cat!!'
B'!!cat'
C'cat!'
D'cat'
✗ 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?
AFORMAT(number, 1)
BFORMAT(number, 0)
CLPAD(number, 0, ',')
DRPAD(number, 0, ',')
✗ 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.
Practice
(1/5)
1. What does the MySQL function FORMAT(12345.6789, 2) do?
easy
A. Formats the number with commas and 2 decimal places, like '12,345.68'.
B. Rounds the number to the nearest integer, like '12346'.
C. Converts the number to a string without decimals, like '12345'.
D. Adds leading zeros to make the number 2 digits long.
Solution
Step 1: Understand FORMAT function usage
The FORMAT function formats a number with commas and rounds it to the specified decimal places.
Step 2: Apply FORMAT(12345.6789, 2)
The number 12345.6789 is rounded to 2 decimals and commas are added, resulting in '12,345.68'.
Final Answer:
Formats the number with commas and 2 decimal places, like '12,345.68'. -> Option A
Quick Check:
FORMAT adds commas and decimals = A [OK]
Hint: FORMAT adds commas and rounds decimals [OK]
Common Mistakes:
Thinking FORMAT rounds to integer only
Confusing FORMAT with LPAD or RPAD
Expecting no commas in output
2. Which of the following is the correct syntax to add leading zeros to make a string 6 characters long using LPAD in MySQL?
easy
A. LPAD(123, '6', '0')
B. LPAD('123', '6', 0)
C. LPAD('123', 6)
D. LPAD('123', 6, '0')
Solution
Step 1: Recall LPAD syntax
LPAD(string, length, pad_string) pads the string on the left to the given length using pad_string.
Step 2: Check each option
LPAD('123', 6, '0') uses correct types: string '123', length 6, pad '0'. Others have wrong types or missing arguments.
Final Answer:
LPAD('123', 6, '0') -> Option D
Quick Check:
LPAD needs string, length, pad string = C [OK]
Hint: LPAD needs string, length, and pad string in order [OK]
Common Mistakes:
Using numbers instead of strings for pad character
Missing the pad string argument
Passing length as a string instead of number
3. What is the result of this query? SELECT LPAD('abc', 6, '*'), RPAD('abc', 6, '!');
medium
A. 'abc***', '!!!abc'
B. '***abc', 'abc!!!'
C. 'abc', 'abc'
D. '***abc', '!!!abc'
Solution
Step 1: Understand LPAD and RPAD behavior
LPAD pads on the left, RPAD pads on the right to reach total length 6.
Step 2: Calculate LPAD('abc', 6, '*')
'abc' length is 3, so 3 '*' are added on left: '***abc'.
Step 3: Calculate RPAD('abc', 6, '!')
Similarly, 3 '!' are added on right: 'abc!!!'.
Final Answer:
'***abc', 'abc!!!' -> Option B
Quick Check:
LPAD left stars, RPAD right exclamations = B [OK]
Hint: LPAD pads left, RPAD pads right to total length [OK]
Common Mistakes:
Confusing LPAD and RPAD directions
Not counting original string length
Expecting padding to replace characters
4. Identify the error in this query: SELECT LPAD('test', 4, '0');
medium
A. Syntax error due to missing RPAD.
B. Error because length is less than string length.
C. No error; it returns 'test'.
D. Error because pad string must be numeric.
Solution
Step 1: Check LPAD length vs string length
LPAD pads string to length 4, but 'test' length is 4, so no padding needed.
Step 2: Confirm behavior when length equals string length
LPAD returns original string if length equals string length, no error occurs.
Final Answer:
No error; it returns 'test'. -> Option C
Quick Check:
LPAD returns original if length equals string length = D [OK]
Hint: LPAD returns original if length equals string length [OK]
Common Mistakes:
Expecting error when length equals string length
Thinking pad string must be numeric
Confusing LPAD with RPAD requirements
5. You want to display a price column with 2 decimals and commas, and pad the result on the left with '#' to make it 12 characters wide. Which query achieves this?
hard
A. SELECT LPAD(FORMAT(price, 2), 12, '#') FROM products;
B. SELECT FORMAT(LPAD(price, 12, '#'), 2) FROM products;
C. SELECT RPAD(FORMAT(price, 2), 12, '#') FROM products;
D. SELECT LPAD(price, 12, '#') FROM products;
Solution
Step 1: Format price with commas and 2 decimals
Use FORMAT(price, 2) to get string like '1,234.56'.
Step 2: Pad formatted string on left to length 12 with '#'
Use LPAD(formatted_string, 12, '#') to add '#' on left if needed.
Step 3: Check options
SELECT LPAD(FORMAT(price, 2), 12, '#') FROM products; correctly formats then pads left. SELECT FORMAT(LPAD(price, 12, '#'), 2) FROM products; tries to pad number before formatting (wrong). SELECT RPAD(FORMAT(price, 2), 12, '#') FROM products; pads right, not left. SELECT LPAD(price, 12, '#') FROM products; pads number without formatting.
Final Answer:
SELECT LPAD(FORMAT(price, 2), 12, '#') FROM products; -> Option A
Quick Check:
Format first, then LPAD left padding = A [OK]
Hint: Format number first, then LPAD to pad left [OK]
Common Mistakes:
Padding before formatting number
Using RPAD instead of LPAD for left padding
Padding raw number without formatting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.