What is SQL, and what kinds of problems does it solve that a hand-written loop would not?
SQL is the language that you use to ask a database questions and to change what it stores. You say what you want, and the engine works out how to get it for you.
That is the whole difference between SQL and a hand-written loop. A loop over a list of customers has to decide for itself which records to look at, and the program has to be rewritten whenever the question changes. A SQL query says "the customers in London who signed up in 2024", and the engine works out whether to walk the whole table, to use an index, or to do something cleverer.
There are three things that a loop cannot do and a database can.
The first one is scale. A table with fifty million rows is not something you want to pull into your program and sort there, because you do not have the memory for it and the database is built for exactly this.
The second one is the rules. A database can hold a rule such as "the email in this column must be different for every row" or "this column must point at a row that really exists". It checks the rule every single time, so a rule cannot be broken by one forgotten check somewhere in the code.
The third one is the safety. A database can do several changes as one piece of work, where either all of them happen or none of them do. A loop that updates two tables can leave the first one changed and the second one not, if the program dies in between.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT NOT NULL,
signed_up_on TEXT NOT NULL
);
INSERT INTO customers (id, name, city, signed_up_on) VALUES
(1, 'Ada Lovelace', 'London', '2024-03-02'),
(2, 'Grace Hopper', 'New York', '2024-11-17');
SELECT name, city FROM customers WHERE city = 'London';
The next question in this topic looks at a real query and at why the shape of it matters.