Interview Q&A · SQL · 8 questions in this topic

Tables, DDL & Basic DML — SQL Interview Q&A

All SQL topics

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

Example.sqlsql
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

Example.sqlsql
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

Example.sqlsql
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.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits