1. A computer implemented method for optimizing database queries, the method comprising: receiving, by a database system, a request to optimize a database query; identifying an inner subquery and an outer subquery in the database query, the inner subquery nested within the outer subquery; identifying a first set of columns such that the inner subquery performs a group by based on the first set of columns; identifying a second set of columns such that the outer subquery performs a group by based on the second set of columns; determining that the second set of columns is a subset of the first set of columns; determining whether a result of the inner subquery is provided as an input to the outer subquery; generating a combined database query corresponding to a composition of the outer subquery and the inner subquery if the result of the inner subquery is determined to be an input to the outer subquery and the second set of columns is determined to be a subset of the first set of columns; and storing the combined database query.