I have this table with millions of records:
I want to get the sum of all running jobs from Start Time to End Time. For example query 1:
Between 1:00 and 4:00 how many jobs were running?
Then here the answer will be 3 (Job1, Job2 & Job7).
I can use joins but want the approach that takes least time for millions of records.

You can write a
subquerythat gets your jobCOUNTbetween your start and end times along with aGROUP_CONCATto group all of your jobs in a single, comma delimited row:Result:
Alternatively, you can use a
CTE:Result:
db<>fiddle here.
Notes:
I used a dummy value of
1for the grouping which is why I used asubqueryandCTE, if you don't mind outputting the column you can remove thesubqueryorCTE.You can add a
DISTINCTandORDER BYattributes to theGROUP_CONCATbut this will likely impact your performance.indexed. I'm not sure what your key columns are but setting upindexeswill speed up the query. See How MySQL Uses Indexes.