Scrivio Interview library

Relational data modeling

44 questions in 11 subtopics.

Entity relationship modeling

ConceptFoundations6 min read
What is the difference between an identifying and a non-identifying relationship in an ER model? Give a table example of each.
DesignFoundations6 min read
Model a small library system: members borrow copies of books, a book has many authors, and a member can reserve a book (not a specific copy). Name the entities, their relationships and cardinalities, and the tables you would create.
ScenarioIntermediate6 min read
A product owner describes an 'account' that sometimes means a login, sometimes a billing customer and sometimes a company with several users. The current schema has one accounts table with 60 nullable columns. How do you untangle the model?
DesignAdvanced6 min read
Several teams each own a part of the customer data (identity, billing, support, marketing) in separate services and databases. How do you model 'customer' across the organization without one shared customer table?
LeadershipPrincipal6 min read
Your company's core data model was designed 15 years ago for one product and now constrains three new product lines. Leaders ask whether to remodel. How do you decide and run such a long-lived data model change?

Primary, foreign, natural and surrogate keys

ConceptFoundations6 min read
Explain natural keys and surrogate keys with an example of each. Why do many teams use a surrogate primary key even when a natural key exists, and what should still be enforced on the natural key?
CodingFoundations7 min read
Write PostgreSQL DDL for orders and order_lines where an order line belongs to exactly one order, deleting an order deletes its lines, a product referenced by a line cannot be deleted, and a line number is unique within an order.
DebuggingIntermediate6 min read
Deleting a single row from customers takes 40 seconds and blocks other writes. The table has 2 million rows, and orders(customer_id) references customers(id). What is the likely cause and fix?
DesignAdvanced7 min read
Your platform team must set a standard for primary keys and foreign keys across 150 services, some of which share data through events and exports. What would the standard say?

Normalization 1NF to BCNF

ConceptFoundations6 min read
A table enrollments(student_id, course_id, student_name, course_title, instructor, grade) has primary key (student_id, course_id). Which normal forms does it violate and how would you decompose it?
DebuggingFoundations6 min read
A users table has a column phone_numbers storing values like '555-1234,555-9876'. Support reports they cannot find users by a second phone number and updates sometimes drop a number. What is wrong with the model and what would you change?
ConceptIntermediate6 min read
Explain the difference between 3NF and BCNF using a table where it matters, for example bookings(student, subject, tutor) where each tutor teaches one subject and a student has one tutor per subject.
LeadershipPrincipal6 min read
Normalization theory dates from the 1970s, and today many teams use document databases and event logs. How do you explain to a modern engineering organization where normalization still matters and where it does not?

Denormalization trade-offs

ConceptFoundations6 min read
What is denormalization, and give two examples where it is a good idea and one where it causes bugs.
ScenarioIntermediate7 min read
An order list page joins orders, customers, addresses and order_lines to show the customer name and order total, and is slow at 5000 requests per second. A teammate proposes copying customer_name and total into orders. How do you evaluate this?
DesignAdvanced7 min read
Your product needs operational screens over the normalized OLTP schema and also heavy reporting. Teams keep adding denormalized columns and triggers to the OLTP database for reports. What architecture do you propose?
LeadershipPrincipal6 min read
Across your company, teams built dozens of denormalized copies of product and customer data in caches, search clusters and service databases. Incidents from stale data are rising. What principles and investments do you set?

Constraints and referential integrity

CodingFoundations7 min read
Add constraints to a PostgreSQL table bookings(id, room_id, starts_at, ends_at, status) so that ends_at is after starts_at, status is one of 'held', 'confirmed', 'cancelled', and two non-cancelled bookings of the same room cannot overlap.
ScenarioIntermediate6 min read
A team says 'we do not use foreign keys because they slow down writes and our ORM handles integrity'. A data audit finds 3 percent of order lines point to missing products. How do you respond?
LeadershipAdvanced6 min read
Your company has many databases where integrity is enforced only in application code, and data quality incidents keep reaching finance reports. How would you raise integrity standards without stopping delivery?
DesignPrincipal6 min read
Twenty years ago many systems kept business rules in stored procedures and triggers; then the industry moved rules into application code. Where should integrity rules live today, and how do you decide for a new platform?

Modeling many-to-many and hierarchies

