Example 1
Stage high-value orders in a temp table then query it
Only orders with total >= 100 (Alice: 150, Carol: 220) pass the filter and land in the temp table. Bob (40) and Dave (90) are excluded. The second SELECT then queries the staged subset. The temp table is automatically dropped when the session ends — there is no need to explicitly DROP it, though doing so early is good practice when the result is large.
CREATE TABLE orders (id INT, customer VARCHAR(50), total INT);
INSERT INTO
orders
VALUES
(1, 'Alice', 150),
(2, 'Bob', 40),
(3, 'Carol', 220),
(4, 'Dave', 90);CREATE TEMPORARY TABLE high_value AS
SELECT
id,
customer,
total
FROM
orders
WHERE
total >= 100;
SELECT
id,
customer,
total
FROM
high_value
ORDER BY
total DESC;| id | customer | total |
|---|---|---|
| 3 | Carol | 220 |
| 1 | Alice | 150 |
CREATE TABLE orders (id INT, customer VARCHAR(50), total INT);
INSERT INTO
orders
VALUES
(1, 'Alice', 150),
(2, 'Bob', 40),
(3, 'Carol', 220),
(4, 'Dave', 90);SELECT id, customer, total INTO #high_value FROM orders WHERE total >= 100; SELECT id, customer, total FROM #high_value ORDER BY total DESC;| id | customer | total |
|---|---|---|
| 3 | Carol | 220 |
| 1 | Alice | 150 |
CREATE TABLE orders (id INT, customer VARCHAR(50), total INT);
INSERT INTO
orders
VALUES
(1, 'Alice', 150),
(2, 'Bob', 40),
(3, 'Carol', 220),
(4, 'Dave', 90);CREATE TEMP TABLE high_value AS
SELECT
id,
customer,
total
FROM
orders
WHERE
total >= 100;
SELECT
id,
customer,
total
FROM
high_value
ORDER BY
total DESC;| id | customer | total |
|---|---|---|
| 3 | Carol | 220 |
| 1 | Alice | 150 |
SQL Server uses SELECT … INTO #high_value. All other engines use CREATE TEMPORARY/TEMP TABLE … AS SELECT. Results are identical.