RadhaPublication
HomeInterviewLearning PathsRole TracksMockBooksPricing
Login / Sign UpLog in

© 2026 Radha Publication. All rights reserved.

Shop on AmazonDonateContactPrivacyTerms
SQL questions
EasySQL#157 min readJul 11, 2026

CREATE

ddl
practice
learning
sql-fundamentals
Practice modeTest yourself instead of reading straight through

Hook: CREATE is the moment you draw the blueprint before a database can hold a single row.

Question: What does CREATE do in SQL?

Answer: CREATE is the SQL command family used to define new database objects, such as tables, indexes, views, and schemas. It belongs to DDL (Data Definition Language), which means it changes structure, not the row data itself. You use it when setting up a database, adding a new object during a migration, or preparing a faster path for queries.

Interview-Ready Answer: I use CREATE to define database structure before any data is stored. For example, CREATE TABLE sets up columns, data types, and constraints like PRIMARY KEY and NOT NULL, while CREATE INDEX helps reads go faster. One detail I always mention is that creating an empty table is usually just a quick metadata change, but creating an index on existing data can take much longer because the engine has to scan and organize the rows.

🧠 Memory Map
Memory map — visual summary of this topic

What CREATE really means

CREATE is the database’s way of saying, “I need a new thing to exist.” That thing might be a table for rows, a view for a saved query, an index for faster lookups, or a schema for organizing names. In simple words: CREATE builds the skeleton first, and data comes later.

How it works under the hood

  1. The SQL parser reads the statement and identifies the object type, such as table or index.
  2. The engine checks permissions, because you usually need create rights on a database or schema.
  3. The engine validates the object name, column definitions, data types, and constraints like PRIMARY KEY, UNIQUE, and CHECK.
  4. If the name already exists, the database either raises an error or follows a dialect-specific rule such as IF NOT EXISTS.
  5. The engine writes metadata into the system catalog, which is the database’s internal registry of objects and definitions.
  6. For a table, the engine allocates the logical structure. For an index, it often builds a separate lookup structure over existing rows, which is why index creation can be expensive.
  7. The object becomes visible to later queries, and in some databases the operation is transactional, while in others some DDL causes an implicit commit.

When and why to use it

  • Bootstrap: create the first tables and schemas for a new application.
  • Migrations: add a column, table, view, or index as the app evolves.
  • Performance: create an index when a query repeatedly filters or joins on the same column.
  • Abstraction: create a view to hide complex joins behind a simple name.

Common CREATE object types

ObjectCreatesBest useWatch out
TABLEStored rowsCore dataConstraints matter
VIEWSaved querySimpler readsNo stored rows
INDEXLookup pathFaster searchSlower writes
SCHEMAName spaceOrganize objectsPermission issues

Memory model: think of a database like a building site. CREATE TABLE is pouring the foundation, CREATE INDEX is adding a signposted hallway for quick access, and CREATE VIEW is putting a labeled door over a complicated room.

Performance and complexity notes

  • CREATE TABLE on an empty table is usually very fast, often milliseconds, because it mainly writes metadata.
  • CREATE INDEX on a populated table is more expensive. In practice it is often close to O(n) or O(n log n) work depending on the engine and index method, because every existing row may need to be read and organized.
  • Indexes cost storage. A b-tree index can add noticeable overhead, sometimes tens of percent of the indexed column footprint, and it also slows inserts, updates, and deletes because the index must be maintained.
  • Some databases lock the object or schema while creating it. On a large table, an index build can take seconds to minutes and may block writes unless the engine supports online creation.

Important edge cases

  • Name collisions: if an object already exists, plain CREATE fails. IF NOT EXISTS can prevent the error in many systems, but it can also hide schema drift if the existing object is not shaped the way you expect.
  • Defaults vs NULL: a DEFAULT value is used only when the column is omitted. If you explicitly insert NULL into a nullable column, the default is not used.
  • Dialect differences: some features, like CREATE OR REPLACE, are common for views and functions but not universal for tables. Syntax varies across PostgreSQL, MySQL, SQL Server, and SQLite.
  • Transactions: do not assume every database handles DDL the same way. Some engines fully support rolling back CREATE statements, while others commit automatically or partially commit around schema changes.

In an interview, the strongest simple sentence is: CREATE defines structure first; it does not insert data.

Real-world story

A checkout service in an e-commerce app needs a new orders table before launch. The team ships the application code that writes orders, but forgets to run the migration that contains CREATE TABLE orders .... As soon as traffic hits production, the app starts failing with errors like relation orders does not exist or no such table: orders. Users see failed payments, retries spike, and logs fill with database errors.

What often goes wrong is not the idea of CREATE itself, but the details inside it. If the table is created without NOT NULL or UNIQUE constraints, the app may accept bad data silently: duplicate order IDs, missing customer IDs, or negative totals. That bug may not crash immediately, but it shows up later as reconciliation mismatches, support tickets, and confusing analytics.

In production, schema creation is usually handled by migrations, not by the app on every request. That keeps startup predictable, avoids race conditions, and makes the change reviewable.

SQL
-- Create a small table from scratch, then load clean data and show one failure path.
-- This is deliberately simple and portable: no vendor-specific extensions are required.

CREATE TABLE products (
    product_id INTEGER NOT NULL PRIMARY KEY,
    sku        VARCHAR(20) NOT NULL UNIQUE,
    name       VARCHAR(100) NOT NULL,
    price      DECIMAL(10,2) NOT NULL CHECK (price >= 0),
    active     INTEGER NOT NULL DEFAULT 1
);

