Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
Chaturmind
← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: Fresher to 2 YearsSQL
✓ FreeBeginner· 8 min read

SQL Basics, Keys, Normalization & Transactions — Interview Questions

What SQL is and where it's used, the statement categories (DDL, DML, DQL, DCL, TCL), joins, SELECT, normalization, MySQL data types, primary vs unique vs foreign keys, NULL vs zero, and transactions with ACID.

Published September 25, 2026


How to use this lesson

Backend interviews almost always include SQL, even for Java roles. The basics below are asked quickly, often as a warm-up before a query-writing question. Accuracy matters: mixing up DCL and TCL, or saying "NULL equals NULL", costs easy marks.

Q1. What is SQL?

Short answer: SQL (Structured Query Language) is the standard declarative language for working with relational databases. You describe what data you want (or want to change), and the database's query optimiser decides how to get it. It's used to define schemas, query and modify data, control access, and manage transactions.

Key points to cover:

  • It's standardised (ANSI/ISO), but every database adds its own dialect: MySQL, PostgreSQL, Oracle (PL/SQL) and SQL Server (T-SQL).

Learn it in depth → Practical SQL & JDBC for Interviews

Q2. Where is SQL used?

Short answer:

  • In application backends, through JDBC, JPA or Spring Data, to store and query business data.
  • In reporting and business intelligence.
  • In data engineering and analytics (warehouses such as Snowflake and BigQuery, Spark SQL).
  • In database administration (users, permissions, performance).
  • In data science, for data extraction.

Q3. What are the types of SQL statements?

Short answer:

CategoryPurposeCommands
DDL — Data DefinitionDefine and change structureCREATE, ALTER, DROP, TRUNCATE, RENAME
DML — Data ManipulationChange dataINSERT, UPDATE, DELETE, MERGE
DQL — Data QueryRead dataSELECT (often grouped under DML)
DCL — Data ControlPermissionsGRANT, REVOKE
TCL — Transaction ControlManage transactionsCOMMIT, ROLLBACK, SAVEPOINT

Common trap: saying DCL covers "transactions, locks, commit and rollback". Those belong to TCL. DCL is only about privileges (GRANT/REVOKE).

Key points to cover:

  • TRUNCATE is DDL. In MySQL and Oracle it causes an implicit commit, and can't be rolled back. PostgreSQL allows it inside a transaction.

Q4. What is a join?

Short answer: A join combines rows from two or more tables, based on a related column (usually a foreign key matching a primary key), into a single result. Relational designs split data across tables, and joins put it back together at query time.

SELECT o.id, o.total, c.name
FROM orders o
JOIN customers c ON c.id = o.customer_id;     -- INNER JOIN: only orders that have a matching customer

Key points to cover:

  • The types are INNER, LEFT/RIGHT OUTER, FULL OUTER (not in MySQL), CROSS and self joins. They're covered in the next lesson.

Learn it in depth → SQL Joins

Q5. What is the purpose of the SELECT statement?

Short answer: SELECT retrieves data. It lets you choose columns, filter rows (WHERE), join tables, aggregate (GROUP BY with COUNT, SUM, AVG), filter groups (HAVING), sort (ORDER BY), and limit results (LIMIT/OFFSET).

SELECT department, COUNT(*) AS headcount, AVG(salary) AS avg_salary
FROM employees
WHERE active = TRUE
GROUP BY department
HAVING COUNT(*) >= 5
ORDER BY avg_salary DESC
LIMIT 10;

Key points to cover:

  • Logical order of evaluation: FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT. That explains why a column alias defined in SELECT can't be used in WHERE.
  • Avoid SELECT * in application code. Name the columns, so you transfer less data and schema changes don't break things.

Q6. What is normalization?

Short answer: Normalization organises tables to remove redundancy and the update anomalies it causes, by splitting data into related tables so that each fact is stored once.

Normal formRule (simplified)
1NFAtomic values; no repeating groups or lists in a column
2NF1NF, and every non-key column depends on the whole primary key (matters for composite keys)
3NF2NF, and no non-key column depends on another non-key column (no transitive dependencies)
BCNFEvery determinant is a candidate key

Example: a student_courses table that repeats each instructor's name and email on every row. When the email changes, you must update many rows, and missing one leaves inconsistent data. Moving instructors into their own table, and referencing them by ID, stores each email once. (Strictly, this removes a transitive dependency, course → instructor → email, so it's a 3NF fix.)

Key points to cover:

  • Denormalization (deliberately duplicating data) is a valid trade-off for read-heavy workloads and reporting, but make it a conscious decision.

Learn it in depth → Normalization

Q7. What are the main data types in MySQL?

