Skip to main content

Introduction to Tables

Introduction

Welcome to the CTAS & TEMP Tables section of our SQL journey! Before diving into advanced table creation methods, let's establish a solid understanding of what tables are and the different ways to create them.

In this tutorial, you will learn:

  • What database tables are and why they matter
  • Different methods to create tables in SQL
  • The evolution from basic CREATE TABLE to more advanced techniques
  • When to use each table creation approach

What is a Table?

The Foundation of Relational Databases

A table is the fundamental structure for storing data in a relational database. Think of it as a spreadsheet with rows and columns, where:

  • Columns (also called fields) define the type of data stored
  • Rows (also called records) contain the actual data values
Generated visual representation from text diagram for What is a Table?.
Generated visual representation from text diagram for What is a Table?.

Key Characteristics of Tables

CharacteristicDescription
Physical StorageData is stored on disk
PersistenceData remains after session ends
StructureDefined schema with data types
RelationshipsCan link to other tables via keys
IndexableCan have indexes for fast access
Anatomy of a database table showing rows, columns, cells, primary key, and data types
Anatomy of a database table showing rows, columns, cells, primary key, and data types

Ways to Create Tables

Three Main Approaches to Create Tables

SQL provides several methods to create tables, each suited for different scenarios:

Generated visual representation from text diagram for Ways to Create Tables.
Generated visual representation from text diagram for Ways to Create Tables.

When to Use Each Method

MethodBest For
CREATE + INSERTPermanent tables with precise schema control
CTASQuickly materializing query results, data pipelines
TEMP TablesIntermediate calculations, session-specific data

Tables in the Learning Path

Understanding the Bigger Picture

Tables fit into a broader ecosystem of database objects:

Generated visual representation from text diagram for Tables in the Learning Path.
Generated visual representation from text diagram for Tables in the Learning Path.

What You'll Learn in This Section

  1. CTAS (CREATE TABLE AS SELECT) - Create and populate tables in one statement
  2. Temporary Tables - Session-scoped tables for intermediate processing
  3. When to use what - Decision frameworks for choosing the right approach

Quick Preview

Let's see a quick preview of the table types we'll explore:

-- Preview: This query shows customer counts by state
-- In upcoming lessons, we'll learn to save this as a permanent table (CTAS)
-- or a temporary table for session-specific processing

SELECT 
    customer_state,
    COUNT(*) AS customer_count,
    COUNT(DISTINCT customer_city) AS city_count
FROM olist_customers_dataset
GROUP BY customer_state
HAVING COUNT(*) > 100
ORDER BY customer_count DESC
LIMIT 10;

The query above produces useful aggregated data. In the following lessons, you'll learn how to:

  1. Save this as a CTAS table - CREATE TABLE state_summary AS SELECT ...
  2. Create a temp table - CREATE TEMP TABLE session_stats AS SELECT ...

Each approach has its own advantages and use cases that we'll explore in detail.

Summary

Key Takeaways

  1. Tables are the foundation - They store data physically in your database

  2. Multiple creation methods exist - Each serves different purposes:

    • CREATE TABLE + INSERT for precise control
    • CTAS for quick materialization
    • TEMP tables for session-scoped work
  3. Tables vs Views - Tables store data; Views store queries

  4. Coming up next - We'll compare CTAS with the traditional CREATE + INSERT approach

Quick Reference

ObjectStores Data?Persists?Session-Scoped?
Regular Table✅ Yes✅ Yes❌ No
CTAS Table✅ Yes✅ Yes❌ No
Temp Table✅ Yes❌ No✅ Yes
View❌ No✅ Yes❌ No
CTE❌ No❌ NoQuery only