# ES - SQL query equivalent

**URL:** https://discuss.elastic.co/t/es-sql-query-equivalent/105284
**Category:** Elasticsearch
**Created:** [October 25, 2017, 6:27pm UTC](https://discuss.elastic.co/t/es-sql-query-equivalent/105284 "2017-10-25T18:27:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Dallas\_Toth](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dallas_toth/32/22630_2.png) [@Dallas\_Toth](https://discuss.elastic.co/u/Dallas_Toth)
#### Post date: [October 25, 2017, 6:27pm UTC](https://discuss.elastic.co/t/es-sql-query-equivalent/105284/1 "2017-10-25T18:27:56Z")

</div>

SQL Query  
Select \* from posts where userid in (Select userid from users where username like 'Mr%')

I am wanting to get the results from posts for those userid's.  
Userid exists in both index's but the username only exists in the users index.

Can someone help me with designing the ES query that would result in the same way as the SQL Query?  
Please and Thank-you

---

<div class="post-metadata">

### Author: ![Dallas\_Toth](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dallas_toth/32/22630_2.png) [@Dallas\_Toth](https://discuss.elastic.co/u/Dallas_Toth)
#### Post date: [November 2, 2017, 4:03pm UTC](https://discuss.elastic.co/t/es-sql-query-equivalent/105284/2 "2017-11-02T16:03:18Z")

</div>

Anyone have any input on the above? Is this not possible to have results from one query affect the results from another.

---

<div class="post-metadata">

### Author: ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)
#### Post date: [November 2, 2017, 11:49pm UTC](https://discuss.elastic.co/t/es-sql-query-equivalent/105284/3 "2017-11-02T23:49:24Z")

</div>

For very simple forms of this, you can use [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html). Otherwise, you'll need to either denormalize the data or perform multiple queries.

---

<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: [November 30, 2017, 11:49pm UTC](https://discuss.elastic.co/t/es-sql-query-equivalent/105284/4 "2017-11-30T23:49:25Z")

</div>

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