Bird
Raised Fist0
SQLquery~20 mins

Why string functions matter in queries in SQL - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Using LOWER() to find case-insensitive matches
Given a table Users with a column username, which query returns all users whose username is 'alice' regardless of case?
SQL
SELECT username FROM Users WHERE LOWER(username) = 'alice';
ASELECT username FROM Users WHERE LOWER(username) = 'alice';
BSELECT username FROM Users WHERE username LIKE 'Alice';
CSELECT username FROM Users WHERE UPPER(username) = 'ALICE';
DSELECT username FROM Users WHERE username = 'alice';
Attempts:
2 left
💡 Hint
Think about how to ignore uppercase or lowercase differences in text.
❓ query_result
intermediate
2:00remaining
Extracting domain from email using SUBSTRING and POSITION
Which query correctly extracts the domain part (after '@') from the email column in the Contacts table?
SQL
SELECT SUBSTRING(email FROM POSITION('@' IN email) + 1) AS domain FROM Contacts;
ASELECT SUBSTRING(email, 1, POSITION('@' IN email) - 1) AS domain FROM Contacts;
BSELECT SUBSTRING(email, POSITION('@' IN email)) AS domain FROM Contacts;
CSELECT SUBSTRING(email FROM 1 FOR POSITION('@' IN email)) AS domain FROM Contacts;
DSELECT SUBSTRING(email FROM POSITION('@' IN email) + 1) AS domain FROM Contacts;
Attempts:
2 left
💡 Hint
Remember that POSITION('@' IN email) gives the location of '@'.
📝 Syntax
advanced
2:00remaining
Identifying syntax error in string concatenation
Which option contains a syntax error when trying to concatenate first and last names with a space in between in SQL?
ASELECT first_name || ' ' || last_name AS full_name FROM Employees;
BSELECT first_name || ' ' || last_name FROM Employees;
CSELECT first_name + ' ' + last_name AS full_name FROM Employees;
DSELECT CONCAT(first_name, ' ', last_name) AS full_name FROM Employees;
Attempts:
2 left
💡 Hint
Check which operator is valid for string concatenation in SQL.
❓ optimization
advanced
2:00remaining
Optimizing query with TRIM to remove spaces
Which query efficiently removes leading and trailing spaces from the product_code column in the Inventory table before comparison?
ASELECT * FROM Inventory WHERE TRIM(product_code) = 'ABC123';
BSELECT * FROM Inventory WHERE product_code = TRIM(' ABC123 ');
CSELECT * FROM Inventory WHERE product_code = 'ABC123';
DSELECT * FROM Inventory WHERE LTRIM(RTRIM(product_code)) = 'ABC123';
Attempts:
2 left
💡 Hint
Consider which side the spaces might be on and how to remove them from the column data.
🧠 Conceptual
expert
2:00remaining
Why use string functions in queries?
Which reason best explains why string functions are important in database queries?
AThey speed up database hardware performance automatically.
BThey allow manipulation and comparison of text data to handle variations and extract useful parts.
CThey replace the need for indexing tables.
DThey convert all data types into strings for easier storage.
Attempts:
2 left
💡 Hint
Think about what string functions do to text data in queries.

Practice

(1/5)
1. Why do we use string functions like UPPER() or LOWER() in SQL queries?
easy
A. To perform mathematical calculations on numbers
B. To delete rows from a table
C. To create new tables in the database
D. To change text case for consistent comparison or display

Solution

  1. Step 1: Understand the purpose of string functions

    Functions like UPPER() and LOWER() change the case of text data to uppercase or lowercase.
  2. Step 2: Recognize their use in queries

    Changing case helps compare text consistently or display it uniformly, which is important for searching or reporting.
  3. Final Answer:

    To change text case for consistent comparison or display -> Option D
  4. Quick Check:

    String functions change text case = B [OK]
Hint: String functions change or check text, not numbers [OK]
Common Mistakes:
  • Confusing string functions with numeric functions
  • Thinking string functions create or delete tables
  • Assuming string functions modify database structure