CodingFoundations7 min read
Model a category tree of unlimited depth in PostgreSQL with an adjacency list, and write a query that returns all descendants of category 10 with their depth.
DesignIntermediate6 min read
Users can follow other users, and the relationship needs a created_at, a notification setting and fast lookups of both 'who do I follow' and 'who follows me' for users with millions of followers. Design the table and indexes.
DesignAdvanced7 min read
An organisation chart service must answer 'all reports under this manager' and 'the management chain of this person' quickly for 300,000 employees, with reorganisations moving thousands of people at once. Which hierarchy model would you choose?

Soft deletes and audit columns

ConceptFoundations6 min read
A table uses a deleted_at column for soft deletes. What problems does this cause for unique constraints and queries, and how do you handle them?
CodingIntermediate6 min read
Implement soft delete for a JPA entity Customer in Hibernate 6 so that repository.delete(customer) sets deleted_at instead of deleting, and normal queries hide deleted rows. What are the gotchas?
DesignIntermediate6 min read
Which audit columns would you add to business tables (created_at, updated_at, created_by and so on), how do you populate them reliably, and when are they not enough?
LeadershipPrincipal6 min read
Legal asks engineering to guarantee that customer data is fully deleted within 30 days of a request across all systems, but most services use soft deletes, backups last 90 days and data is copied into analytics. How do you lead this?

Temporal data and history tables

ConceptFoundations6 min read
What is a history table and how does it differ from storing valid_from and valid_to on the main table? Give one way to populate a history table.
DesignIntermediate7 min read
Product prices change over time and invoices must show the price that applied on any past date. Design the schema and the query for 'price of product 42 on 2026-03-15'.
ScenarioAdvanced8 min read
Regulators require you to show what your system believed a customer's risk rating was on any past date, including later corrections to earlier data. Which temporal model do you need and how would you implement it?
DesignPrincipal6 min read
Leadership wants 'full history of everything' kept forever for analytics and audits, across all operational databases. What do you advise about cost, design and governance?

UUID vs sequence keys

ConceptFoundations6 min read
Compare bigint sequence keys and UUID keys for a PostgreSQL table. When would you choose each, and what changes with UUIDv7?
CodingFoundations6 min read
In a Spring Boot 3 JPA entity using Hibernate 6, write the id mapping for (a) a PostgreSQL sequence with batched inserts and (b) a UUID generated by the application. Why does GenerationType.IDENTITY hurt batching?
DebuggingIntermediate7 min read
After switching primary keys from bigint identity to random UUIDv4 stored as CHAR(36) in MySQL 8.4 InnoDB, insert throughput drops by half and the buffer pool hit rate falls. Explain why and fix it.
LeadershipAdvanced6 min read
Your organisation is debating a company-wide rule: 'all primary keys are UUIDv7'. Some teams have large MySQL tables with bigint keys. How would you decide and roll out such a rule?

Modeling money, time zones and enums

ConceptFoundations7 min read
How would you model an order status enum in the database: a native PostgreSQL ENUM type, a VARCHAR with a CHECK constraint, or a lookup table? What does JPA's @Enumerated(EnumType.ORDINAL) risk?
DebuggingFoundations6 min read
A Java service stores prices as double and a PostgreSQL column as real. Customers see totals like 19.989999. What is wrong and how should money be modelled in Java and SQL?
DesignIntermediate6 min read
Your system stores appointments for clinics in many countries. Some columns are 'timestamp without time zone'. Users report appointments shifting by an hour after daylight saving changes. How should instants, local times and time zones be modelled?
LeadershipPrincipal6 min read
A global company has had repeated incidents from money rounding, currency conversion and time zone handling across 80 services in several languages. How would you prevent this class of problems at the organisation level?

Multi-tenant schemas

CodingFoundations6 min read
Write PostgreSQL row-level security for a shared invoices table with a tenant_id column so that each connection only sees its tenant's rows, and show how a Spring service sets the tenant.
DesignIntermediate6 min read
Compare three multi-tenant database models for a B2B SaaS on PostgreSQL: shared tables with a tenant_id column, schema per tenant, and database per tenant. Which would you choose for 2000 small tenants and 5 very large ones?
ScenarioAdvanced6 min read
One tenant's bulk import in a shared-table SaaS database saturates I/O and slows every other tenant. How do you handle this now and design against it long term?
LeadershipPrincipal5 min read
Your SaaS started with schema-per-tenant and now has 15,000 schemas; migrations take a day and costs are rising. Leadership asks for a multi-year tenancy strategy. What do you propose?