Hey there! Today, let's chat about the LIKE operator in SQL. It's a pretty cool tool that helps you find things that are 'kind of' but not 'exactly' what you're looking for.
Think about it like this: Imagine you're trying to remember a song, but you only remember a few words of the lyrics. You don’t need the exact title; you just need something close. That's where LIKE comes in handy in SQL.
For example, say you're looking through a database of books, but you can’t recall the exact title of the one you want. You remember it starts with 'The Great'. With LIKE, you can search for titles that start with these words, even if you don’t remember the rest.
Here's how you'd use it: Suppose we have a table named 'books' in our database. The table has columns like 'title', 'author', and 'year_published'.
| Title | Author | Year Published |
|---|---|---|
| The Great Gatsby | F. Scott Fitzgerald | 1925 |
| To Kill a Mockingbird | Harper Lee | 1960 |
| The Great Escape | Paul Brickhill | 1950 |
| 1984 | George Orwell | 1949 |
| The Catcher in the Rye | J.D. Salinger | 1951 |
| The Great Adventure | Sarah Jenkins | 2001 |
| Pride and Prejudice | Jane Austen | 1813 |
| The Lord of the Rings | J.R.R. Tolkien | 1954 |
If you want to find books that start with 'The Great', your SQL query would look something like this:
SELECT *
FROM books
WHERE title LIKE 'The Great%';
The '%' symbol is a wildcard. It tells SQL, 'Hey, I'm looking for titles that start with 'The Great', but whatever comes after that, I'm cool with it! Even nothing works (i.e The Great will also be included)
Now, let's say you want to find book titles that include the word 'Great'. In SQL, you can do this using the LIKE operator with the '%' symbol, which acts like a wildcard:
SELECT *
FROM books
WHERE title LIKE '%Great%';
This query tells SQL to find all titles that have 'Great' in them, no matter what text comes before or after it. Just remember, the LIKE operator has its limitations and is specific about capitalization. So, searching for 'Great' will not find variations like 'great' or 'GREAT'. It only matches 'Great' exactly as it is.
Soon, you'll learn about an advanced tool that isn't picky about capitalization. Hang tight – it's coming up!
Meanwhile, let's practice this new concept⭐️