Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 21/ 160
02. SQL & Data Engineering Basics

Relational Database Concepts & Schemas

Tables, keys, constraints, and schema design foundations for analytics-ready databases.

BeginnerPrimary/Foreign Keys · Constraints3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

A database is a set of tables that point to each other with ID numbers, and a schema is the map of which tables exist and how they connect.

Think of it like…

A set of linked spreadsheets: each sheet has an ID column, and other sheets refer to those IDs instead of repeating the details. To answer a question you follow the IDs from sheet to sheet, so you need to know what each row in each sheet stands for.

Where you’ve seen it

A churn analyst gets tables customers, subscriptions, invoices and support_tickets. Reading the schema, she sees customers(customer_id PK), subscriptions(subscription_id PK, customer_id FK, status, started_at, ended_at) with several rows per customer, and invoices(invoice_id PK, subscription_id FK, amount). The grain differs per table, so a naive join of customers to invoices multiplies rows; she builds one row per customer by aggregating subscriptions and invoices first, and uses ended_at only to define the label, never as a feature.

Words you’ll hear

Primary key
A column (or set) that uniquely identifies each row and is never NULL.
Foreign key
A column whose values must exist in another table's key, linking the two tables.
Grain
What a single row represents: one customer, one order line, one customer-day.
Constraint
A rule enforced by the database: NOT NULL, UNIQUE, CHECK, FOREIGN KEY.

Visual diagram

Loading interactive diagram

Theory & deep dive

Tables, keys and constraints

A table's primary key guarantees one row per key value, so joining on it never duplicates rows. A foreign key guarantees that every value points to an existing parent row (when the constraint is enforced; warehouses often don't enforce them, so verify with a quick check). One-to-many relationships (a customer has many orders) are the norm, many-to-many relationships use a junction table, and a one-to-one table is usually an extension of the same entity.

Constraints tell you what you can rely on. NOT NULL columns are always present; UNIQUE columns identify rows even if they aren't the primary key; CHECK constraints bound values (amount >= 0); defaults explain what "missing" really means. When constraints aren't enforced (common in analytical stores), test your assumptions with queries such as counting duplicates of a supposed key.

Verify a key before joining

SELECT customer_id, COUNT(*) FROM customers GROUP BY customer_id HAVING COUNT(*) > 1; returns no rows if customer_id is unique. If it returns rows, any join on it will duplicate data.

Key takeaway: Primary keys make joins safe; check uniqueness when constraints aren't enforced.

Schemas, grain and analytics-ready design

A schema is a namespace (analytics.orders vs raw.orders) and also the overall design. Before writing any query, write down each table's grain in one sentence and your target grain (for churn: one row per customer per snapshot date). Operational databases are normalised: facts live in one place and are referenced by key, which avoids update anomalies but needs joins. Analytics layers often denormalise into wide tables or star schemas to make queries simple and fast.

Learn to find documentation: ER diagrams, data dictionaries and column comments (information_schema.columns), and ask owners about meaning and history: is status the current state or at the time of the row? Is updated_at changed by every edit? Which timezone are timestamps in? These questions decide whether a column is a valid feature, a label or leakage.

Exploring an unfamiliar schema

SELECT table_name, column_name, data_type FROM information_schema.columns WHERE table_schema = 'public' ORDER BY table_name, ordinal_position; lists all tables and columns, a quick start before reading the ER diagram.

Key takeaway: Write down grain, history and timestamps semantics before using any column.

How it works, step by step

Step 1 of 4
Explore

1. List tables and keys

Use the data dictionary or information_schema to list tables, primary keys and foreign keys.

information_schema.columns
Key numbers at this step
Draw
a quick ER sketch

When to use it & trade-offs

What it is

A relational database stores data in tables (rows are records, columns are attributes) connected by keys. A primary key uniquely identifies a row; a foreign key in another table points back to it; constraints (NOT NULL, UNIQUE, CHECK) keep values valid; and a schema groups related tables. For a data scientist, the working skill is reading a schema: finding the grain of each table (what one row means), the keys that join tables, and which columns are safe to use as features or labels.

Why it matters: Most modelling data starts in a database, and most analysis errors start with a misunderstood table: joining on a non-unique key, treating a status column as the current state when it's a history, or using a timestamp that is only set after the outcome you're predicting. Ten minutes understanding keys, grain and constraints prevents days of debugging inflated counts and leaked labels.

When to reach for it

  • Understanding a new database before querying
  • Planning joins and aggregation levels for a modelling table
  • Checking whether columns can leak the label

When to avoid or be careful

  • Joining before knowing each table's grain
  • Assuming foreign keys are enforced in a warehouse
  • Using timestamps without knowing when they're set

Operational vs analytical schemas

Normalised (OLTP) schema
Best for: Applications that write data

Strengths: No duplication; consistent updates; enforced constraints.

Watch out: Many joins for analysis; grain differs per table.

Star schema / marts
Best for: Reporting and BI

Strengths: Clear grain; simple joins; fast scans.

Watch out: Built and maintained separately; some duplication.

Wide feature tables
Best for: Modelling datasets

Strengths: One row per entity; ready for pandas or ML.

Watch out: Easy to leak future information if snapshots aren't dated.

Interview signals & numbers to know

When this topic comes up

  • “Which column do I join on?”
  • “Why did my row count explode after a join?”
  • “What does one row in this table mean?”
  • Data dictionaries
  • Label leakage

Benchmarks & rules of thumb

1 -
row per primary key value
1:N -
the most common relationship
0 -
duplicate keys allowed in a primary key

Common mistakes & follow-ups

Common interview mistakes

  • Joining customers to a many-row-per-customer table and then counting customers.
  • Treating status as a history when the table only stores the latest value.
  • Using a timestamp that is filled in after the event as a predictor.

Likely interviewer follow-ups

  • How do you find out what one row of a table represents?
  • What goes wrong when joining on a non-unique key?
  • Why do analytical systems often skip enforced foreign keys?
Interview tip: Say "first I check the grain of each table". It's the habit that prevents the most common analytics mistakes.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Relational Database Concepts & Schemas with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Automated Profiling & Reproducible Pipelines
Next topic
SELECT, WHERE, ORDER BY & Filtering