Finding Substrings with LOCATE and INSTR in MySQL
📖 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