Este contenido no está disponible en el idioma seleccionado.

7.11. Subqueries


For databases that support subselects, EJB-QL supports subqueries within queries. A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed.
select fatcat from Cat as fatcat 
where fatcat.weight > ( 
    select avg(cat.weight) from DomesticCat cat 
)
Copy to Clipboard Toggle word wrap
select cat from DomesticCat as cat 
where cat.name = some ( 
    select name.nickName from Name as name 
)
Copy to Clipboard Toggle word wrap
select cat from Cat as cat 
where not exists ( 
    from Cat as mate where mate.mate = cat 
)
Copy to Clipboard Toggle word wrap
select cat from DomesticCat as cat 
where cat.name not in ( 
    select name.nickName from Name as name 
)
Copy to Clipboard Toggle word wrap
For subqueries with more than one expression in the select list, you can use a tuple constructor:
select cat from Cat as cat 
where not ( cat.name, cat.color ) in ( 
    select cat.name, cat.color from DomesticCat cat 
)
Copy to Clipboard Toggle word wrap
Note that on some databases (but not Oracle or HSQLDB), you can use tuple constructors in other contexts, for example when querying components or composite user types:
select cat from Person where name = ('Gavin', 'A', 'King')
Copy to Clipboard Toggle word wrap
Which is equivalent to the more verbose:
select cat from Person where name.first = 'Gavin' and name.initial = 'A' and name.last = 'King')
Copy to Clipboard Toggle word wrap
There are two good reasons you might not want to do this kind of thing: first, it is not completely portable between database platforms; second, the query is now dependent upon the ordering of properties in the mapping document.
Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat