What is a database?
Before databases were widely used, applications often stored information directly in files. Imagine a telephone directory kept in a file named telephone_directory.csv:
person_id,first_name,last_name,phone_number,address,city
1,Asha,Rao,9876543210,12 Lake Road,Bengaluru
2,Ben,Thomas,9123456780,45 Park Street,Bengaluru
3,Carla,Shah,9988776655,18 Hill View,Mumbai
4,Dev,Mehta,9000012345,12 Lake Road,BengaluruFor a small amount of information, a file works perfectly well: a program opens it, reads the contents, and searches for the record it needs. As the data grows and more applications and users work with it, several problems appear.
Why plain files struggle as data grows
Every question needs custom code
A file stores data but offers no way to ask questions of it. Each new question (“Bill’s number,” “Who lives at 221 B Street?”) means writing another file-scanning program.
Finding records gets slower as the file grows
As a file grows to millions of records, finding a specific match takes more time. An index makes searching faster by helping locate records using details like name, phone number, address, or city, without checking every record.
Safe updates are hard
Changing one number means finding the record, rewriting the file, and not corrupting the rest, especially when several users write at once.
Duplicated information can become inconsistent
When the same information appears in multiple records, updating one copy but forgetting another can leave different versions of the same information.
Files enforce no rules
A file can contain duplicate IDs, missing names, or invalid phone numbers. Without rules to check the data, these mistakes can go unnoticed.
Relationships aren’t guaranteed
A person_id in one file might refer to a person who doesn’t exist in another file. Without checks, these broken connections can go unnoticed.
Controlling access is difficult
File permissions can control who can read or modify a file, but restricting access to specific records or fields is harder.
Recovery from a crash is difficult
If a program crashes while updating a file, some changes might be saved while others are lost. This can leave the data incomplete or inconsistent, making recovery difficult.
Two problems, explained with examples
1. Repeated information can become inconsistent
If we store a separate row for each phone number, Asha’s name and address appear multiple times. When she moves to a new address, updating only one row leaves two different addresses for the same person:
2. Files enforce no rules
Suppose every person must have a unique ID, a name, a valid phone number, and a city. A plain file doesn’t check these rules. It can contain duplicate IDs, missing names, or invalid phone numbers without reporting an error:
The id 1 is duplicated, unknown is not a phone number, Ben’s city is missing, and the third record has no name.
What a database provides
A database is an organised collection of information. The software used to store, search, update, and manage this information is called a Database Management System (DBMS). Examples include PostgreSQL, MySQL, Microsoft SQL Server, Oracle Database, and SQLite.
A DBMS makes it easier to manage large amounts of data. It helps you find records quickly, update information safely, allow multiple users to work with the same data, enforce rules to prevent invalid data, control who can access information, and recover from failures. When data is organised properly, it also helps reduce duplication and keep related information consistent.
Files are still useful for documents, exports, logs, and small datasets. Databases earn their place when growing, related data must be searched, updated, validated, shared, and protected by many users at once. One of the most widely used ways to organise that data is the relational model.
The Relational Model
In a relational database, information is organised into tables. Each table stores one type of information, such as customers or orders. A table has rows and columns:
- A row represents one record, such as a single order.
- A column represents a specific detail about that record, such as the order ID, customer ID, or total amount.
For example, an orders table might look like this:
Customer and product details don’t need to be repeated in every order. Instead, we store them in separate tables and use IDs to connect the information.
For example, order 1001 contains product_id = 101 and customer_id = 1. Using these IDs, we can find out that Asha ordered a Wireless Mouse without storing her personal details or the product information again.
How tables connect: keys
The three tables describe one business process, Customers → Orders → Products, linked through shared identifying columns called keys.
Primary key
Uniquely identifies each row in a table, for example customers.customer_id or orders.order_id. No two rows share the same value.
Foreign key
Refers to a row in another table. orders.customer_id points at customers.customer_id, and orders.product_id points at products.product_id.
Thinking in rows and columns
Most questions become questions about tables, rows, columns, and relationships:
- Which orders have been paid? Filter
ordersbystatus. - Which products cost more than 2,000? Filter
productsbyprice. - Which products has Asha ordered? Combine
customers,orders, andproducts. - How much revenue did each product generate? Combine orders and products, compute an amount, and group by product.
What is SQL?
SQL (pronounced “S-Q-L” or “sequel”) stands for Structured Query Language. It is the language we use to work with relational databases. SQL lets us retrieve, add, update, and delete information. When retrieving data, we describe what we want, rather than writing every step needed to find it.
For example, a SQL query can specify which columns to return, which table to read, and which conditions the rows must satisfy. The database figures out how to retrieve the data efficiently. This approach is called declarative programming.
Imperative vs. declarative: what’s the difference?
Imperative: Describe every step
Open the products table. Read the first row and check its price. If the price is greater than 2,000, keep its ID, name, and price. Move to the next row and repeat until every row has been checked.
Declarative: Describe what you want
“Show me the ID, name, and price of all products that cost more than 2,000.” With SQL, you describe the result you need. The database decides how to find it.
What you can do with SQL
SQL does more than read data. It can filter, sort, combine, summarise, add, update, remove, and even define the structure of information in a relational database.
SELECT product_name, priceFROM products;SELECT product_name, priceFROM productsWHERE price > 2000;SELECT product_name, priceFROM productsORDER BY price DESC;SELECT o.order_id, c.customer_name, p.product_nameFROM orders AS oJOIN customers AS c ON c.customer_id = o.customer_idJOIN products AS p ON p.product_id = o.product_id;SELECT status, COUNT(*) AS order_countFROM ordersGROUP BY status;INSERT INTO products (product_id, product_name, category, price)VALUES (105, 'Laptop Sleeve', 'Accessories', 1200);UPDATE ordersSET status = 'shipped'WHERE order_id = 1002;DELETE FROM ordersWHERE order_id = 1004;CREATE TABLE products ( product_id INTEGER PRIMARY KEY, product_name VARCHAR(100), category VARCHAR(50), price DECIMAL(10, 2));Because of this, SQL is used by software engineers, data analysts, data engineers, database administrators, data scientists, and business teams alike. The same core ideas appear across PostgreSQL, MySQL, SQL Server, Oracle, SQLite, Snowflake, BigQuery, and DuckDB; the exact syntax varies, but the relational concepts stay the same.
A SQL query returns another table
What does a SQL query return?
An important concept is that a SELECT query returns a result that looks like a table. For example, suppose we run a query to find products priced above 2,000. The result contains only the products that match our condition and the columns we requested. The original table remains unchanged. The query simply returns the information we asked for. As you learn more SQL, you’ll build queries that perform additional operations: filtering rows, combining related tables, calculating values, grouping records, and sorting results. The basic idea remains the same: start with data, describe what you need, and let SQL return the result.
SELECT product_id, product_name, priceFROM productsWHERE price > 2000;What comes next
We build SQL knowledge gradually, from the operations used in everyday queries toward more advanced analytical patterns.
- Selecting columns and reading tables.
SELECT … FROM …, aliases, removing duplicates, and the shape of a result. - Filtering rows with WHERE. Comparisons,
AND/OR,IN,LIKE,BETWEEN,NULL, precedence, and date filters. - Sorting and limiting results.
ORDER BY,ASC/DESC,LIMIT, and database-specific alternatives. - Calculations and expressions. Order totals, combining text,
CASEcategories, missing values, and type conversions. - Aggregate functions and grouping.
COUNT,SUM,AVG,MIN,MAX,GROUP BY, andHAVING. - Joining related tables.
INNER JOIN,LEFT JOIN, multi-table joins, missing matches, and duplicate rows. - Subqueries and CTEs. Scalar and correlated subqueries,
EXISTS/NOT EXISTS, andWITH. - Window functions. Ranking, running totals, comparing to a previous row, and rolling averages.
- Set operations.
UNION,UNION ALL,INTERSECT, andEXCEPT. - Modifying data safely. Insert, update, delete, transactions, and not touching more rows than intended.
- Designing reliable SQL queries. The mistakes that return believable but incorrect results: grouping, join fan-out,
NULL, date boundaries, and type mismatches.
The goal is not only SQL that runs. It is SQL that expresses the business requirement clearly, returns the correct result, and stays understandable when someone reads it later.