0
0
MySQLquery~20 mins

LENGTH and CHAR_LENGTH in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of LENGTH and CHAR_LENGTH
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Difference between LENGTH and CHAR_LENGTH with multibyte characters
Consider the string 'café' stored in a UTF-8 encoded MySQL database. What is the output of the following query?

SELECT LENGTH('café') AS length_bytes, CHAR_LENGTH('café') AS length_chars;
MySQL
SELECT LENGTH('café') AS length_bytes, CHAR_LENGTH('café') AS length_chars;
Alength_bytes = 4, length_chars = 4
Blength_bytes = 5, length_chars = 4
Clength_bytes = 4, length_chars = 5
Dlength_bytes = 5, length_chars = 5
Attempts:
2 left
💡 Hint
Remember that LENGTH counts bytes, and CHAR_LENGTH counts characters.
query_result
intermediate
1:30remaining
Using LENGTH and CHAR_LENGTH on ASCII string
What is the output of this query?

SELECT LENGTH('hello') AS length_bytes, CHAR_LENGTH('hello') AS length_chars;
MySQL
SELECT LENGTH('hello') AS length_bytes, CHAR_LENGTH('hello') AS length_chars;
Alength_bytes = 5, length_chars = 4
Blength_bytes = 4, length_chars = 5
Clength_bytes = 6, length_chars = 5
Dlength_bytes = 5, length_chars = 5
Attempts:
2 left
💡 Hint
ASCII characters use 1 byte each.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in LENGTH usage
Which option contains a syntax error when using LENGTH or CHAR_LENGTH functions in MySQL?
ASELECT LENGTH 'test';
BSELECT CHAR_LENGTH('test') FROM dual;
CSELECT LENGTH('test');
DSELECT LENGTH('test') FROM dual;
Attempts:
2 left
💡 Hint
Check the parentheses usage in function calls.
🧠 Conceptual
advanced
2:00remaining
Why use CHAR_LENGTH instead of LENGTH for user input?
Why is it better to use CHAR_LENGTH instead of LENGTH when validating the length of user input strings in a UTF-8 encoded MySQL database?
ABecause CHAR_LENGTH counts bytes, which is more accurate for storage size.
BBecause LENGTH counts characters, not bytes, so it is faster.
CBecause CHAR_LENGTH counts characters, not bytes, so it reflects the actual number of characters the user typed.
DBecause LENGTH counts words, which is useful for input validation.
Attempts:
2 left
💡 Hint
Think about multibyte characters and user experience.
query_result
expert
2:30remaining
Output of LENGTH and CHAR_LENGTH on emoji string
What is the output of this query?

SELECT LENGTH('👍👍') AS length_bytes, CHAR_LENGTH('👍👍') AS length_chars;

Assume the database uses UTF-8 encoding.
MySQL
SELECT LENGTH('👍👍') AS length_bytes, CHAR_LENGTH('👍👍') AS length_chars;
Alength_bytes = 8, length_chars = 2
Blength_bytes = 4, length_chars = 2
Clength_bytes = 2, length_chars = 2
Dlength_bytes = 8, length_chars = 4
Attempts:
2 left
💡 Hint
Emoji characters usually use 4 bytes each in UTF-8.