Interview Q&A · SQL · 8 questions in this topic
Tables, DDL & Basic DML — SQL Interview Q&A
Creating tables, inserting rows, reading them back, and removing duplicates — the write/read foundation. Part of SQL Fundamentals · 192 questions in the SQL bank.
How do you create a table (CREATE TABLE)?
Answer
Name columns with types, mark the key, add constraints and defaults in one statement: CREATE TABLE with column definitions (name, type, constraints), a PRIMARY KEY, and any FOREIGN KEY / UNIQUE / CHECK / DEFAULT clauses. Keep names stable — renames ripple through queries, ORMs and dumps.
IF NOT EXISTS makes migrations re-runnable; explicit types beat lazy TEXT-everywhere.
Example
CREATE TABLE IF NOT EXISTS employees ( id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, department VARCHAR(50) DEFAULT 'General', salary NUMERIC(12,2) CHECK (salary >= 0) );
💡 One statement with types, key and a constraint is the complete interview example.
How do you insert rows (INSERT INTO)?
Answer
List target columns explicitly and supply matching VALUES rows — explicit columns survive schema changes that positional INSERT breaks on. Multi-row VALUES inserts in one statement; INSERT ... SELECT copies query results directly.
Always name columns: INSERT INTO t VALUES (...) without a list is a future bug.
Example
INSERT INTO employees (id, name, department, salary) VALUES (1, 'Alice', 'Engineering', 85000), (2, 'Bob', 'Sales', 52000); INSERT INTO archive SELECT * FROM employees WHERE age > 60;
How do you read data back (SELECT, SELECT *)?
Answer
SELECT lists the columns (or * for all), FROM names the table. SELECT * is for exploration; production code names columns — stable against schema changes, narrower over the wire, and immune to column-order surprises.
Projection (choosing columns) plus filtering (WHERE) plus ordering is 90% of real queries.
Example
SELECT name, salary FROM employees; -- production: named columns SELECT * FROM employees; -- exploration only
💡 SELECT * for ad-hoc, named columns for code is the one-line doctrine.
How do you remove duplicates (DISTINCT)?
Answer
DISTINCT collapses fully-duplicate result rows into one — it applies to the whole selected row, not one column. SELECT DISTINCT department returns each department once. For per-group picks (latest row per user), DISTINCT is the wrong tool — use GROUP BY aggregates, DISTINCT ON (PostgreSQL), or window functions.
DISTINCT sorts/hashes the result, so it costs on huge outputs — filter first.
Example
SELECT DISTINCT department FROM employees;
SELECT COUNT(DISTINCT department) FROM employees;How do you modify table structure (ALTER TABLE)?
Answer
ALTER TABLE ADD COLUMN (with a DEFAULT so existing rows stay valid), DROP COLUMN, ALTER COLUMN TYPE (may rewrite the table), RENAME, and ADD/DROP CONSTRAINT. Structural changes can lock big tables — production migrations prefer online/concurrency-safe variants and backfill strategies.
Always test migrations on a copy: type changes and constraint additions fail on dirty data.
Example
ALTER TABLE employees ADD COLUMN bonus NUMERIC(12,2) DEFAULT 0; ALTER TABLE employees ALTER COLUMN salary TYPE NUMERIC(14,2);
💡 ADD with DEFAULT keeps old rows valid — the safe-migration detail interviewers want.
How do you delete tables and data (DROP vs TRUNCATE vs DELETE)?
Answer
DROP TABLE removes the table definition itself. TRUNCATE empties all rows fast (deallocates pages, resets sequences, usually non-transactional in MySQL). DELETE removes selected rows row-by-row inside a transaction (with WHERE — without it, everything goes).
Safety ladder: DELETE with WHERE for surgery, TRUNCATE for full resets, DROP for structure removal — each with backups verified first.
Key points
- 1DROP — removes the table itself.
- 2TRUNCATE — fast full empty, minimal logging.
- 3DELETE — selective, transactional, logged.
Example
DELETE FROM employees WHERE id = 99; -- one row, rollback-able TRUNCATE TABLE staging; -- fast full reset
What is the difference between `VARCHAR`, `CHAR`, `TEXT`?
Answer
CHAR(n) is fixed-width, blank-padded — legacy fixed formats. VARCHAR(n) stores variable text up to n with a length limit enforced. TEXT is unbounded variable text (same storage as VARCHAR in PostgreSQL, off-row past sizes in others).
PostgreSQL guidance: TEXT (or VARCHAR without length) plus CHECK constraints for real limits — arbitrary (255) caps are folklore, not engineering.
Example
code CHAR(5), -- 'AB' stored padded name VARCHAR(100), -- up to 100, enforced notes TEXT -- unbounded
How do you handle dates, times and time zones?
Answer
Store moments as TIMESTAMPTZ (instant in time, displayed per zone) and plain dates as DATE; never store moments as VARCHAR (sorting/comparison breaks) or naive TIMESTAMP across zones. Convert at the edges: UTC in storage, local in display.
Daylight-saving gaps/overlaps make naive arithmetic wrong twice a year — zoned types plus interval math exist for exactly this.
Example
created_at TIMESTAMPTZ DEFAULT now(), SELECT created_at AT TIME ZONE 'Europe/Berlin' FROM orders;
Recommended resourcesAffiliate links · may earn us a commission