skip to Main Content

I’m a beginner in SQL and I use MySQL database.
I have a table called "employees".
How to retrieve only those employees whose third or any letter after the third is "r"?
I have to use the LIKE operator.

What I’ve tried:

SELECT * FROM employees WHERE last_name LIKE "__r%"

, but I think it only retrieves those employees who have "r" as the third letter in their last name.

2

Answers


  1. select * from customer
    WHERE last_name LIKE "__%r%"
    
    Login or Signup to reply.
  2. Almost there. You just need another wildcard before the r (the wildcard can also match 0 characters).

    SELECT * FROM employees WHERE last_name LIKE '__%r%'
    

    Or, you could use Substring():

    SELECT * FROM employees WHERE Substring(last_name, 3) LIKE '%r%'
    

    Either way, the performance will not generally be great, as you’ll have to evaluate every row in the table (even if you only want a few), and indexes won’t be very helpful.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search