# Sub queries in custom finders

**URL:** <https://discourse.cakephp.org/t/sub-queries-in-custom-finders/3062>\
**Category:** Need Help\
**Created:** [August 21, 2017, 9:42am UTC](https://discourse.cakephp.org/t/sub-queries-in-custom-finders/3062 "2017-08-21T09:42:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![HarentsoaNotoavina](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/harentsoanotoavina/32/745_2.png) [@HarentsoaNotoavina](https://discourse.cakephp.org/u/HarentsoaNotoavina)\
**Post date:** [August 21, 2017, 9:42am UTC](https://discourse.cakephp.org/t/sub-queries-in-custom-finders/3062/1 "2017-08-21T09:42:24Z")

</div>

Hi, I want to make sub queries in custom finders but I don’t know how to make it, in cakePhp 3.x releases.  
My SQL request is like on the image bellow ![requete](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/1X/2ed0a423b436d9da2738292eeb6ce0fc44fe7dfc.JPG)

And the finder method in scr/model/Table is like this:  
public function findEtatStock(Query $query){  
$query = $this-\>find()  
-\>select([  
‘produits.designation’,  
‘produits.stock’,  
‘approvisionnements.qteentree’,  
‘ventes.qtesortie’,  
‘etat\_stock’ =\> ‘??’ // the place of the request  
]);

```
        $query
            ->innerJoinWith('Approvisionnements')
            ->innerJoinWith('Ventes')
            ->where([
                ['approvisionnements.produit_id = produits.id'], ['ventes.produit_id = produits.id']
            ])
            ->groupBy('produits.desingation');
    return $query;
}
```
