Challenge - 5 Problems
Length Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Difference between LENGTH and CHAR_LENGTH
Given a table words with a column
1.
2.
Assume UTF-8 encoding.
word containing the value 'café', what is the result of these queries?1.
SELECT LENGTH(word) FROM words;2.
SELECT CHAR_LENGTH(word) FROM words;Assume UTF-8 encoding.
SQL
CREATE TABLE words (word VARCHAR(10)); INSERT INTO words VALUES ('café');
Attempts:
2 left
💡 Hint
LENGTH counts bytes, CHAR_LENGTH counts characters.
✗ Incorrect
The character 'é' uses 2 bytes in UTF-8, so LENGTH counts 5 bytes total, while CHAR_LENGTH counts 4 characters.
❓ query_result
intermediate2:00remaining
Using LENGTH and CHAR_LENGTH with ASCII text
What is the output of these queries on a table
texts with a single row where text = 'hello'?SQL
CREATE TABLE texts (text VARCHAR(10)); INSERT INTO texts VALUES ('hello');
Attempts:
2 left
💡 Hint
ASCII characters use 1 byte each.
✗ Incorrect
Each ASCII character is 1 byte, so LENGTH and CHAR_LENGTH both return 5 for 'hello'.
📝 Syntax
advanced2:00remaining
Identify the syntax error in LENGTH usage
Which of the following SQL queries will cause a syntax error?
Attempts:
2 left
💡 Hint
LENGTH is a function and needs parentheses with an argument.
✗ Incorrect
Option C uses LENGTH without parentheses or argument, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing string length checks
You want to find rows in a table
messages where the message length is exactly 10 characters. Which query is more efficient and why?Attempts:
2 left
💡 Hint
Consider byte vs character length and indexing.
✗ Incorrect
LENGTH is usually faster because it counts bytes directly; CHAR_LENGTH may be slower due to character decoding.
🧠 Conceptual
expert2:00remaining
Understanding LENGTH and CHAR_LENGTH with multi-byte characters
Consider a table
texts with a column content containing the string '😊😊'. What are the results of SELECT LENGTH(content), CHAR_LENGTH(content) FROM texts; assuming UTF-8 encoding?Attempts:
2 left
💡 Hint
Each emoji uses 4 bytes in UTF-8.
✗ Incorrect
Each '😊' emoji is 4 bytes in UTF-8, so LENGTH counts 8 bytes total, CHAR_LENGTH counts 2 characters.