# Getting a query from a query

**URL:** <https://forum.inductiveautomation.com/t/getting-a-query-from-a-query/28185>\
**Category:** Ignition\
**Created:** [September 11, 2019, 7:02pm UTC](https://forum.inductiveautomation.com/t/getting-a-query-from-a-query/28185 "2019-09-11T19:02:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DD\_Chelsea](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@DD\_Chelsea](https://forum.inductiveautomation.com/u/DD_Chelsea)\
**Post date:** [September 11, 2019, 7:02pm UTC](https://forum.inductiveautomation.com/t/getting-a-query-from-a-query/28185/1 "2019-09-11T19:02:17Z")

</div>

Good morning guys.

I’m trying to gather information about a table in my database.

The information of the database is the following:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/31b0fd5f4a809986a12545e9629f616d24c3d190.png)

I’m trying to gather a count of “NEXT MAX” of the last 10 rows of my table.

my query is:  
SELECT COUNT(\*)  
FROM prueba3  
WHERE modelo = “NEXT MAX”  
ORDER BY id DESC  
LIMIT 10

The result im getting is 3, i want to get the result 2.

Is there a direct way to do this using a simple Query? if not, Is there a way doing a query over a query?

Thanks in advance for the help.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 11, 2019, 7:22pm UTC](https://forum.inductiveautomation.com/t/getting-a-query-from-a-query/28185/2 "2019-09-11T19:22:50Z")

</div>

You can use a subquery to gather your 10 rows:

```sql
SELECT count(*)
FROM (SELECT * FROM prueba3
		ORDER BY id DESC
		LIMIT 10
	) AS sample
WHERE modelo = 'NEXT MAX'
```

---

<div class="post-metadata">

**Author:** ![DD\_Chelsea](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@DD\_Chelsea](https://forum.inductiveautomation.com/u/DD_Chelsea)\
**Post date:** [September 11, 2019, 7:26pm UTC](https://forum.inductiveautomation.com/t/getting-a-query-from-a-query/28185/3 "2019-09-11T19:26:39Z")

</div>

It worked 🙂

Thank you so much for the help, i really appreciate it.

Have a good day!
