2

I've got a query that returns the name of a country, where the name must include all of the vowels (a,e,i,o,u).

I'd like to transform this:

SELECT name FROM world
WHERE name LIKE '%a%'
AND name LIKE '%e%'
AND name LIKE '%i%'
AND name LIKE '%o%'
AND name LIKE '%u%'

into a 2 line query. I tried this, but it didn't work:

SELECT name FROM world
WHERE name LIKE '%a%e%i%o%u%'

I though %a%e%i%o%u% should've done it, but I guess I haven't fully grasped the concept yet.

Kaleko
  • 21
  • 2
  • 2
    `'%a%e%i%o%u%'` would only match names which contained all of the vowels **in that specific order**. If you need to match them in any order, you'll need the multiple `LIKE` conditions from your first query. – Richard Deeming Feb 11 '22 at 13:53
  • Please tag your database. SQL is a language – Radagast Feb 11 '22 at 15:25

2 Answers2

1

You can use the wildcard like this:

SELECT name FROM world
WHERE name LIKE '%[aeiou]%';

More info

buddemat
  • 4,552
  • 14
  • 29
  • 49
  • There is condition that all vowels must be included. The square bracket indicates it is performing OR operation on it which clearly not what is expected. Because then it will satisfy the condition even when there one of those characters in the word. So, the solution would be specifying multiple LIKE conditions with AND operations – Dhruv Shah Feb 27 '23 at 12:22
0

SELECT name FROM world WHERE name LIKE '%a%' AND name LIKE '%e%' AND name LIKE '%i%' AND name LIKE '%o%' AND name LIKE '%u%' AND name NOT LIKE '% %'

  • That's essentially the same as the first query in the question, with the addition of the "no spaces" filter which was never mentioned as a requirement. – Richard Deeming Feb 11 '22 at 14:58