INSERT Using the WITH Clause in MySQL

As of MySQL v8, we have Common Table Expressions (CTEs) or the WITH clause available. I recently learned how to use the WITH clause in an INSERT statement, similar to INSERT with SELECT. Like always, I enjoy sharing what I learn so let’s all learn together…

Code, content, and community for developers.

The LAMP stack and the PHP technologies and frameworks it runs.

For the sake of simplicity (and my sanity) I’m using this arbitrary sample data:

SELECT *
FROM auto_test;

We can use the WITH clause to generate a name value for the INSERT:

WITH rogue_name AS(
SELECT 'Dillion' AS some_name
)
SELECT * FROM rogue_name;

✔️ MySQL does not require a table named in the FROM clause of a SELECT statement as some SQL dialects do.


You must be mindful of where the WITH clause is placed in the overall statement.

WITH rogue_name AS(
SELECT 'Dillion' AS some_name
)
INSERT INTO auto_test
SELECT some_name FROM rogue_name;

Error Code: 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 'INSERT INTO auto_test SELECT some_name FROM rogue_name' at line 4

You can see that an error has been returned on this attempt to INSERT using WITH. At this point, the WITH clause is expecting the next statement to be a SELECT, and that’s just not the case in this query.

However, moving the WITH clause in between the INSERT INTO clause and the SELECT statement executes with no errors:

INSERT INTO auto_test (name)
WITH rogue_name AS(
SELECT 'Dillion' AS some_name
)
SELECT some_name FROM rogue_name;
SELECT *
FROM auto_test;

It’s pretty handy if you have a WITH clause that generates some data you need to INSERT into another table. Let me know if you use the WITH clause for INSERTs too!

Thank you for reading this post. Please share it with someone else who would enjoy it as well.


Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.

More ways I can help

  • Need hosting for your next web application or WordPress site? I highly recommend Hostinger and use them to host my niche bass fishing site. The service is second to none.
  • Desktop and mobile wallpapers, digital downloads, photography services, Shopify and WooCommerce customizations, and content writing – all in one E-commerce Shop. Find your next digital purchase today!
  • Take your Laravel applications next level with Battle Ready Laravel by Ash Allen. Learn how to improve the performance, maintainability, and security of your Laravel projects in this e-book.

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

Code, content, and community for developers.

The LAMP stack and the PHP technologies and frameworks it runs.


📰 OpenLampTech has sponsorship opportunities for your brand, product, or service in the weekly newsletter. As an independent publication, collaboration is very affordable.


Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.