CREATE Table — SQL Technical Interview Questions & Answers

CREATE Table

CREATE Table

  • CREATE TABLE students (roll INT PRIMARY KEY, name VARCHAR(50) NOT NULL, marks INT); draws the register: column names, types, and rules. PRIMARY KEY means each roll is unique and not NULL. NOT NULL on name means you cannot insert a student without a name.
  • Order: database selected (USE), then CREATE TABLE, then INSERT rows, then SELECT. Empty table is normal after CREATE — it is not an error.
  • CREATE TABLE marks (id INT PRIMARY KEY AUTO_INCREMENT, roll INT, subject VARCHAR(30), score INT, FOREIGN KEY (roll) REFERENCES students(roll)); now two tables are related.
  • Trap: no PRIMARY KEY. Trap: VARCHAR with no length in old habits. Trap: reserved words as column names (date, order) — use backticks or a better name (order_date).
  • On the example next to this theory: CREATE TABLE defines columns and constraints; then INSERT sample rows.
CREATE TABLE students
  ┌──────┬────────┬───────┐
  │ roll │ name   │ marks │
  ├──────┼────────┼───────┤
  │  (empty until INSERT) │
  └──────┴────────┴───────┘

Technical Interview Tip: Show students(roll PK, name, marks). Say empty table after CREATE is OK.

Code Examples for CREATE Table

CREATE Table — sample query

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT UNIQUE
);
INSERT INTO users (id, name, email) VALUES (1, 'Asha', 'a@x.com');
SELECT * FROM users;

CREATE TABLE defines columns and constraints; then INSERT sample rows.

How to Explain CREATE Table to an Interviewer

Write a tiny CREATE TABLE.

students(roll INT PRIMARY KEY, name VARCHAR(50), marks INT)

Why PRIMARY KEY?

Uniquely identifies a row Cannot be NULL