Scrivio Interview library

Views, procedures and triggers

24 questions in 7 subtopics.

Views

ConceptFoundations7 min read
What is a database view, and what are views good for in an application backed by Spring Data JPA? Can you insert or update through a view?
DebuggingIntermediate7 min read
A query on a view orders_summary is slow, while the same query written by hand against the base tables with the filter WHERE customer_id = 42 is fast. The view joins four tables and uses a window function. Explain why the planner may not push the filter down, and how you would fix it.
DesignAdvanced6 min read
Several applications and reporting tools read directly from your service's tables, which makes schema changes hard. Would you use database views as the contract for these consumers? Describe the design and its limits.
LeadershipPrincipal6 min read
Your data platform team wants to give business units direct SQL access to production replicas through curated views, while the architecture group prefers APIs and an analytics warehouse. How would you decide the long-term data access strategy?

Materialized views

ConceptFoundations6 min read
How is a materialized view different from a view? In PostgreSQL, what does REFRESH MATERIALIZED VIEW CONCURRENTLY require, and what does it change compared with a normal refresh?
ScenarioIntermediate7 min read
A dashboard query aggregates 300 million order rows and takes 40 seconds. Product wants it under 1 second with data at most 5 minutes old. Compare a materialized view refreshed every 5 minutes, a summary table updated incrementally, and an external analytics store.
LeadershipAdvanced7 min read
Over time, teams created 150 materialized views and summary tables refreshed by cron jobs in the main OLTP database. Refresh jobs now take a large share of database CPU at night and sometimes overlap the morning peak. What would you do?
DesignPrincipal6 min read
Over the next five years your company expects reporting needs to grow fast while the main transactional databases must stay small and fast. Lay out a strategy for derived data (materialized views, summary tables, caches, read models, warehouses) and who owns it.

Stored procedures and functions

CodingFoundations7 min read
Write a PostgreSQL function in PL/pgSQL that returns the total of open invoices for a customer, and explain the difference between a FUNCTION and a PROCEDURE in PostgreSQL, including transaction control.
DebuggingIntermediate7 min read
A PL/pgSQL function that looks up orders by status is fast for the first few calls in a session and then suddenly becomes slow for rare status values. Explain what changed and how you would fix it.
DesignAdvanced7 min read
A reporting procedure in MySQL 8.4 builds dynamic SQL from parameters with CONCAT and PREPARE. A security review flags it. Explain the risk and rewrite the approach safely, including privileges.
LeadershipPrincipal7 min read
Your company's core banking logic lives in 4,000 Oracle PL/SQL procedures written over 25 years. Leadership wants to move to PostgreSQL and Java services to cut licence costs. How would you approach the decision and the plan?

Triggers

CodingFoundations7 min read
Write a PostgreSQL trigger that sets updated_at = now() on every UPDATE of the customer table, and explain BEFORE vs AFTER and row-level vs statement-level triggers.
DebuggingIntermediate7 min read
Bulk loading 10 million rows into a table became 20 times slower after someone added an AFTER INSERT row trigger that writes an audit row and updates a counter in a stats table. Explain the costs and how you would redesign it.
DesignAdvanced7 min read
Your team needs a reliable audit history of changes to 30 tables for compliance. Compare database triggers, Hibernate Envers, application-level events and change data capture with Debezium.
LeadershipPrincipal7 min read
A post-mortem found that a hidden trigger written ten years ago silently changed prices during a data migration, costing the company money. How would you prevent hidden database logic from causing such failures across the company?

Calling procedures from Java

CodingFoundations7 min read
Show how to call a stored procedure transfer(p_from bigint, p_to bigint, p_amount numeric, OUT p_status text) from Java with plain JDBC CallableStatement and with Spring's SimpleJdbcCall. What differs between PostgreSQL and MySQL here?
CodingIntermediate7 min read
Call a database function from Spring Data JPA in two ways: with @Procedure on a repository method, and with a native query. Then explain how you would test this code, and what can go wrong with transaction boundaries.

Sequences and identity columns

ConceptFoundations7 min read
Compare SERIAL, IDENTITY columns (GENERATED ALWAYS / BY DEFAULT AS IDENTITY) and explicit sequences in PostgreSQL. Why does Hibernate batch inserts with SEQUENCE but not with IDENTITY generation?
ScenarioIntermediate7 min read
Finance requires invoice numbers that are strictly gapless per year, such as 2026-000001, 2026-000002. A developer proposes a database sequence. Explain why that fails and design a correct solution that still scales reasonably.
DesignAdvanced7 min read
You are merging two companies' order databases that both use bigint sequences starting at 1, and planning a move to multiple active regions. What identifier strategy would you choose for new systems and how would you migrate the existing keys?

When to put logic in the database

DesignIntermediate7 min read
A team proposes moving order pricing rules from Java into PostgreSQL functions to cut round trips. Give a balanced review: what belongs in the database, what belongs in the service, and how you would decide here.
LeadershipAdvanced7 min read
Two senior engineers disagree: one says 'the database is just storage, all logic in Java', the other says 'data integrity must be enforced in the database'. You must set a guideline for 40 teams. What would it say?
LeadershipPrincipal6 min read
Thirty years ago much business logic lived in stored procedures; then it moved to application servers, and now some teams move logic back with serverless database functions and SQL-heavy analytics. What drives these swings, and how should a large company plan for them?