Query
In SQLite we can create a temporary table with each customer's number of non-cancelled orders and their total amount. Using TEMP keeps the example inside the practice session.
CREATE TEMP TABLE customer_summary AS
SELECT customer_id,
COUNT(*) AS orders,
ROUND(SUM(total), 2) AS total_amount
FROM orders
WHERE status <> 'cancelled'
GROUP BY customer_id;You can then query the table like any other table in the session:
SELECT customer_id, orders, total_amount
FROM customer_summary
ORDER BY total_amount DESC, customer_id
LIMIT 6;The temporary table holds ten customers with non-cancelled orders. Unlike a view, those values are materialised in the new table and are not recalculated when the source tables change.
How CREATE TABLE AS SELECT works
The table gets one column for each column returned by the SELECT. Use aliases when a calculated expression needs a clear name.
The aliases orders e total_amount become sensible names for the calculated columns of the new table.
Cases that change the result
CTAS, INSERT ... SELECT or VIEW
All three techniques reuse a query, but they solve different problems.
| Option | Target | When it fits |
|---|---|---|
CREATE TABLE ... AS SELECT | A new table with a copy of the result | The target does not exist yet and you want to materialise data |
INSERT INTO ... SELECT | New rows in an existing table | You have already defined the schema and its constraints |
CREATE VIEW ... AS SELECT | A saved query, not a separate copy of the rows | You always want the current data from the base tables |
If you need to control PRIMARY KEY, FOREIGN KEY, CHECK, exact types or indexes up front, it is usually clearer to create the table first and then load it with INSERT ... SELECT.
What is copied and what is not
CTAS starts from the query output. That is not the same as cloning the full definition of an existing table.
- Columns are created from the expressions returned by the
SELECT. - The rows produced by that query are inserted.
- Output aliases help define understandable names.
- Do not assume that primary keys, foreign keys,
UNIQUE/CHECKconstraints or indexes from the source tables have been copied. - The order used to produce the query does not turn the table into a permanently sorted collection.
If the new table is to become a stable part of the model, define and check its schema explicitly. A quick copy for analysis and a production table have different needs.
Check the table after creating it
A structural change is not finished just because the statement stops erroring. At the very least, check the number of rows, the names and types of the relevant columns and a few edge-case rows.
SELECT COUNT(*) AS rows
FROM customer_summary;
SELECT *
FROM customer_summary
ORDER BY customer_id
LIMIT 5;To inspect types, constraints or indexes, use the engine's own catalogue tools. There is no metadata query that is fully portable across PostgreSQL, SQLite, MySQL and SQL Server.
Key differences between engines
PostgreSQL and SQLite document CREATE TABLE ... AS SELECT. The shared idea is to create a table and fill it with the result of a query, but the details on temporary tables, derived types, storage options and creating the table without data vary between engines.
SQLite specifically documents that a table created with CREATE TABLE ... AS SELECT takes its columns from the result and does not automatically inherit primary key or other constraints from the source schema. On any engine, treat CTAS as materialising a query and check the resulting definition before relying on it.