How do you run an SQL UPDATE statement using values from a SELECT query? | A1Lab@HumairaM to SQLEnglish • 3 days ago

How do you run an SQL UPDATE statement using values from a SELECT query?

message-square
0

To update rows in an SQL table using values from a SELECT query, use a JOIN, a correlated subquery, or a Common Table Expression (CTE) depending on your SQL dialect and how many columns or tables you are updating. Always filter rows carefully with a WHERE clause or join condition so you do not accidentally overwrite unintended rows.


1. UPDATE with JOIN (MySQL, SQL Server / T-SQL)

Using a JOIN is clean and fast when matching keys between a target table and a source table or subquery.

MySQL Syntax

UPDATE employees AS e
JOIN department_budgets AS b
    ON e.department_id = b.department_id
SET e.bonus = e.salary * b.bonus_rate
WHERE b.fiscal_year = 2026;

SQL Server (T-SQL) Syntax

UPDATE e
SET
    e.bonus = e.salary * b.bonus_rate,
    e.updated_at = CURRENT_TIMESTAMP
FROM employees AS e
INNER JOIN department_budgets AS b
    ON e.department_id = b.department_id
WHERE b.fiscal_year = 2026;

2. UPDATE with FROM Clause (PostgreSQL)

PostgreSQL does not allow JOIN directly after UPDATE table_name; instead, it uses a FROM clause to reference additional tables or subqueries.

UPDATE employees AS e
SET
    bonus = e.salary * b.bonus_rate,
    updated_at = CURRENT_TIMESTAMP
FROM department_budgets AS b
WHERE e.department_id = b.department_id
  AND b.fiscal_year = 2026;

3. UPDATE with a Subquery (Standard ANSI SQL / SQLite / All Dialects)

A subquery works across virtually all SQL databases and is useful when pulling a single value or filtering against a derived result set.

Correlated Subquery (Single Column per Row)

UPDATE products AS p
SET price = (
    SELECT sp.new_price
    FROM supplier_prices AS sp
    WHERE sp.product_id = p.product_id
      AND sp.is_active = 1
)
WHERE EXISTS (
    SELECT 1
    FROM supplier_prices AS sp
    WHERE sp.product_id = p.product_id
      AND sp.is_active = 1
);

Note: Include WHERE EXISTS (...) (or another filter) when using a correlated scalar subquery so rows without a match do not get set to NULL.


4. UPDATE with a CTE (PostgreSQL, SQL Server, SQLite)

A Common Table Expression (WITH) helps pre-aggregate or filter data before applying updates, making complex queries easier to read and audit.

WITH latest_totals AS (
    SELECT
        customer_id,
        SUM(order_amount) AS total_spent
    FROM orders
    WHERE status = 'COMPLETED'
    GROUP BY customer_id
)
UPDATE customers AS c
SET lifetime_value = lt.total_spent
FROM latest_totals AS lt
WHERE c.customer_id = lt.customer_id;

Safe Workflow Before Running an UPDATE

  1. Preview with SELECT first: Run a SELECT with the exact same join keys and WHERE conditions to verify which rows will change and inspect the old vs. new values.
  2. Check for duplicate matches: If a join or subquery returns multiple matching rows for one target row, behavior varies by database (some error out, while others apply one match).
  3. Wrap in a transaction when possible: Use BEGIN TRANSACTION / COMMIT or ROLLBACK during testing so you can revert mistakes easily.