0
0
SQLquery~20 mins

LENGTH and CHAR_LENGTH in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Length Mastery
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
Given a table words with a column 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é');
A1. 4 2. 4
B1. 4 2. 5
C1. 5 2. 5
D1. 5 2. 4
Attempts:
2 left
💡 Hint
LENGTH counts bytes, CHAR_LENGTH counts characters.
query_result
intermediate
2: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');
ALENGTH(text) = 5, CHAR_LENGTH(text) = 5
BLENGTH(text) = 6, CHAR_LENGTH(text) = 5
CLENGTH(text) = 5, CHAR_LENGTH(text) = 6
DLENGTH(text) = 6, CHAR_LENGTH(text) = 6
Attempts:
2 left
💡 Hint
ASCII characters use 1 byte each.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in LENGTH usage
Which of the following SQL queries will cause a syntax error?
ASELECT LENGTH('test') + 1 FROM dual;
BSELECT LENGTH('test') FROM dual;
CSELECT LENGTH FROM dual;
DSELECT LENGTH('test') AS len FROM dual;
Attempts:
2 left
💡 Hint
LENGTH is a function and needs parentheses with an argument.
optimization
advanced
2: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?
ASELECT * FROM messages WHERE CHAR_LENGTH(message) = 10;
BSELECT * FROM messages WHERE LENGTH(message) = 10;
CSELECT * FROM messages WHERE LENGTH(message) = CHAR_LENGTH(message) AND LENGTH(message) = 10;
DSELECT * FROM messages WHERE LENGTH(message) > 9 AND LENGTH(message) < 11;
Attempts:
2 left
💡 Hint
Consider byte vs character length and indexing.
🧠 Conceptual
expert
2: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?
ALENGTH = 8, CHAR_LENGTH = 2
BLENGTH = 2, CHAR_LENGTH = 8
CLENGTH = 4, CHAR_LENGTH = 2
DLENGTH = 2, CHAR_LENGTH = 2
Attempts:
2 left
💡 Hint
Each emoji uses 4 bytes in UTF-8.