# How can I create SELECT FROM SELECT query with JPA and Hibernate?

**URL:** <https://discourse.hibernate.org/t/how-can-i-create-select-from-select-query-with-jpa-and-hibernate/1156>\
**Category:** Hibernate ORM\
**Created:** [August 2, 2018, 4:11am UTC](https://discourse.hibernate.org/t/how-can-i-create-select-from-select-query-with-jpa-and-hibernate/1156 "2018-08-02T04:11:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iRumba](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@iRumba](https://discourse.hibernate.org/u/iRumba)\
**Post date:** [August 2, 2018, 4:11am UTC](https://discourse.hibernate.org/t/how-can-i-create-select-from-select-query-with-jpa-and-hibernate/1156/1 "2018-08-02T04:11:22Z")

</div>

Hello. I need create query

```auto
SELECT *
FROM (SELECT ...) alias
WHERE alias.SomeProperty = someValue AND alias.OtherProperty = otherValue

```

But QueryOver.WithSubquery creates query SELECT \* FROM table WHERE someProperty = (SELECT …)

Help me please

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [August 7, 2018, 7:25am UTC](https://discourse.hibernate.org/t/how-can-i-create-select-from-select-query-with-jpa-and-hibernate/1156/2 "2018-08-07T07:25:17Z")

</div>

Neither JPQL or Criteria supports derived tables. Just use the native SQL which allows you to [benefit from any database-specific feature or syntax](https://vladmihalcea.com/the-jpa-entitymanager-createnativequery-is-a-magic-wand/).

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [August 8, 2018, 8:03am UTC](https://discourse.hibernate.org/t/how-can-i-create-select-from-select-query-with-jpa-and-hibernate/1156/3 "2018-08-08T08:03:56Z")

</div>

Just recently Jan-Willem Gmeilig Meyling was introduced on the Hibernate blog who wrote about exactly that use case and how Blaze-Persistence’s support for CTEs can help you with this:  
[http://in.relation.to/2018/08/07/meet-jan-willem-gmelig-meyling/](http://in.relation.to/2018/08/07/meet-jan-willem-gmelig-meyling/)

See here for examples: [https://persistence.blazebit.com/documentation/1.3/core/manual/en\_US/#ctes](https://persistence.blazebit.com/documentation/1.3/core/manual/en_US/#ctes)
