# I am not getting expected result using subquery in \_sql method of elasticsearch query DSL

**URL:** <https://discuss.elastic.co/t/i-am-not-getting-expected-result-using-subquery-in-sql-method-of-elasticsearch-query-dsl/375550>\
**Category:** Elasticsearch\
**Created:** [March 7, 2025, 9:14am UTC](https://discuss.elastic.co/t/i-am-not-getting-expected-result-using-subquery-in-sql-method-of-elasticsearch-query-dsl/375550 "2025-03-07T09:14:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunil\_Jagtap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sunil_jagtap/32/133604_2.png) [@Sunil\_Jagtap](https://discuss.elastic.co/u/Sunil_Jagtap)\
**Post date:** [March 7, 2025, 9:14am UTC](https://discuss.elastic.co/t/i-am-not-getting-expected-result-using-subquery-in-sql-method-of-elasticsearch-query-dsl/375550/1 "2025-03-07T09:14:22Z")

</div>

Below is the query & output, kindly suggest,

```auto
GET _sql?format=txt
{
 "query": """
  SELECT 
  tenant_id,
  (SELECT is_vulnerability_scan_tool_active from "index1") AS VST
  from "index2" where tenant_id='ABCDEFHG'
"""
}

```

Output :

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "planning_exception",
        "reason": """Found 1 problem
line 4:3: Unresolved expression"""
      }
    ],
    "type": "planning_exception",
    "reason": """Found 1 problem
line 4:3: Unresolved expression"""
  },
  "status": 400
}

```

basically I want to extract data using subquery on the basis on different conditions applied as well as aggregation applied on some column fields.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 7, 2025, 9:51am UTC](https://discuss.elastic.co/t/i-am-not-getting-expected-result-using-subquery-in-sql-method-of-elasticsearch-query-dsl/375550/2 "2025-03-07T09:51:54Z")

</div>

Please don't post images of text as they are hard to read, may not display correctly for everyone, and are not searchable.

Instead, paste the text and format it with `</>` icon or pairs of triple backticks (```), and check the preview window to make sure it's properly formatted before posting it. This makes it more likely that your question will receive a useful answer.

It would be great if you could update your post to solve this.

---

<div class="post-metadata">

**Author:** ![Sunil\_Jagtap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sunil_jagtap/32/133604_2.png) [@Sunil\_Jagtap](https://discuss.elastic.co/u/Sunil_Jagtap)\
**Post date:** [March 7, 2025, 10:33am UTC](https://discuss.elastic.co/t/i-am-not-getting-expected-result-using-subquery-in-sql-method-of-elasticsearch-query-dsl/375550/3 "2025-03-07T10:33:18Z")

</div>

Thanks for suggestion @dadoonet

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 7, 2025, 10:57am UTC](https://discuss.elastic.co/t/i-am-not-getting-expected-result-using-subquery-in-sql-method-of-elasticsearch-query-dsl/375550/4 "2025-03-07T10:57:38Z")

</div>

I'm wondering if you are hitting this limitation : [SQL Limitations | Elasticsearch Guide [8.17] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-limitations.html#_using_a_sub_select)

I think that ES|QL might be able to solve such use case with the coming 8.18 version.

---

<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:** [April 4, 2025, 10:57am UTC](https://discuss.elastic.co/t/i-am-not-getting-expected-result-using-subquery-in-sql-method-of-elasticsearch-query-dsl/375550/5 "2025-04-04T10:57:52Z")

</div>

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