Finding opening and closing price using sqlite GROUP BY statement (accessing main query alias in subquery)
You first query is OK although not entirely correct. 


You need to be careful with GROUP BY and not have any column in the SELECT list that is not also in the GROUP BY list. So, add the symbol in the group by list.
Next, the LIMIT needs an ORDER BY so you are 100% certain that you get always the same results. SQLite (and many other DBMS) may return the same results all the time but that "all the time" is not guaranteed. You never know when you'll get a different execution plan and weird result.
If you want more aggregate results (like minimum or maximum price) over the windows, you can add them in the list.


The query becomes:

SELECT symbol, 
       MIN(datetime) AS mindt, 
       MAX(datetime) AS maxdt, 
       AVG(price)    AS avg_price,
       MIN(price)    AS min_price,
       MAX(price)    AS max_price
FROM stocks
WHERE datetime &lt; '1999-06-02 00:00:00'
GROUP BY symbol, strftime('%s', DATETIME)/(60*5) 
ORDER BY symbol, strftime('%s', DATETIME)/(60*5)
LIMIT 4 ;


Now to get also the first and the last price ("opening" and "closing" in finance terminology) in these 5-minutes windows, you'll either need window functions or correlated subqueries or to wrap this query in a derived table and then join again to the original table. I'll write the 3rd option.

It assumes there is a UNIQUE constraint on (symbol, datetime) or (even without a constraint) that you don't have 2 rows with same symbol and datetime:

SELECT 
    s.*,
    s1.price AS opening_price,
    s2.price AS closing_price
FROM
  ( SELECT symbol, 
           MIN(datetime) AS mindt, 
           MAX(datetime) AS maxdt, 
           AVG(price)    AS avg_price,
           MIN(price)    AS min_price,
           MAX(price)    AS max_price
    FROM stocks
    WHERE datetime &lt; '1999-06-02 00:00:00'
    GROUP BY symbol, strftime('%s', DATETIME)/(60*5) 
    ORDER BY symbol, strftime('%s', DATETIME)/(60*5)
    LIMIT 4
  ) AS s
  LEFT JOIN
    stocks AS s1
      ON  s.symbol = s1.symbol
      AND s.mindt  = s1.datetime
  LEFT JOIN
    stocks AS s2
      ON  s.symbol = s2.symbol
      AND s.maxdt  = s2.datetime ;