title: How to Add Row Index of each data row using Query() function in Google Sheets?
body: <p>Is there a way to get the row index using Query function like using row() function but inside a query?</p>
  <p><a href="https://i.stack.imgur.com/qztEV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qztEV.png" alt="enter image description here" /></a></p>
  <p>Here is my query SQL. <strong>The query source are from 2 different sheets. So the row index should restart to 1 when the next source is being selected into the query.</strong></p>
  <pre><code>={
                  query(IMPORTRANGE('Static Data'!$B$2,'Static Data'!$B$3),
                          &quot;select Col&quot; &amp;  ArrayFormula(textjoin(&quot;, Col&quot;,true,column('Static Data'!$A$22:$AC$22))) &amp; &quot;, 'BAU'
                          where 
                          Col&quot;&amp; 'Static Data'!$B$22 &amp;&quot; is not null
                          label 'BAU' 'SOURCE'&quot;, 
                  1);
                  
                  query(IMPORTRANGE('Static Data'!$B$2,'Static Data'!$B$5),
                          &quot;select Col&quot; &amp;  ArrayFormula(textjoin(&quot;, Col&quot;,true,column('Static Data'!$A$22:$AC$22))) &amp; &quot;, 'ICS'
                          where 
                          Col&quot;&amp; 'Static Data'!$B$22 &amp;&quot; is not null
                          label 'ICS' ''&quot;, 
                  0)`}
  </code></pre>
  <p><strong>EDIT (24/Aug/2021):</strong></p>
  <p>Final code that works looks like this after the suggestions from the comments below. I moved the rowId to the end of the table coz that's where I want it to be positioned.</p>
  <pre><code>=INDEX({QUERY({IMPORTRANGE('Static Data'!$B$2, 'Static Data'!$B$3),SEQUENCE(ROWS(
   IMPORTRANGE('Static Data'!$B$2, 'Static Data'!$B$3)))},
   &quot;select Col1,Col&quot;&amp;TEXTJOIN(&quot;,Col&quot;, 1, 1+COLUMN('Static Data'!$A$22:$AC$22))&amp;&quot;,'BAU'
    where Col&quot;&amp;1+'Static Data'!$B$22&amp;&quot; is not null
    label 'BAU''SOURCE'&quot;, 1);
   QUERY({IMPORTRANGE('Static Data'!$B$2, 'Static Data'!$B$5),SEQUENCE(ROWS(
   IMPORTRANGE('Static Data'!$B$2, 'Static Data'!$B$5)))+1},
   &quot;select Col1,Col&quot;&amp;TEXTJOIN(&quot;,Col&quot;, 1, 1+COLUMN('Static Data'!$A$22:$AC$22))&amp;&quot;,'ICS'
    where Col&quot;&amp;1+'Static Data'!$B$22&amp;&quot; is not null
    label 'ICS'''&quot;, )})
  </code></pre>