Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use LIKE wild card on subquery?

Tags:

sql

oracle

Table: FirstNames
NAME
    Tom
    Joe
    Peter

Table: FullNames
FULL_NAME:
    Tom Petty.
    Joe Satriani.
    Peter Griffin.
    Sarah Connor.

I would like to run a query:

select * 
from FullNames where FULL_NAME like '%' || (select NAME from FirstNames) || '%'

It yields:

ORA-01427: single-row subquery returns more than one row

which seems correct. Is there a way to do that in Oracle?

like image 929
filippo Avatar asked Jan 02 '23 00:01

filippo


2 Answers

You could use JOIN:

SELECT * 
FROM FullNames f
JOIN FirstNames g
  ON f.FULL_NAME LIKE '%' || g.NAME || '%';
like image 51
Lukasz Szozda Avatar answered Jan 05 '23 17:01

Lukasz Szozda


You can use exists:

select f.*
from FullNames f
where exists (select 1
              from firstnames fn
              where f.FULL_NAME like '%' || fn.NAME || '%'
             );
like image 40
Gordon Linoff Avatar answered Jan 05 '23 16:01

Gordon Linoff