Fundamentals · Medium · Free
Model one-to-many and many-to-many relationships in SQL.
FresherFundamentalsDatabase Fundamentals
Relationships in SQL
For Model one-to-many and many-to-many relationships in SQL., interviewers listen for a crisp definition, how the pieces interact in a real system, and one trade-off or failure mode you have seen or would guard against.
sql
-- 1:N departments → employees
CREATE TABLE employees (
id INT PRIMARY KEY,
dept_id INT REFERENCES departments(id)
);
-- M:N students ↔ courses via enrollmentsM:N always needs junction table with composite key.