# Query a result set from another query

**URL:** https://discuss.elastic.co/t/query-a-result-set-from-another-query/289964
**Category:** Elasticsearch
**Created:** [November 23, 2021, 3:46pm UTC](https://discuss.elastic.co/t/query-a-result-set-from-another-query/289964 "2021-11-23T15:46:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![antonkallenberg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antonkallenberg/32/6259_2.png) [@antonkallenberg](https://discuss.elastic.co/u/antonkallenberg)
#### Post date: [November 23, 2021, 3:46pm UTC](https://discuss.elastic.co/t/query-a-result-set-from-another-query/289964/1 "2021-11-23T15:46:32Z")

</div>

Hi,

Is it possible to query a result set from another query?

**Example**

1. List all employees by organization with ID #1 \> returns 10 employees.
2. From those 10 employees \> List all employees working at department with ID #1 \> returns 5 employees.
3. From those 5 employees \> List all employees at section with ID #1 \> returns 3 employees.

This might sound ridiculous given my example but let’s assume that the result sets from step #1, #2 and #3 is fetched using a complex queries with nested ANDs and ORs then it’s quite complicated to build something which can calculates a filter which covers everything and being sure no data is leaking between the steps.

Using SQL it would be something like (not working but you get the idea 🙂)

```auto
select * from  
   (select * from 
       (select * from employees where organization = 10) 
    where department = 1) 
where section = 1

```

Regards,  
Anton

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 21, 2021, 3:46pm UTC](https://discuss.elastic.co/t/query-a-result-set-from-another-query/289964/2 "2021-12-21T15:46:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
