INSERT INTO ... SELECT copies data from one table into another existing table.
To create a new table from a query:
- MySQL, PostgreSQL, SQLite:
CREATE TABLE NewTable AS SELECT ... - SQL Server:
SELECT ... INTO NewTable FROM ...
Copy rows from one table to another.
✍️ 3 exercises + code challenge in this lessonINSERT INTO ... SELECT copies data from one table into another existing table.
To create a new table from a query:
CREATE TABLE NewTable AS SELECT ...SELECT ... INTO NewTable FROM ...Mastering SQL INSERT INTO SELECT helps you write clean and reliable SQL code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
CREATE TABLE PakistanCustomers AS
SELECT CustomerID, CustomerName, City FROM Customers WHERE 0;
INSERT INTO PakistanCustomers
SELECT CustomerID, CustomerName, City FROM Customers
WHERE Country = 'Pakistan';
SELECT * FROM PakistanCustomers;
Check what you learned in “SQL INSERT INTO SELECT”.
Copy rows from a query into a table.
INSERT INTO Archive * FROM Orders WHERE Status = 'Delivered';
Which statement creates a NEW table from a query in SQLite or MySQL?
Create a table Delivered AS SELECT OrderID FROM Orders WHERE Status = 'Delivered', then select COUNT(*) AS Total from it.
Free sign-in required · Google login available
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services