Data Types — SQL Technical Interview Questions & Answers

Data Types

Data Types

  • A data type is the kind of value a column can hold. INT is a whole number (roll, marks). VARCHAR(50) is short text (name). DATE is a calendar day. DECIMAL(10,2) is money — not FLOAT, because 0.1 + 0.2 in binary floats is messy.
  • Pick the smallest type that fits. Marks 0–100 → TINYINT or INT, not VARCHAR. Phone as VARCHAR, not INT — leading zero and +91 will break. ENUM is a short fixed list (‘pass’,‘fail’) — don’t use it for a list that grows every week.
  • NULL means ‘unknown’, not zero and not empty string. A marks column can be NULL if the exam is not written yet. Don’t write WHERE marks = NULL. Write WHERE marks IS NULL.
  • Board: CREATE TABLE students (roll INT PRIMARY KEY, name VARCHAR(50) NOT NULL, marks INT, dob DATE). That one line teaches types better than a chart of 40 names.
  • Trap: storing money in FLOAT. Trap: VARCHAR(1) for names. Trap: using TEXT for every column ‘just in case’ — indexes and memory suffer.
  • On the example next to this theory: Data Types: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Technical Interview Tip: INT / VARCHAR / DATE / DECIMAL. Show one CREATE TABLE. NULL ≠ 0.

Code Examples for Data Types

Data Types — sample query

-- Data Types
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

Data Types: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

How to Explain Data Types to an Interviewer

What is a data type?

Kind of value a column holds INT, VARCHAR, DATE, DECIMAL

Which type for money?

DECIMAL(10,2) Not FLOAT

How do you test NULL?

IS NULL / IS NOT NULL Never = NULL