-- An index is another CREATE object: it speeds up lookups on a column.
CREATE INDEX idx_products_active ON products (active);

INSERT INTO products (product_id, sku, name, price, active) VALUES
    (1, 'SKU-100', 'Keyboard', 49.99, 1),
    (2, 'SKU-200', 'Mouse', 19.99, 1),
    (3, 'SKU-300', 'Old Monitor', 129.00, 0);

-- Failure path: the CHECK constraint protects the table from bad business data.
-- If you run this line, the database should reject it because price must be >= 0.
-- INSERT INTO products (product_id, sku, name, price, active)
-- VALUES (4, 'SKU-400', 'Broken Item', -5.00, 1);

-- Failure path: the PRIMARY KEY protects uniqueness of the row identity.
-- If you run this line, the database should reject it because product_id = 1 already exists.
-- INSERT INTO products (product_id, sku, name, price, active)
-- VALUES (1, 'SKU-999', 'Duplicate Key', 10.00, 1);

SELECT product_id, sku, name, price, active
FROM products
ORDER BY product_id;

Follow-up & Tricky Questions

  • What is the difference between DDL and DML? DDL changes structure, like CREATE and ALTER; DML changes rows, like INSERT, UPDATE, and DELETE. That distinction matters because DDL often has different locking and transaction behavior.
  • When would you use CREATE INDEX? Use it when a query repeatedly filters, sorts, or joins on the same column and the table is large enough that full scans are too slow. The trade-off is faster reads versus extra storage and slower writes.
  • What happens if you run CREATE TABLE twice? Without protection, the second run usually errors because the name already exists. Many teams use migrations or IF NOT EXISTS to make startup safer.
  • Does CREATE TABLE insert data? No, it only defines the structure. Data is added later with INSERT.
  • How is CREATE VIEW different from CREATE TABLE? A table stores rows physically, while a view stores the query definition. A view is useful when you want a reusable, simplified read model.
  • Tricky: does IF NOT EXISTS guarantee the object is correct? No. It only prevents an error if something with that name already exists; it does not verify that the columns, constraints, or indexes match your intended design.
  • Tricky: is CREATE INDEX always a free performance win? No. It helps reads, but it costs space and slows writes because every insert, update, or delete must maintain the index.
  • Tricky: can every database roll back CREATE? Not reliably. Transaction support for DDL varies by engine, so never assume schema changes behave exactly like row-level updates in every system.

Common Mistakes:

  • Confusing structure with data. Correction: CREATE defines objects; INSERT adds rows.
  • Skipping constraints. Correction: use PRIMARY KEY, NOT NULL, UNIQUE, and CHECK to stop bad data early.
  • Creating too many indexes. Correction: every index helps reads only when it matches real queries; too many indexes slow down writes.
  • Assuming identical syntax everywhere. Correction: SQL is standardized, but CREATE options vary by database dialect.

Memory Hook: Blueprint before bricks. CREATE draws the blueprint; data comes later.

Cheat Sheet:

  • CREATE is DDL: it defines structure.
  • Most common objects: table, view, index, schema.
  • CREATE TABLE sets columns, types, defaults, and constraints.
  • CREATE INDEX speeds reads but costs storage and write time.
  • Empty object creation is usually fast; index creation on large data can be expensive.
  • Watch for dialect differences and transaction behavior.

Practice Tasks:

  • Create a customers table with a primary key, a unique email, and a non-negative age check.
  • Add an index on the column you would filter most often, then explain why it helps one query and hurts one write path.
  • Create a small view that hides a join between two tables, then compare it to reading from the base tables directly.
Previous
Back to Questions←→to navigate
Next

Recommended Resources

Recommended Book

Cracking the Coding Interview

189 Programming Questions and Solutions

Recommended Book

Cracking the Coding Interview

189 Programming Questions and Solutions

-- Create a small table from scratch, then load clean data and show one failure path. -- This is deliberately simple and portable: no vendor-specific extensions are required. CREATE TABLE products ( product_id INTEGER NOT NULL PRIMARY KEY, sku VARCHAR(20) NOT NULL UNIQUE, name VARCHAR(100) NOT NULL, price DECIMAL(10,2) NOT NULL CHECK (price >= 0), active INTEGER NOT NULL DEFAULT 1 ); -- An index is another CREATE object: it speeds up lookups on a column. CREATE INDEX idx_products_active ON products (active); INSERT INTO products (product_id, sku, name, price, active) VALUES (1, 'SKU-100', 'Keyboard', 49.99, 1), (2, 'SKU-200', 'Mouse', 19.99, 1), (3, 'SKU-300', 'Old Monitor', 129.00, 0); -- Failure path: the CHECK constraint protects the table from bad business data. -- If you run this line, the database should reject it because price must be >= 0. -- INSERT INTO products (product_id, sku, name, price, active) -- VALUES (4, 'SKU-400', 'Broken Item', -5.00, 1); -- Failure path: the PRIMARY KEY protects uniqueness of the row identity. -- If you run this line, the database should reject it because product_id = 1 already exists. -- INSERT INTO products (product_id, sku, name, price, active) -- VALUES (1, 'SKU-999', 'Duplicate Key', 10.00, 1); SELECT product_id, sku, name, price, active FROM products ORDER BY product_id;