📖 Scenario: You work at a small bookstore. You have a table of book titles, and you want to find where certain words appear inside those titles.
🎯 Goal: Build a simple MySQL query that uses LOCATE and INSTR functions to find the position of a word inside book titles.
📋 What You'll Learn
Create a table called books with columns id (integer) and title (varchar).
Insert exactly these three rows into books: (1, 'The Great Gatsby'), (2, 'Great Expectations'), (3, 'The Grapes of Wrath').
Create a variable called search_word and set it to the string 'Great'.
Write a SELECT query that uses LOCATE(search_word, title) to find the position of search_word in each title.
Write a SELECT query that uses INSTR(title, search_word) to find the position of search_word in each title.
💡 Why This Matters
🌍 Real World
Finding where a word or phrase appears inside text fields is common in searching, filtering, and data cleaning tasks.
💼 Career
Database developers and analysts often use LOCATE and INSTR to write queries that find and manipulate text data efficiently.
Progress0 / 4 steps
1
Create the books table and insert data
Create a table called books with columns id as INT and title as VARCHAR(100). Then insert these exact rows: (1, 'The Great Gatsby'), (2, 'Great Expectations'), and (3, 'The Grapes of Wrath').
MySQL
Hint
Use CREATE TABLE to make the table, then INSERT INTO to add rows.
2
Set the search_word variable
Create a variable called search_word and set it to the string 'Great'. Use SET @search_word = 'Great'; to do this.
MySQL
Hint
Use SET @search_word = 'Great'; to create the variable.
3
Use LOCATE to find the word position
Write a SELECT query that shows id, title, and the position of @search_word inside title using LOCATE(@search_word, title). Name the position column locate_pos.
MySQL
Hint
Use LOCATE(@search_word, title) inside the SELECT statement.
4
Use INSTR to find the word position
Write a SELECT query that shows id, title, and the position of @search_word inside title using INSTR(title, @search_word). Name the position column instr_pos.
MySQL
Hint
Use INSTR(title, @search_word) inside the SELECT statement.
Practice
(1/5)
1. What does the MySQL function LOCATE('cat', 'concatenate') return?
easy
A. 0
B. 4
C. 1
D. 7
Solution
Step 1: Understand LOCATE function behavior
LOCATE returns the position of the first occurrence of the substring in the string, starting from the beginning by default.
Step 2: Find 'cat' in 'concatenate'
The substring 'cat' starts at the 4th character in 'concatenate'.
Final Answer:
4 -> Option B
Quick Check:
LOCATE('cat', 'concatenate') = 4 [OK]
Hint: LOCATE returns first position of substring, counting from 1 [OK]
Common Mistakes:
Counting from 0 instead of 1
Confusing LOCATE with INSTR starting position
Assuming substring not found returns NULL
2. Which of the following is the correct syntax to find the position of 'dog' in the string 'bulldog' starting search from position 5 using LOCATE?
easy
A. LOCATE('dog', 'bulldog') FROM 5
B. LOCATE('dog', 5, 'bulldog')
C. LOCATE('bulldog', 'dog', 5)
D. LOCATE('dog', 'bulldog', 5)
Solution
Step 1: Recall LOCATE syntax
The correct syntax is LOCATE(substring, string, start_position).
Step 2: Match syntax with options
LOCATE('dog', 'bulldog', 5) matches the correct order: substring 'dog', string 'bulldog', start position 5.