SQL SELECT INTO Get Certified
Learn how SELECT INTO creates a new table from the result of a query, and how to achieve the same result in SQLite with CREATE TABLE ... AS SELECT.
What is SELECT INTO?
In many database systems the statement
SELECT ... INTO new_table FROM existing_table;
creates a brand-new table and fills it with the rows returned by the query.
Important for this tutorial: SQLite does not support the SELECT INTO syntax.
The fully equivalent and portable form is:
CREATE TABLE new_table AS
SELECT ...
FROM existing_table;
All executable examples below use the SQLite-compatible form so that “Test My Code” works correctly.
Copy an Entire Table
The simplest use is to make an exact copy of every column and every row.
What this code does: Creates students_backup containing the same three rows that exist in students.
DROP TABLE IF EXISTS students;
DROP TABLE IF EXISTS students_backup;
CREATE TABLE students (
student_id INTEGER,
student_name TEXT,
course TEXT,
score INTEGER
);
INSERT INTO students VALUES
(1, 'Aarav', 'SQL', 88),
(2, 'Meera', 'Python', 92),
(3, 'Kabir', 'HTML', 79);
CREATE TABLE students_backup AS
SELECT *
FROM students;
SELECT *
FROM students_backup
ORDER BY student_id;
Copy Selected Columns Only
You do not have to copy every column. List only the columns you need.
What this code does: Creates a slim table that contains only product_id and product_name.
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS product_names;
CREATE TABLE products (
product_id INTEGER,
product_name TEXT,
category TEXT,
price INTEGER
);
INSERT INTO products VALUES
(1, 'Mechanical Keyboard', 'Accessories', 2800),
(2, 'Wireless Mouse', 'Accessories', 1200),
(3, 'USB Microphone', 'Audio', 3500);
CREATE TABLE product_names AS
SELECT product_id, product_name
FROM products;
SELECT *
FROM product_names
ORDER BY product_id;
Copy Filtered Rows
A WHERE clause lets you store only the rows that satisfy a condition.
What this code does: Creates high_paid_employees containing only the two employees whose salary is at least 80 000.
DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS high_paid_employees;
CREATE TABLE employees (
employee_id INTEGER,
employee_name TEXT,
department TEXT,
salary INTEGER
);
INSERT INTO employees VALUES
(1, 'Riya', 'Design', 58000),
(2, 'Arjun', 'Engineering', 82000),
(3, 'Tara', 'Marketing', 64000),
(4, 'Vikram', 'Engineering', 95000);
CREATE TABLE high_paid_employees AS
SELECT employee_id, employee_name, department, salary
FROM employees
WHERE salary >= 80000;
SELECT *
FROM high_paid_employees
ORDER BY salary DESC;
Copy with ORDER BY
The source query may contain an ORDER BY clause. The new table will still receive the rows in the order the query produces them.
What this code does: Creates expensive_books with books priced 900 or more, sorted from highest to lowest price.
DROP TABLE IF EXISTS books;
DROP TABLE IF EXISTS expensive_books;
CREATE TABLE books (
book_id INTEGER,
title TEXT,
price INTEGER
);
INSERT INTO books VALUES
(1, 'SQL Fundamentals', 700),
(2, 'Database Design', 1200),
(3, 'Web Development', 900),
(4, 'Data Analysis', 1500);
CREATE TABLE expensive_books AS
SELECT book_id, title, price
FROM books
WHERE price >= 900
ORDER BY price DESC;
SELECT *
FROM expensive_books;
Copy with Calculated Columns
You can add expressions to the SELECT list; the new table will contain the computed values.
What this code does: Creates product_summary that includes a total_value column calculated as price × quantity.
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS product_summary;
CREATE TABLE products (
product_id INTEGER,
product_name TEXT,
price INTEGER,
quantity INTEGER
);
INSERT INTO products VALUES
(1, 'Keyboard', 1800, 4),
(2, 'Monitor', 12500, 2),
(3, 'Webcam', 3200, 3);
CREATE TABLE product_summary AS
SELECT
product_id,
product_name,
price,
quantity,
price * quantity AS total_value
FROM products;
SELECT *
FROM product_summary
ORDER BY total_value DESC;
Copy Grouped / Aggregated Results
Aggregate queries can also be materialised into a new table – useful for summary or reporting tables.
What this code does: Creates sales_summary with one row per category containing the total sales amount and the number of orders.
DROP TABLE IF EXISTS sales;
DROP TABLE IF EXISTS sales_summary;
CREATE TABLE sales (
sale_id INTEGER,
category TEXT,
amount INTEGER
);
INSERT INTO sales VALUES
(1, 'Books', 700),
(2, 'Books', 900),
(3, 'Games', 1200),
(4, 'Games', 800),
(5, 'Accessories', 500);
CREATE TABLE sales_summary AS
SELECT
category,
SUM(amount) AS total_sales,
COUNT(*) AS order_count
FROM sales
GROUP BY category;
SELECT *
FROM sales_summary
ORDER BY total_sales DESC;
Copy Data from Multiple Tables (JOIN)
The source query can contain joins, so the new table can combine columns from several existing tables.
What this code does: Creates customer_orders that stores the customer name together with each order id and amount.
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS customer_orders;
CREATE TABLE customers (
customer_id INTEGER,
customer_name TEXT
);
CREATE TABLE orders (
order_id INTEGER,
customer_id INTEGER,
amount INTEGER
);
INSERT INTO customers VALUES
(1, 'Anaya'),
(2, 'Rohan'),
(3, 'Mira');
INSERT INTO orders VALUES
(101, 1, 1500),
(102, 2, 900),
(103, 3, 2100);
CREATE TABLE customer_orders AS
SELECT
c.customer_name,
o.order_id,
o.amount
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id;
SELECT *
FROM customer_orders
ORDER BY order_id;
Create a Table of DISTINCT Values
DISTINCT can be used to store only the unique values of a column.
What this code does: Creates a departments table containing the three unique department names that appear in the employees table.
DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS departments;
CREATE TABLE employees (
employee_id INTEGER,
employee_name TEXT,
department TEXT
);
INSERT INTO employees VALUES
(1, 'Aarav', 'Engineering'),
(2, 'Meera', 'Design'),
(3, 'Kabir', 'Engineering'),
(4, 'Riya', 'Marketing'),
(5, 'Dev', 'Design');
CREATE TABLE departments AS
SELECT DISTINCT department
FROM employees;
SELECT *
FROM departments
ORDER BY department;
SELECT INTO vs CREATE TABLE AS SELECT vs INSERT INTO … SELECT
Syntax Comparison
Advantages
- Quickly materialises the result of any SELECT query into a permanent table.
- Can copy whole tables, selected columns, filtered rows, calculated values or aggregated summaries.
- Works with JOINs, so data from several tables can be combined in one step.
- Useful for creating backup tables, reporting tables or intermediate result sets.
Best Practices
- In SQLite always write
CREATE TABLE … AS SELECTinstead ofSELECT INTO. - Use
DROP TABLE IF EXISTSbefore the CREATE statement in tutorial or script examples so the code can be re-run safely. - Select only the columns you actually need – the new table structure is derived from the SELECT list.
- Remember that indexes, primary keys and other constraints are not copied; add them afterwards if required.
- When the destination table already exists, use
INSERT INTO … SELECTinstead.
Although the classic SELECT INTO statement is not available in SQLite,
the equivalent CREATE TABLE … AS SELECT form gives you the same practical power:
turn any query result into a brand-new table in a single statement.
Click Test My Code on any example to run it live, or
Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.