Short answer:

  • Numeric: TINYINT, INT, BIGINT, DECIMAL(p,s) (exact, use it for money), FLOAT/DOUBLE (approximate).
  • String: CHAR(n) (fixed length), VARCHAR(n) (variable length), TEXT/LONGTEXT, ENUM, BLOB (binary).
  • Date and time: DATE, TIME, DATETIME, TIMESTAMP (stored in UTC, converted to the session time zone), YEAR.
  • Also JSON, BOOLEAN (an alias for TINYINT(1)) and spatial types.

Common trap: storing money in FLOAT or DOUBLE. Rounding errors creep in, so use DECIMAL(19,4) (and BigDecimal in Java).

Q8. What's the difference between a primary key and a unique key?

Short answer: Both enforce uniqueness. A primary key identifies each row. There's only one per table, it can't be NULL, and in InnoDB it's the clustered index that determines the physical row order. A unique key prevents duplicates in other columns. A table can have many unique keys, and they allow NULLs. In MySQL and PostgreSQL, multiple NULLs are allowed, because NULL isn't equal to NULL.

CREATE TABLE users (
    id    BIGINT PRIMARY KEY AUTO_INCREMENT,
    email VARCHAR(255) NOT NULL UNIQUE,     -- a business rule: one account per email
    phone VARCHAR(20) UNIQUE                -- optional, but unique when present
);

Q9. What is a foreign key constraint?

Short answer: A foreign key makes a column's values reference the primary (or unique) key of another table. The database then enforces referential integrity: you can't insert a row pointing to a non-existent parent, or delete a parent that's still referenced, unless you define an ON DELETE action.

CREATE TABLE enrollments (
    student_id BIGINT NOT NULL,
    course_id  BIGINT NOT NULL,
    PRIMARY KEY (student_id, course_id),
    FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE,
    FOREIGN KEY (course_id)  REFERENCES courses(id)                      -- default: RESTRICT
);

Key points to cover:

  • ON DELETE actions: RESTRICT/NO ACTION (block the delete), CASCADE (delete the children too), SET NULL.
  • Index your foreign-key columns. Joins and parent deletes need them. InnoDB creates the index automatically. PostgreSQL doesn't.

Q10. What's the difference between NULL and zero?

Short answer: Zero is a known numeric value. NULL means "unknown or missing". It isn't a value at all, and it behaves differently everywhere:

  • Arithmetic with NULL gives NULL (NULL + 1 → NULL).
  • Comparisons with NULL give UNKNOWN, so WHERE amount = NULL matches nothing. Use IS NULL / IS NOT NULL.
  • Aggregates skip NULLs: COUNT(amount) counts only non-NULL values, while COUNT(*) counts every row. AVG ignores NULLs.
-- payments: amount NULL = not yet charged, amount 0 = attempted but nothing charged
SELECT COUNT(*) AS all_rows, COUNT(amount) AS charged_or_zero, SUM(COALESCE(amount, 0)) AS total
FROM payments;

Key points to cover:

  • COALESCE(col, default) (or MySQL's IFNULL) substitutes a value for NULL.
  • In Java, map nullable numeric columns to wrapper types (Integer, BigDecimal), not primitives.

Q11. What is a database transaction?

Short answer: A transaction is a group of operations executed as one logical unit: either all of them take effect (commit) or none do (rollback). Transactions provide the ACID guarantees:

  • Atomicity: all or nothing.
  • Consistency: constraints hold before and after.
  • Isolation: concurrent transactions don't see each other's partial work.
  • Durability: committed changes survive crashes.
START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1 AND balance >= 500;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT;          -- or ROLLBACK if any step fails, so the money is never lost halfway

Key points to cover:

  • In Spring, @Transactional wraps a service method in exactly this, rolling back on runtime exceptions.
  • Isolation levels (READ COMMITTED, REPEATABLE READ, the MySQL default, and SERIALIZABLE) trade consistency against concurrency.

Learn it in depth → ACID Properties

Follow-up questions this topic invites — and their answers

Q: What is a candidate key? A super key? An alternate key? A: A super key is any set of columns that uniquely identifies a row. A candidate key is a minimal super key. The primary key is the candidate key you choose. The other candidate keys are alternate keys, usually enforced with UNIQUE.

Q: What is a surrogate key vs a natural key? A: A natural key comes from the business domain (email, PAN or ISBN). A surrogate key is system-generated (auto-increment or UUID), with no business meaning. Surrogate keys are stable, even when business data changes, so they're the usual choice for primary keys. Keep natural keys UNIQUE.

Q: Can a foreign key reference a column that isn't a primary key? A: Yes, as long as that column is UNIQUE, or a primary key.

Q: Is COUNT(1) faster than COUNT(*)? A: No. Modern databases treat them identically. COUNT(column) is the one that differs, because it skips NULLs.

Previous

Hibernate Performance, Mapping & Scenarios — Interview Questions

Next

SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions

AI Tutor

Lesson: SQL Basics, Keys, Normalization & Transactions — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.