skip to content

Relational Model & Theory

The mathematical foundation under every SQL engine: relations, keys, relational algebra and calculus, and Codd's design principles. Interviewers probe this layer to see whether you understand why relational databases behave the way they do, not just how to query them.

part ofRelational database conceptsoverview, primer and where to startread it →
on this pageshow

questions

page 2 of 2

What is an attribute's domain in the relational model, and what do you gain by giving an attribute a narrow domain instead of storing everything as free text?

level: middleimportance: should knowfreq 48%

basics

~20 s

A domain is the set of values an attribute may take, plus the operations defined on them. A narrow domain rejects impossible values at write time, gives correct comparison and ordering semantics, and lets the engine store and compare values efficiently instead of every reader re-parsing text.

open as a page

A database schema is often described as a set of relation schemas plus the constraints over them. What exactly belongs to that database schema, what belongs to the database instance at a given moment, and how does that differ from what SQL calls a schema?

level: middleimportance: should knowfreq 42%

basics

~20 s

The database schema is every relation schema - names, attributes, domains - plus all constraints, including ones spanning relations such as referential constraints. The database instance is one instance of each relation, all satisfying those constraints together. In SQL, 'schema' also means a namespace grouping objects, which is a different idea.

open as a page

Why is the outer join treated as an extended relational-algebra operator rather than something derivable from the basic operators, and what does it do with tuples that find no match on the other side?

level: seniorimportance: should knowfreq 40%

basics

~20 s

An outer join keeps dangling tuples — those with no matching partner — and pads their missing attributes with null. Basic relational algebra has no null value and no operator that invents one, so outer join needs both an extension to the value domain and an operator definition of its own.

open as a page

A colleague rewrites an existential subquery into an inner join, claiming it is 'the same thing but simpler'. Under what conditions does that rewrite change the answer, and what has to be added to make it equivalent?

level: seniorimportance: should knowfreq 38%

basics

~20 s

It changes the answer whenever an outer row matches more than one inner row: the semi-join returns that row once, the inner join returns it once per match. Equivalence requires either a proof that the join key is unique on the inner side, or an explicit duplicate elimination on the outer row's identity after the join.

open as a page

An engine wants to apply a relational-algebra projection before a selection instead of after it. Under what condition is that rewrite safe, and what does it buy?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Safe only if every attribute the selection predicate references survives the projection. Otherwise the filter has no column to test. When safe, projecting first narrows tuples early, cutting memory and I/O through the rest of the plan.

open as a page

To reconcile two datasets you compute the difference in both directions — rows in A not in B, and rows in B not in A. What properties of set-difference semantics can make that result misleading, and how do you make the comparison trustworthy?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Set difference deduplicates, so count mismatches vanish; it compares whole rows positionally, so a column-order or type-coercion difference makes everything look different; and it says which rows differ, not which columns. Compare on a key with a value-level check instead.

open as a page

Codd's view-updating rule says every view that is theoretically updatable must also be updatable by the system. Why do real engines fall short of that, and what do they offer instead?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Because for many views a change to the view has no unique translation back to base rows — joins, aggregates and set operations are ambiguous or lossy. Engines auto-update only simple single-table views and require an explicit handler, such as an INSTEAD OF trigger, for the rest.

open as a page

You must restructure a heavily used table — splitting it in two — while many independent read and write paths keep working. How does a view layer give you logical data independence during that migration, and where does the technique run out?

level: seniorimportance: should knowfreq 33%

basics

~20 s

Create the new tables, then expose a view with the old table's name and shape so read paths keep working while consumers migrate. It runs out on writes (only simple views are updatable), on non-derivable changes such as cardinality shifts, on performance, and on the double-write period during backfill.

open as a page

Does a foreign key have to point at the parent table's primary key? Explain what a foreign key actually requires of the attributes it references.

level: seniorimportance: should knowfreq 34%

basics

~20 s

No. A foreign key must reference a candidate key of the parent, meaning a declared unique, minimal attribute set. The primary key is the usual target because it is stable and non-null, but any enforced candidate key works. Referencing non-unique attributes is meaningless because the reference would not denote one row.

open as a page

What is a 'safe' expression in relational calculus, and what goes wrong with an unsafe one?

level: seniorimportance: should knowfreq 18%

basics

~20 s

