KatInterview

Row Removal, Saved Queries and Pairing Every Row in SQL

SQL & DataModel answer
MediumModel answerSQL & Datasqldelete-vs-truncateviews

Last updated

Explain how DELETE and TRUNCATE differ, when a view beats a stored table, and how a CROSS JOIN builds full combination grids, using a small employee and department schema.

You are working with two small tables in a relational database.

employees

idnamedept_id
1Alice10
2Bob20
3Carol10
4Dave30

departments

dept_iddept_name
10Sales
20Marketing
30Engineering

Answer the three conceptual questions below as you would in a technical discussion with a data team. Use the tables above for examples where it helps.

  1. Removing data. How do DELETE and TRUNCATE differ? Consider logging, speed, filtering, triggers, foreign keys, identity counters, rollback and locking. When would you choose each one?
  2. Saved query vs stored data. How does a view differ from a physical table? Give the advantages and drawbacks of each, and say where a materialized view fits.
  3. Pairing every row. What does a CROSS JOIN do? Describe a realistic analytics scenario that needs it, and explain what to watch out for regarding result size.

Where behavior differs between database engines (SQL Server, PostgreSQL, MySQL, Oracle, Redshift), point that out rather than giving one universal answer.

Hints

Hint 1

For the first question, think about what the engine records and locks: row-by-row logging versus releasing whole storage pages, and what that means for WHERE clauses, triggers, foreign keys and auto-increment counters.

Hint 2

For the second question, ask when the work happens. A view runs its query every time it is read; a table pays the cost once at load time. Then consider freshness, indexing, storage, security and updatability.

Hint 3

For the third question, compute the output size as rows in A times rows in B using the sample tables, then think about a case where you need every combination to exist even when no source data exists for it, such as a date by product grid with zero-sales days.