     }
     
     func makeDeleteStatement(_ db: Database) throws -> UpdateStatement {
-        if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty {
+        switch try grouping(db) {
+        case .none:
+            break
+        case .unique:
+            return try makeTrivialDeleteStatement(db)
+        case .nonUnique:
             // Programmer error
             fatalError("Can't delete query with GROUP BY clause")
-        }
-        
-        guard havingExpressions.isEmpty else {
-            // Programmer error
-            fatalError("Can't delete query with HAVING clause")
-        }
-        
-        guard relation.joins.isEmpty else {
-            // Programmer error
-            fatalError("Can't delete query with JOIN clause")
         }
         
         guard case .table = relation.source else {
             // Programmer error
             fatalError("Can't delete without any database table")
+        }
+        
+        guard relation.joins.isEmpty else {
+            return try makeTrivialDeleteStatement(db)
         }
         
         var context = SQLGenerationContext.queryGenerationContext(aliases: relation.allAliases)

/// SQLQueryGenerator is able to generate an SQL SELECT query.
struct SQLQueryGenerator {
    fileprivate let relation: SQLQualifiedRelation
    private let isDistinct: Bool
    private let groupPromise: DatabasePromise<[SQLExpression]>?
    private let havingExpressions: [SQLExpression]

            }
            sql += " LIMIT " + limit.sql
        }
        
        let statement = try db.makeUpdateStatement(sql: sql)
        statement.arguments = context.arguments!
        return statement
    }
    
    /// DELETE FROM table WHERE rowid IN (SELECT rowid FROM table ...)
    private func makeTrivialDeleteStatement(_ db: Database) throws -> UpdateStatement {
        guard case let .table(tableName: tableName, alias: _) = relation.source else {
            // Programmer error
            fatalError("Can't delete without any database table")
        }
        
        var context = SQLGenerationContext.queryGenerationContext(aliases: relation.allAliases)
        
        // SELECT rowid FROM table ...
        var generator = self
        generator.relation = generator.relation.selectOnly([Column.rowID])
        let selectSQL = try generator.sql(db, &context)
        
        // DELETE FROM table WHERE rowid IN (SELECT rowid FROM table ...)
        let sql = "DELETE FROM \(tableName.quotedDatabaseIdentifier) WHERE rowid IN (\(selectSQL))"
        
        let statement = try db.makeUpdateStatement(sql: sql)
        statement.arguments = context.arguments!