A safe expression is one whose result contains only values already present in the database or in the query, so it is finite and independent of the underlying domains. Unsafe expressions, typically unrestricted negation such as 'all tuples not in R', describe infinite or domain-dependent results that no engine can compute.

open as a page

The relational model defines a relation's body as an unordered set of tuples over a heading of named attributes. Why is neither row order nor column position part of a relation, and what goes wrong in production when application code assumes rows come back in the order they were stored?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Both the heading and the body are sets, and sets have no order, so position is not part of the value. Engines exploit that freedom: storage reorganisation, updates relocating rows, parallel scans and plan changes all reorder results, so code assuming stored order breaks intermittently with no code change to blame.

open as a page

Why is an integrity constraint described as a property of the schema rather than of the data, and what follows from saying that only legal instances are permitted - including for the intermediate states a transaction passes through?

level: seniorimportance: should knowfreq 38%

basics

~20 s

A constraint is a predicate declared on the schema, so it must hold for every instance the database is ever allowed to reach, not just the current one. A property that merely happens to be true of today's data guarantees nothing. Enforcement therefore checks state transitions, and some constraints must be checked at transaction end rather than per statement.

open as a page

Relational division is not a primitive operator. Show how it is derived from projection, Cartesian product and set difference, and explain why the derivation is naturally read as 'build the counterexamples and subtract them'.

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

Take all candidates (project the dividend onto the non-divisor attributes), pair every candidate with every divisor value via Cartesian product, subtract the pairs that actually exist in the dividend — what remains are the missing pairs. Project those onto the candidate attributes to get the disqualified candidates, and subtract them from all candidates.

open as a page

Codd argued that a single NULL marker is not sufficient and proposed distinguishing two kinds of missing information. What was that distinction, why did he consider one marker inadequate, and how do practitioners deal with it today?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

He separated 'missing but applicable' (a value exists, unknown to us) from 'missing and inapplicable' (no value can exist), proposing two markers and four-valued logic. One marker conflates them, so queries cannot tell them apart. Today the usual answer is decomposition into separate tables or an explicit status column.

open as a page

Real query engines evaluate bag (multiset) algebra rather than the set algebra of the textbook. Which algebraic laws stop holding once duplicates are preserved, and how does that constrain the rewrites an optimizer is allowed to perform?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

In bag algebra each tuple carries a multiplicity. Projection no longer removes duplicates, union adds multiplicities instead of merging, and idempotence, absorption and some distributive laws fail. An optimizer may therefore only insert or remove duplicate elimination where the enclosing context is insensitive to multiplicity.

open as a page

Codd's non-subversion rule forbids any low-level interface that bypasses the integrity rules enforced by the higher-level relational language. Which of Codd's rules do mainstream SQL engines actually fail, and how much should that influence a database choice?

level: principalimportance: nice to knowfreq 24%

basics

~20 s

Non-subversion means bulk loaders and record-level APIs must not skip constraints or authorisation. Mainstream engines commonly fall short on view updating, integrity independence, distribution independence and full non-subversion. It matters as a checklist of where your team must compensate, not as a product-selection score.

open as a page

Data independence is presented as an unqualified good, yet real systems deliberately let physical concerns leak into logical design and application code. When is breaking data independence the right call, and how do you contain the damage?

level: principalimportance: nice to knowfreq 25%

basics

~20 s

Break it when a physical fact drives a decision the logical layer cannot express and the cost is measured, not assumed — partition keys, clustering-aware access patterns, denormalisation for read cost, sharding keys. Contain it by writing the dependency down, testing it, and giving it an owner and a review trigger.

open as a page

What does it mean to call a query language 'relationally complete', and which queries still fall outside that bar?

level: principalimportance: nice to knowfreq 16%

basics

~20 s

Relationally complete means the language can express every query expressible in relational algebra, equivalently in safe relational calculus, which is Codd's theorem. It is a floor, not a ceiling: first-order power excludes counting and aggregation, and transitive closure such as reachability, so real languages add extensions.

open as a page

A table is supposed to hold exactly one row per (customer, product) pair, but duplicates keep appearing. How would you decide whether duplicate elimination belongs in the schema, in the write path, or in every read - and what does each choice cost?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Prefer the schema: a unique constraint makes the table a set on every write, is race-proof, and lets the planner skip duplicate removal. Write-path logic alone races; read-time deduplication taxes every query and hides the defect. Read-time dedup is right only when duplicates are legitimate events.

open as a page

showing 31–49 of 49