mysqldude

DELETE with a join

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
DELETE c FROM carts c JOIN sessions USING (sessionid) (original)
original post
✓✓✓✓✓✓✓✓✓
Single-table DELETE with IN (subquery) (standard SQL)
modern rewrite
✓✓✓✓✓✓✓✓✓
DELETE FROM carts c WHERE ... (alias in single-table DELETE)
version check
✗ 1064✗ 1064✓✓✓✗ 1064✗ 1064✗ 1064✓
DELETE c FROM carts c JOIN sessions USING (sessionid) (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Single-table DELETE with IN (subquery) (standard SQL) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
DELETE FROM carts c WHERE ... (alias in single-table DELETE) version check
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✗ 106410.11✗ 106411.4✗ 106411.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (2)
  • DELETE FROM carts c WHERE ... (alias in single-table DELETE) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'c WHERE c.sessionid = 19' at line 1
  • DELETE FROM carts c WHERE ... (alias in single-table DELETE) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'c WHERE c.sessionid = 19' at line 1

The original article

First published 2012-02-27. The text below is the original; the table above shows how it behaves on current versions.

Imagine you want to perform a DELETE from one table but requiring a join across multiple tables. There is a particular syntax which permits this.

In my example here, I have a sessions table which contains a session which maps to a sessionid. A cart table consists of a list of products, units and a link back to the sessionid. We wish to clear the cart based on the session provided but leave the sessions table as is i.e. without deleting the entry there.

mysql> select * from sessions where sessionid = 19;
+-----------+----------------------------------+---------+---------------------+
| sessionid | session                          | loginid | expiredate          |
+-----------+----------------------------------+---------+---------------------+
|        19 | q4f5dj4nh7kkf3ip8u98skaiinc32r14 |       1 | 2012-02-07 19:59:14 |
+-----------+----------------------------------+---------+---------------------+
1 row in set (0.00 sec)

mysql> select * from carts where sessionid = 19;
+-------+-----------+---------------+
| units | sessionid | productCodeID |
+-------+-----------+---------------+
|     1 |        19 |            27 |
|     1 |        19 |            28 |
+-------+-----------+---------------+
2 rows in set (0.00 sec)

The syntax used to DELETE from a table is:

DELETE table_to_delete FROM table_to_delete JOIN other_tables ON (...) WHERE ...

So in our example above we have to remove based on a session ‘q4f5dj4nh7kkf3ip8u98skaiinc32r14’.

mysql> DELETE carts c FROM carts c JOIN sessions USING (sessionid) WHERE session = 'q4f5dj4nh7kkf3ip8u98skaiinc32r14';
Query OK, 2 rows affected (0.00 sec)

mysql> select * from carts where sessionid = 19;
Empty set (0.00 sec)

mysql> select * from sessions where sessionid = 19;
+-----------+----------------------------------+---------+---------------------+
| sessionid | session                          | loginid | expiredate          |
+-----------+----------------------------------+---------+---------------------+
|        19 | q4f5dj4nh7kkf3ip8u98skaiinc32r14 |       1 | 2012-02-07 19:59:14 |
+-----------+----------------------------------+---------+---------------------+
1 row in set (0.00 sec)