is at least ae powerful as SQL. Two well known relational query languages that have a sound theoretical foundation are relational algebra and relational calculus. As SQL is more closely related to relational calculus, we define the semantics of SQL by translation into calculus. However, relational calculus has to be extended in order to deal with aggregate functions and null values. The op timiation of SQL queries is thus reduced to the optimiaation of relational calculus queries, which makes the following results also applicable to other query languages based on relational calculus (e.g. QUEL). In this paper, we give a precise definition of the semantics of SQL queries having aggregate functions, identify the problems associated with the optimisation of such queries and give some solutions. The semantics of SQL queries is defined by translating them into expressions of an extended relational calculus (extensions are necessary for a correct treatment of aggregate functions and null val...