// dialect comparison

Update from an aggregate on the same table

Recompute a column from a GROUP BY over the very table you are updating.

Filed under Inserting & updating

The verdict

SQL Server, PostgreSQL and MySQL read best with the derived-table join; SQLite and Oracle with the correlated subquery. Only MySQL rejects the direct form, so the derived-table join is the version that runs on the most engines.

Where they differ

Each example stores every employee's department headcount on the employee row, which means aggregating the table being updated. The trap is MySQL: it refuses to update a table that a subquery in the same statement reads (error 1093), so the aggregate goes in a derived table, which MySQL materializes first. The other engines accept the direct correlated subquery.

How each engine writes it

SQL Server

UPDATE e
SET e.dept_headcount = t.cnt
FROM employees e
JOIN (SELECT department, COUNT(*) AS cnt
      FROM employees GROUP BY department) t
  ON t.department = e.department;

The derived-table join computes each count once. A correlated subquery in SET also works; SQL Server has no error 1093.

PostgreSQL

UPDATE employees e
SET dept_headcount = t.cnt
FROM (SELECT department, COUNT(*) AS cnt
      FROM employees GROUP BY department) t
WHERE t.department = e.department;

The ordinary UPDATE … FROM shape. The subquery sees the table as it was before the update started.

MySQL

UPDATE employees e
JOIN (SELECT department, COUNT(*) AS cnt
      FROM employees GROUP BY department) t
  ON t.department = e.department
SET e.dept_headcount = t.cnt;

Writing SET dept_headcount = (SELECT COUNT(*) FROM employees e2 WHERE …) fails with error 1093. The GROUP BY derived table is materialized before the update, which makes it legal.

SQLite

UPDATE employees
SET dept_headcount = (SELECT COUNT(*) FROM employees e2
                      WHERE e2.department = employees.department);

The correlated subquery is the simplest form; UPDATE … FROM a derived table works too from 3.33.

Oracle

UPDATE employees e
SET e.dept_headcount = (SELECT COUNT(*) FROM employees e2
                        WHERE e2.department = e.department);

Correlated subquery, evaluated per row. MERGE with the GROUP BY query as its USING source does the same in one pass.

Questions that come up

Why does MySQL raise error 1093?

MySQL won't let a subquery read the table that the same UPDATE or DELETE is modifying ('You can't specify target table for update in FROM clause'). A derived table with GROUP BY is materialized into a temporary table before the update starts, so the restriction no longer applies.

Should the aggregate be NULL or zero for missing rows?

For COUNT it is always at least 1 here, since each employee counts itself. With SUM over another table, a correlated subquery yields NULL for rows with no matches, so wrap it in COALESCE((SELECT …), 0); the derived-table join simply leaves those rows untouched.

Is MERGE an option here?

In SQL Server, Oracle and PostgreSQL 15+, yes: use the GROUP BY query as the USING source and WHEN MATCHED THEN UPDATE. It is equivalent to the derived-table join, not faster.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.