📖 Scenario: You work at a small bookstore that keeps track of book titles and authors in a database. Sometimes, the data is messy: extra spaces, inconsistent capitalization, or partial names. You want to clean up and search this data efficiently.
🎯 Goal: Build SQL queries that use string functions to clean and search text data in the bookstore database.
📋 What You'll Learn
Create a table called books with columns id, title, and author.
Insert sample data with inconsistent spacing and capitalization.
Use the TRIM() function to remove extra spaces from titles.
Use the UPPER() function to standardize author names.
Use the LIKE operator with string functions to find books by partial author name.
💡 Why This Matters
🌍 Real World
Cleaning and searching text data is common in databases for customer names, product descriptions, and more.
💼 Career
Knowing string functions helps database professionals write better queries for data cleaning, reporting, and search features.
Progress0 / 4 steps
1
Create the books table and insert sample data
Write SQL statements to create a table called books with columns id (integer), title (text), and author (text). Then insert these exact rows: (1, ' The Hobbit ', 'j.r.r. tolkien'), (2, '1984', 'george orwell'), (3, 'Brave New World', 'aldous huxley').
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Add a query to trim spaces from book titles
Write a SQL SELECT statement that returns the id and the trimmed title using the TRIM() function from the books table.
SQL
Hint
Use TRIM(column_name) to remove spaces from both ends of the text.
3
Standardize author names to uppercase
Write a SQL SELECT statement that returns the id and the author names converted to uppercase using the UPPER() function from the books table.
SQL
Hint
Use UPPER(column_name) to convert text to uppercase.
4
Find books by partial author name using string functions
Write a SQL SELECT statement that returns all columns from books where the uppercase author contains the substring 'ORWELL'. Use the UPPER() function and the LIKE operator with wildcards.
SQL
Hint
Use LIKE '%substring%' to find text containing a substring.
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
Step 1: Understand the purpose of string functions
Functions like UPPER() and LOWER() change the case of text data to uppercase or lowercase.
Step 2: Recognize their use in queries
Changing case helps compare text consistently or display it uniformly, which is important for searching or reporting.
Final Answer:
To change text case for consistent comparison or display -> Option D
Quick Check:
String functions change text case = B [OK]
Hint: String functions change or check text, not numbers [OK]
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
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.
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.
Final Answer:
SELECT LEFT(name, 3) FROM table; -> Option B
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
Step 1: Understand the TRIM() function
TRIM(email) removes spaces at the start and end of the email string.
Step 2: Understand LENGTH() function
LENGTH() returns the number of characters in the string after trimming spaces.
Final Answer:
Length of email excluding leading and trailing spaces -> Option A
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
Step 1: Check the LIKE pattern usage
The pattern 'Jo%' correctly uses % wildcard inside quotes to match names starting with 'Jo'.
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.
Final Answer:
LIKE is case-sensitive and may miss 'jo' lowercase -> Option A
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
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.
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.
Final Answer:
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users; -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.