Microsoft SQL Server Certification Course
- 6k Enrolled Learners
- Weekend
- Live Class
SQL is a language, which consists of multiple commands and operators. But, when you have to retrieve data based on some pattern or characters, then you will need the LIKE operator. So, in this article on LIKE in SQL, I will discuss the following topics:
This operator is used along with the WHERE clause to retrieve the data according to a specific pattern. There are two wildcards which are used along with the LIKE operator to retrieve data. They are:
So, now that I have told you, what is LIKE operator, next, in this article, let us understand the syntax of the LIKE operator.
The syntax of the LIKE operator is as follows:
SELECT column1, coulmn2, . . ., columnN FROM tablename WHERE columnName LIKE pattern;
Now, that you have got an idea of the syntax of the LIKE operator, next in this article on LIKE in SQL, let us see the different patterns you can retrieve with the LIKE operator.
The different patterns mentioned with LIKE operators are as follows:
Query 1: If you have to find values that start with “x”
Like operation:
WHERE columnname LIKE ‘x%’
Query 2: If you have to find values that end with “x”
Like operation:
WHERE columnname LIKE ‘%x’
Query 3: If you have to find values that have “abc” in any position
Like operation:
WHERE columnname LIKE ‘%abc%’
Query 4: If you have to find values that have “a” in the third position
Like operation:
WHERE columnname LIKE ‘__a%’
Here, there are 2 underscores present before the letter “a”.
Query 5: If you have to find values that start with “a” and are at least 5 characters in length
Like operation:
WHERE columnname LIKE ‘a____%’
Here, there are 4 underscores present after the letter “a”.
Query 6: If you have to find values that start with “g” and end with “v”
WHERE columnname LIKE ‘g%v’
So, now that I have discussed the various patterns, next in this article on LIKE in SQL, let us look into some examples.
For a detailed, You can even check out the details of relational databases, functions, and queries, variables, etc with the SQL Training.
Consider the following table on which we will apply various operations of the LIKE operator.
studentID | studentname |
1 | akash |
2 | mitali |
3 | sanjay |
4 | anuj |
5 | sonali |
SELECT * FROM students WHERE studentname LIKE 'a%';
studentID | studentname |
1 | akash |
4 | anuj |
SELECT * FROM students WHERE studentname LIKE '%i';
studentID | studentname |
2 | mitali |
5 | sonali |
SELECT * FROM students WHERE studentname LIKE '%li%';
studentID | studentname |
2 | mitali |
5 | sonali |
SELECT * FROM students WHERE studentname LIKE '_o%';
studentID | studentname |
5 | sonali |
SELECT * FROM students WHERE studentname LIKE 'a____%';
studentID | studentname |
1 | akash |
SELECT * FROM students WHERE studentname LIKE 's%y';
studentID | studentname |
3 | sanjay |
With this, we come to an end to this article. I hope you understood how to use the LIKE clause to retrieve various kinds of data. If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.
Got a question for us? Please mention it in the comments section of this article and I will get back to you.
edureka.co