2. Which of the following is the correct syntax to get the first 3 characters of a column named name in SQL?
easy
A. SELECT MID(name, 3) FROM table;
B. SELECT LEFT(name, 3) FROM table;
C. SELECT RIGHT(name, 3) FROM table;
D. SELECT SUBSTRING(name FROM 3) FROM table;

Solution

  1. Step 1: Identify the function to get characters from the start

    The LEFT() function extracts a specified number of characters from the beginning of a string.
  2. Step 2: Check the syntax correctness

    LEFT(name, 3) correctly gets the first 3 characters. Other options either extract from the wrong side or have incorrect syntax.
  3. Final Answer:

    SELECT LEFT(name, 3) FROM table; -> Option B
  4. Quick Check:

    LEFT gets first chars = D [OK]
Hint: LEFT(string, n) extracts first n characters [OK]
Common Mistakes:
  • Using RIGHT() instead of LEFT() for first characters
  • Incorrect SUBSTRING syntax with FROM keyword
  • Using MID() which is not standard SQL
3. Given a table users with a column email, what will this query return?
SELECT LENGTH(TRIM(email)) FROM users;
medium
A. Length of email excluding leading and trailing spaces
B. Length of email including spaces
C. Number of rows in the users table
D. Email addresses converted to uppercase

Solution

  1. Step 1: Understand the TRIM() function

    TRIM(email) removes spaces at the start and end of the email string.
  2. Step 2: Understand LENGTH() function

    LENGTH() returns the number of characters in the string after trimming spaces.
  3. Final Answer:

    Length of email excluding leading and trailing spaces -> Option A
  4. Quick Check:

    TRIM removes spaces, LENGTH counts chars = A [OK]
Hint: TRIM removes spaces, LENGTH counts characters [OK]
Common Mistakes:
  • Thinking LENGTH counts spaces inside the string
  • Confusing TRIM with UPPER or LOWER
  • Assuming query counts rows, not string length
4. Identify the error in this SQL query that tries to find names starting with 'Jo':
SELECT * FROM employees WHERE name LIKE 'Jo%';
medium
A. LIKE is case-sensitive and may miss 'jo' lowercase
B. The wildcard % should be inside quotes
C. Missing a semicolon at the end
D. Using = instead of LIKE for pattern matching

Solution

  1. Step 1: Check the LIKE pattern usage

    The pattern 'Jo%' correctly uses % wildcard inside quotes to match names starting with 'Jo'.
  2. Step 2: Consider case sensitivity of LIKE

    LIKE is case-sensitive in many SQL systems, so it won't match 'jo' lowercase names, which may cause missed results.
  3. Final Answer:

    LIKE is case-sensitive and may miss 'jo' lowercase -> Option A
  4. Quick Check:

    LIKE case sensitivity can cause misses = A [OK]
Hint: LIKE may be case-sensitive; use UPPER() to fix [OK]
Common Mistakes:
  • Thinking % wildcard must be outside quotes
  • Ignoring case sensitivity in LIKE
  • Assuming semicolon is mandatory for all SQL engines
5. You want to combine first name and last name columns into one full name column with a space between them. Which SQL expression correctly does this?
hard
A. SELECT first_name || ' ' || last_name AS full_name FROM users;
B. SELECT CONCAT(first_name, last_name) AS full_name FROM users;
C. SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
D. SELECT first_name + ' ' + last_name AS full_name FROM users;

Solution

  1. Step 1: Understand string concatenation in SQL

    Different SQL systems use different ways to join strings: || or CONCAT(). The CONCAT() function can take multiple arguments.
  2. Step 2: Check which option adds space between names

    SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users; uses CONCAT(first_name, ' ', last_name), correctly adding a space between first and last names. SELECT first_name || ' ' || last_name AS full_name FROM users; uses || which works in some systems but not all. SELECT CONCAT(first_name, last_name) AS full_name FROM users; misses the space. SELECT first_name + ' ' + last_name AS full_name FROM users; uses + which is invalid for strings in SQL.
  3. Final Answer:

    SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users; -> Option C
  4. Quick Check:

    CONCAT with space joins names correctly = C [OK]
Hint: Use CONCAT with space to join names safely [OK]
Common Mistakes:
  • Forgetting space between names
  • Using + operator for string concatenation
  • Using CONCAT without space argument
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard