📖 Scenario: You are managing a small online bookstore database. You want to analyze the titles of books to understand their length in bytes and characters.
🎯 Goal: Build a simple MySQL query that shows the length in bytes and the number of characters for each book title in the books table.
📋 What You'll Learn
Create a table called books with columns id (integer) and title (text).
Insert exactly three books with these titles: 'SQL Basics', 'Learn MySQL', 'データベース入門' (Japanese for 'Database Introduction').
Write a SELECT query that returns the title, the byte length using LENGTH(title), and the character length using CHAR_LENGTH(title).
Order the results by id ascending.
💡 Why This Matters
🌍 Real World
Understanding text length in bytes and characters helps when storing and indexing text data, especially with multi-byte characters like Japanese.
💼 Career
Database developers and administrators often need to analyze and manipulate string data efficiently for storage and search optimization.
Progress0 / 4 steps
1
Create the books table and insert data
Create a table called books with columns id as integer and title as text. Then insert these three rows exactly: (1, 'SQL Basics'), (2, 'Learn MySQL'), and (3, 'データベース入門').
MySQL
Hint
Use CREATE TABLE to make the table and INSERT INTO to add the rows.
2
Add a SELECT query to get titles
Write a SELECT query to get the title column from the books table.
MySQL
Hint
Use SELECT title FROM books; to get the titles.
3
Add LENGTH and CHAR_LENGTH columns
Modify the SELECT query to also show LENGTH(title) as byte_length and CHAR_LENGTH(title) as char_length.
MySQL
Hint
Use LENGTH(title) AS byte_length and CHAR_LENGTH(title) AS char_length in your SELECT.
4
Order results by id
Add an ORDER BY id ASC clause to the SELECT query to sort the results by id in ascending order.
MySQL
Hint
Use ORDER BY id ASC at the end of your SELECT query.
Practice
(1/5)
1. What does the MySQL function LENGTH() return when used on a string?
easy
A. The number of words in the string
B. The number of characters in the string
C. The number of lines in the string
D. The number of bytes in the string
Solution
Step 1: Understand LENGTH() function
The LENGTH() function returns the number of bytes used by the string, not characters.
Step 2: Compare with CHAR_LENGTH()
CHAR_LENGTH() counts characters, LENGTH() counts bytes, which can differ for multibyte characters.
2. Which of the following is the correct syntax to get the number of characters in a string column named name in MySQL?
easy
A. SELECT LENGTH(name) FROM users;
B. SELECT CHAR_LENGTH(name) FROM users;
C. SELECT COUNT(name) FROM users;
D. SELECT SIZE(name) FROM users;
Solution
Step 1: Identify function for character count
CHAR_LENGTH() returns the number of characters in a string, which is what we want.
Step 2: Check syntax correctness
SELECT CHAR_LENGTH(name) FROM users; is valid syntax to get character count per row.
Final Answer:
SELECT CHAR_LENGTH(name) FROM users; -> Option B
Quick Check:
CHAR_LENGTH() counts characters [OK]
Hint: Use CHAR_LENGTH() to count characters, not LENGTH() [OK]
Common Mistakes:
Using LENGTH() instead of CHAR_LENGTH() for characters
Using COUNT() which counts rows, not string length
Using non-existent SIZE() function
3. Given a table messages with a column text containing the value 'café', what will the query SELECT LENGTH(text), CHAR_LENGTH(text) FROM messages; return if the encoding is UTF-8?
medium
A. 5, 4
B. 4, 5
C. 4, 4
D. 5, 5
Solution
Step 1: Understand UTF-8 encoding for 'café'
The character 'é' uses 2 bytes in UTF-8, so total bytes = 3 (c,a,f) + 2 (é) = 5 bytes.
Hint: UTF-8 chars may use multiple bytes; LENGTH > CHAR_LENGTH [OK]
Common Mistakes:
Assuming LENGTH and CHAR_LENGTH return same value
Ignoring multibyte characters in UTF-8
Swapping LENGTH and CHAR_LENGTH results
4. You wrote the query SELECT LENGTH('hello'), CHAR_LENGTH('hello'); but got an error. What is the likely cause?
medium
A. No error; the query is valid and returns results
B. Missing FROM clause in the query
C. Incorrect function names, LENGTH and CHAR_LENGTH do not exist
D. Using single quotes instead of double quotes
Solution
Step 1: Check syntax of the query
SELECT LENGTH('hello'), CHAR_LENGTH('hello'); is valid in MySQL and does not require FROM clause.
Step 2: Verify function existence and quotes
LENGTH and CHAR_LENGTH are valid functions; single quotes are correct for string literals.
Final Answer:
No error; the query is valid and returns results -> Option A
Quick Check:
Query syntax is valid [OK]
Hint: MySQL allows SELECT without FROM for expressions [OK]
Common Mistakes:
Thinking FROM clause is mandatory in all SELECT queries
Confusing string quotes usage
Assuming LENGTH or CHAR_LENGTH are invalid functions
5. You want to find all rows in a table users where the username column has exactly 5 characters, but some usernames contain multibyte UTF-8 characters. Which query correctly finds these rows?
hard
A. SELECT * FROM users WHERE LENGTH(username) = CHAR_LENGTH(username);
B. SELECT * FROM users WHERE LENGTH(username) = 5;
C. SELECT * FROM users WHERE CHAR_LENGTH(username) = 5;
D. SELECT * FROM users WHERE LENGTH(username) > 5;
Solution
Step 1: Understand difference between LENGTH and CHAR_LENGTH
LENGTH counts bytes, which can be more than characters for multibyte UTF-8 chars.
Step 2: Choose function that counts characters
CHAR_LENGTH counts characters correctly regardless of byte size, so use CHAR_LENGTH(username) = 5.
Final Answer:
SELECT * FROM users WHERE CHAR_LENGTH(username) = 5; -> Option C
Quick Check:
Use CHAR_LENGTH for character count in UTF-8 [OK]
Hint: Use CHAR_LENGTH to count characters, not bytes [OK]
Common Mistakes:
Using LENGTH() which counts bytes, not characters
Assuming byte count equals character count
Using incorrect comparison operators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.