Think of the file system as storage and the DBMS as a service on top of that storage. The differences show up in six places.
Structure. The file system stores opaque bytes. A DBMS stores tables with a declared schema, types and constraints.
Access path. Files are read whole and filtered in your code. A DBMS takes a declarative query and picks an access path for you, using indexes when they help.
Concurrency. Files need your own locking. A DBMS has a transaction manager that handles locks and isolation for many users at once.
Integrity. Files trust the writer. A DBMS rejects bad rows for you, so a broken relationship cannot be stored.
Recovery. With files, a backup is a copy script you write. A DBMS keeps a transaction log and can undo or replay work after a crash.
Cost. Files are cheap and simple. A DBMS needs memory, a schema, tuning and an administrator.
So where does the file system still win? For a single user application, static configuration, large assets or append only logs, a plain file is often the better tool. An embedded database such as SQLite sits in between, because it is a DBMS that runs as a file.
A good rule is to ask how many people depend on the same data. One reader and one writer, and a file is fine. Several writers, shared reporting or data that must stay correct, and the DBMS earns its cost.
With files you write the matching yourself. You read the customer file, you read the order file, and you build a map from customer id to orders in memory. SQL does that join for you, and it does it inside the engine.
The script below creates two tables with a foreign key between them, adds a few rows, and then reports every customer with a count of orders and a total amount.
-- Customers and orders: two tables, one relationship.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers(id),
amount INTEGER NOT NULL
);
INSERT INTO customers (id, name) VALUES (1, 'Asha'), (2, 'Bilal'), (3, 'Chen');
INSERT INTO orders (id, customer_id, amount) VALUES (10, 1, 250);
INSERT INTO orders (id, customer_id, amount) VALUES (11, 1, 400);
INSERT INTO orders (id, customer_id, amount) VALUES (12, 2, 900);
SELECT c.name,
COUNT(o.id) AS order_count,
COALESCE(SUM(o.amount), 0) AS total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY total DESC, c.name;
Asha has two orders worth 650. Bilal has one order worth 900. Chen has none, and the row still shows up with a count of zero. That is the LEFT JOIN at work. An inner join would have dropped Chen instead.
The COALESCE call turns an empty sum into zero. Without it, a customer with no orders shows a null total, which is rarely what a report wants.
The real difference shows up when the question changes. Adding a filter, a second join or a monthly total means one more line of SQL. With files, it means another pass over the data in your own code, and another chance to get the match wrong.