Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

create-table

CREATE TABLE

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
beginner

This lesson

This lesson teaches CREATE TABLE: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

Teams query CREATE TABLE on every SQL codebase—skipping it leaves gaps in debugging and data reviews.

You will apply CREATE TABLE in contexts like: Application releases, zero-downtime deploys, and schema reviews in Django, Laravel, or Rails.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

When you can explain the previous lesson's ideas without copying example queries verbatim.

CREATE TABLE defines a new relation: column names, types, defaults, and table-level options. Migrations in Django and other frameworks emit equivalent DDL.

Basic table

CREATE TABLE products (
  id INTEGER PRIMARY KEY,
  sku TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL,
  price REAL NOT NULL DEFAULT 0,
  created_at TEXT DEFAULT (datetime('now'))
);

Practice: Run DDL in a fresh SQLite file or DB Fiddle schema pane. Drop test tables when experimenting.

ALTER TABLE

ALTER TABLE products ADD COLUMN stock INTEGER DEFAULT 0;

-- SQLite limited ALTER; Postgres/MySQL support more renames/types
-- See /postgresql/intro and /mysql/intro for dialect details

Production schema changes go through reviewed migrations, not ad-hoc ALTER on live databases.

DROP and IF NOT EXISTS

CREATE TABLE IF NOT EXISTS products (
  id INTEGER PRIMARY KEY,
  name TEXT
);

DROP TABLE IF EXISTS old_products;

IF NOT EXISTS / IF EXISTS make scripts rerunnable in dev—still use caution in production.

Important interview questions and answers

  1. Q: CREATE vs migration tool?
    A: CREATE is raw DDL; migration tools version and apply DDL changes safely across environments.
  2. Q: DEFAULT clause purpose?
    A: Supplies a value when INSERT omits the column.

Self-check

  1. Which clause makes a column mandatory on INSERT?
  2. What does PRIMARY KEY imply about uniqueness?

Tip: Version schema changes through migrations—see Django migrations lesson later.

Interview prep

PRIMARY KEY implies?

Unique, NOT NULL row identifier with automatic index in most engines.

DEFAULT clause?

Value used when INSERT omits the column.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • IF NOT EXISTS?
  • Naming convention?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump