Expert: design, speed and engines · Interview questions and practice projects · topic 60 of 62 · 35 min
Theory questions you will be asked
Explanation
How to answer: define, example, trade-off
A strong answer is short and has three parts. Define the idea in one sentence. Give a tiny example from a real table. Name a trade-off or a trap ("but TRUNCATE cannot have a WHERE"). This shows you understand it, not just remember it. If you do not know an engine detail, say so and say how you would check.
DELETE vs TRUNCATE vs DROP
| DELETE | TRUNCATE | DROP | |
|---|---|---|---|
| Removes | Chosen rows (WHERE) or all rows | All rows | The whole table: rows and structure |
| Speed on big tables | Slower: works row by row | Fast: frees the data pages | Fast |
| Row triggers | Fire | Do not fire DELETE triggers | Not applicable |
| Roll back? | Yes, inside a transaction | PostgreSQL and SQL Server: yes inside a transaction; MySQL and Oracle: no (implicit commit) | Depends on the engine; treat as permanent |
| Auto-increment | Keeps counting | Usually resets (PostgreSQL only with RESTART IDENTITY) | Gone with the table |
SQLite has no TRUNCATE; DELETE FROM t with no WHERE is optimised to do the same job.
Quick pairs
- WHERE vs HAVING: WHERE filters rows before grouping; HAVING filters groups after aggregation. WHERE city = 'Pune' vs HAVING COUNT(*) > 2.
- UNION vs UNION ALL: UNION removes duplicate rows (extra sort or hash work); UNION ALL keeps every row and is faster. Use UNION ALL when duplicates are impossible or wanted.
- Primary key vs unique key: one primary key per table, never NULL, identifies the row. A table can have many unique keys, and they may hold NULLs (PostgreSQL, MySQL and SQLite allow several NULLs; SQL Server's unique index allows only one).
- View vs table: a table stores data; a view is a saved SELECT that runs when you query it. A materialized view (PostgreSQL, Oracle) stores the result and must be refreshed.
- CHAR vs VARCHAR: CHAR(n) is fixed length, padded with spaces (good for fixed codes like a 2-letter state code); VARCHAR(n) stores only what you put in. In PostgreSQL there is no speed benefit to CHAR.
- OLTP vs OLAP: OLTP (online transaction processing) runs many small reads and writes, like placing orders; OLAP (online analytical processing) runs big read-only reports over history, often in a warehouse with column storage.
Clustered vs non-clustered index
A clustered index decides the physical order of the table's rows: the table is stored inside the index, so there can be only one. A non-clustered index is a separate structure that points back to the rows; a table can have many. In SQL Server, the primary key creates the clustered index by default. MySQL's InnoDB always clusters the table on its primary key. PostgreSQL is different: tables are heaps (rows in no particular order) and every index is separate; the CLUSTER command reorders a table once but does not keep it ordered. Module 10 covers how indexes work.
Normalisation and ACID in one breath
Normalisation (module 9) removes repeated data so each fact is stored once: 1NF has atomic values, 2NF removes partial dependencies on part of a composite key, 3NF removes dependencies between non-key columns. The trade-off: more joins; reporting systems sometimes denormalise on purpose. ACID (module 8): Atomic (all or nothing), Consistent (rules hold), Isolated (transactions do not see each other's half-done work), Durable (committed data survives a crash).
Isolation levels
| Level | Dirty read | Non-repeatable read | Phantom read |
|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed | No | Possible | Possible |
| Repeatable Read | No | No | Possible in the standard |
| Serializable | No | No | No |
A dirty read sees another transaction's uncommitted change. A non-repeatable read gets a different value when reading the same row twice. A phantom read finds new rows appearing in a repeated range query. Defaults differ: PostgreSQL, SQL Server and Oracle use Read Committed; MySQL InnoDB uses Repeatable Read. Engines also go beyond the standard table: PostgreSQL's Repeatable Read does not allow phantoms.
Common mistakes
- Saying "TRUNCATE can never be rolled back". It depends on the engine; say so.
- Saying "a unique key cannot be NULL". Most engines allow NULLs in a unique column.
- Saying PostgreSQL has clustered indexes like SQL Server. It has heaps plus a one-time CLUSTER command.
- Reciting definitions with no example. Always add a five-second example.
- Bluffing an engine detail. "I believe the default is X; I would confirm in the docs" is a good answer.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Theory questions you will be asked · 1/8
Theory questions are short, but they test whether you really understand SQL. Use a simple three-step answer.
Prefer a video by a teacher? Find free videos on “sql interview theory questions delete truncate drop clustered index isolation levels” on YouTube ↗
In real life: A typical data analyst interview round
Many data analyst and backend interviews in India start with 10 to 15 minutes of SQL theory ("difference between WHERE and HAVING", "what is a primary key") before moving to two or three live queries on a shared screen. Interviewers say short, structured answers with an example stand out. Practising the define, example, trade-off pattern aloud for the pairs in this topic covers most of that first part.
Practice (0/4 done)
An interviewer asks: "You need to empty a 50-million-row log table quickly and do not need its triggers to fire. Which command?" What is the best answer?
Which statement about clustered indexes is correct?
Both SHOP and COMPANY are loaded. Return every city that appears in customers.city or departments.city, each city once, sorted A to Z (one column: city). (UNION ALL would give 11 rows; you need the de-duplicated list.)
Say these aloud as if to an interviewer, about 30 seconds each, using define → example → trade-off: (1) WHERE vs HAVING, (2) primary key vs unique key, (3) what ACID means, (4) OLTP vs OLAP. Then compare with the sample answers.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Project: a railway booking database (IRCTC-like)Interview questions and practice projects · same module
- Classic interview queries, part 1Interview questions and practice projects · same module
- Transactions and ACIDBackend Development · SQL in depth
- Normalisation and table designBackend Development · SQL in depth
- Views and materialized viewsIndexes, query plans and views
- Normalisation: 1NF, 2NF, 3NF and BCNFSchema design and normalisation
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which is true about UNION and UNION ALL?
Answer: UNION removes duplicate rows; UNION ALL keeps them and is usually faster
UNION does extra work to remove duplicates.
Which isolation level prevents dirty reads but still allows non-repeatable reads?
Answer: Read Committed
Read Committed only shows committed data, but a row can change between two reads.
What is the default isolation level of MySQL InnoDB?
Answer: Repeatable Read
InnoDB defaults to Repeatable Read; PostgreSQL, SQL Server and Oracle default to Read Committed.
A weekly sales report over five years of data, run by the business team, is typical of...
Answer: OLAP
Big read-only analysis over history is OLAP.
What does a (non-materialized) view store?
Answer: Only the saved query; it runs when you select from the view
A view is a saved SELECT. Materialized views store results and need refreshing.
What is the default isolation level of PostgreSQL?Related
Answer: READ COMMITTED
PostgreSQL, SQL Server and Oracle default to READ COMMITTED.
Which ACID property means "after the app shows Payment successful, a power cut cannot undo it"?Related
Answer: Durable
Durable: once committed, the change is safely stored and survives crashes.
What is the safest way to run a DELETE on a live table?Related
Answer: Preview the same WHERE with a SELECT, then run the DELETE, ideally inside a transaction
Check the rows and the count first, and keep a way back.
A course fee is stored on every enrollment row. The fee changes and one row is missed. What is this problem called?Related
Answer: An update anomaly
Repeated facts lead to update anomalies: some copies change and others do not.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Interview questions and practice projects” before its exam: Project: a railway booking database (IRCTC-like), Project: e-commerce analytics, and your next steps.
- Module 12 exam: interview questions and projects · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- 62 topics left in the course before the final exam.