0
0
MySQLquery~20 mins

SUBSTRING and LEFT/RIGHT in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Substring Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this SUBSTRING query?
Consider the following query on a table products with a column product_code containing the value 'ABCD1234'. What will this query return?

SELECT SUBSTRING(product_code, 2, 3) AS result FROM products WHERE product_code = 'ABCD1234';
MySQL
SELECT SUBSTRING(product_code, 2, 3) AS result FROM products WHERE product_code = 'ABCD1234';
A'BCD'
B'ABC'
C'BCD1'
D'BCD12'
Attempts:
2 left
💡 Hint
Remember SUBSTRING starts at the given position and extracts the specified length.
query_result
intermediate
2:00remaining
What does LEFT() return here?
Given a table users with a column username containing 'charlie123', what is the result of this query?

SELECT LEFT(username, 7) AS short_name FROM users WHERE username = 'charlie123';
MySQL
SELECT LEFT(username, 7) AS short_name FROM users WHERE username = 'charlie123';
A'charlie12'
B'charlie1'
C'charl'
D'charlie'
Attempts:
2 left
💡 Hint
LEFT() returns the first N characters from the string.
📝 Syntax
advanced
2:00remaining
Which query correctly extracts the last 4 characters using RIGHT()?
You want to get the last 4 characters from the column serial in table devices. Which query is correct?
ASELECT RIGHT(serial) FROM devices;
BSELECT RIGHT(serial, 4) FROM devices;
CSELECT RIGHT(serial, -4) FROM devices;
DSELECT RIGHT(serial, 0) FROM devices;
Attempts:
2 left
💡 Hint
RIGHT() needs a positive integer length as second argument.
query_result
advanced
2:00remaining
What is the output of this nested SUBSTRING and LEFT query?
Given a table files with a column filename containing 'report_final_2023.pdf', what does this query return?

SELECT LEFT(SUBSTRING(filename, 8), 5) AS part FROM files WHERE filename = 'report_final_2023.pdf';
MySQL
SELECT LEFT(SUBSTRING(filename, 8), 5) AS part FROM files WHERE filename = 'report_final_2023.pdf';
A'final_2'
B'final_'
C'final'
D'final_2023'
Attempts:
2 left
💡 Hint
SUBSTRING starts at position 8, then LEFT takes first 5 characters of that substring.
🧠 Conceptual
expert
3:00remaining
Why might SUBSTRING() and LEFT()/RIGHT() produce different results on multibyte characters?
Consider a table texts with a column content containing multibyte UTF-8 characters like emojis. Why can SUBSTRING() and LEFT()/RIGHT() behave differently when extracting parts of such strings?
ABecause SUBSTRING() counts characters while LEFT()/RIGHT() count bytes, causing mismatches with multibyte characters.
BBecause LEFT()/RIGHT() only work on ASCII characters and fail on multibyte characters.
CBecause SUBSTRING() automatically converts multibyte characters to single-byte before extracting.
DBecause LEFT()/RIGHT() ignore multibyte characters and return empty strings.
Attempts:
2 left
💡 Hint
Think about how string length is measured in bytes vs characters.