SyntaxStudy
Sign Up
MySQL Intermediate 4 min read

Temporary Tables

Temporary Tables

Temporary tables exist only for the current session and are automatically dropped when the session ends. Useful for breaking complex queries into steps.

Example
CREATE TEMPORARY TABLE top_customers AS
SELECT customer_id, SUM(total) AS revenue
FROM orders
WHERE status = 'completed'
GROUP BY customer_id
HAVING revenue > 10000;

SELECT c.name, t.revenue
FROM top_customers t
JOIN customers c ON c.id = t.customer_id
ORDER BY t.revenue DESC;
Pro Tip

Add indexes to temp tables before using them in further joins.