Challenge - 5 Problems
Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Extracting a substring from a string
Given a table Employees with a column
FullName containing full names, which SQL query extracts the first 4 characters of each name?SQL
SELECT FullName FROM Employees;
Attempts:
2 left
💡 Hint
Remember that SQL substring positions start at 1, not 0.
✗ Incorrect
In SQL, SUBSTRING(string, start, length) extracts a substring starting at position 'start' (1-based) for 'length' characters. Option B correctly extracts 4 characters starting at position 1.
❓ query_result
intermediate2:00remaining
Extracting domain from email addresses
Given a table Users with a column
Email containing email addresses, which query extracts the domain part (after '@') correctly?SQL
SELECT Email FROM Users;
Attempts:
2 left
💡 Hint
The domain starts right after the '@' character.
✗ Incorrect
Option D starts extracting from one position after '@' to the end of the string, correctly capturing the domain. Option D extracts the username part, C includes '@' in the result, and D skips one extra character.
📝 Syntax
advanced2:00remaining
Identifying syntax error in SUBSTRING usage
Which of the following SQL queries will cause a syntax error?
Attempts:
2 left
💡 Hint
Check the number of arguments SUBSTRING accepts in standard SQL.
✗ Incorrect
Standard SQL SUBSTRING accepts either two or three arguments. Option C has four arguments, causing a syntax error. Option C uses the SQL standard syntax with FROM and FOR keywords, which is valid in some SQL dialects.
❓ optimization
advanced2:00remaining
Optimizing substring extraction for performance
You want to extract the first 3 characters of a column
Code from a large table Products. Which query is generally more efficient in SQL Server?Attempts:
2 left
💡 Hint
Consider built-in functions optimized for common tasks.
✗ Incorrect
LEFT is optimized for extracting characters from the start of a string and is generally faster than SUBSTRING for this purpose. Option A works but may be slightly less efficient. Option A extracts the wrong part, and D is invalid due to 0 start position.
🧠 Conceptual
expert2:00remaining
Understanding SUBSTRING behavior with out-of-range positions
What is the result of this query if the
Name column contains 'Amy' (3 characters)?
SELECT SUBSTRING(Name, 5, 3) FROM People;SQL
SELECT SUBSTRING(Name, 5, 3) FROM People WHERE Name = 'Amy';
Attempts:
2 left
💡 Hint
What happens if the start position is beyond the string length?
✗ Incorrect
When the start position is beyond the string length, SUBSTRING returns an empty string, not NULL or an error.