Skip to content

Beginner · Databases and SQL basics · topic 16 of 54 · 20 min

What is a database?

Explanation

A database is a program that stores your app's data safely and finds any part of it fast. Most databases keep data in tables of rows and columns, like a very strict spreadsheet. You talk to them in SQL, a short language of commands like SELECT and CREATE TABLE.

Why not just a file or an array?

An array in your code lives in memory (RAM). When the server restarts, it is gone. A plain file survives restarts, but if two users save at the same moment the file can break, and finding one order among ten lakh means reading the whole file.

A database is a program built only for storing data. It keeps data safe on disk, lets thousands of users read and write at the same time without breaking anything, and finds rows quickly even in huge tables.

Think of it like this: Think of a school office. Loose papers in a bag (a file) get lost and mixed up. A register with fixed columns (roll number, name, class, fees paid), kept in a locked cupboard by one clerk who writes every entry carefully, is a database. Anyone can ask the clerk "show me all students of class 10" and get the answer fast.

Tables, rows and columns

  • A table holds one kind of thing: students, orders or trains.
  • A column is one detail that every row has, like name or fare. Each column has a data type.
  • A row is one item: one student, one order, one train.
  • A primary key is a column whose value is unique for every row, like a roll number or a PNR. It lets you point to exactly one row.
Data type (SQLite / PostgreSQL)HoldsExample
INTEGERWhole numbers12951, 3100
REAL / NUMERICNumbers with decimals4.5, 199.99
TEXTWords and sentences'Mumbai Rajdhani'
DATE / TEXTDates (SQLite stores them as text)'2026-10-19'
BOOLEAN / INTEGERYes or no (SQLite uses 1 and 0)true, 1

SQL: the language of databases

SQL (Structured Query Language, said "S-Q-L" or "sequel") is the language you use to talk to a relational database. A relational database stores data in tables that can be linked to each other. CREATE TABLE makes a new table, INSERT adds rows and SELECT reads them.

sql
CREATE TABLE trains (
  number INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  from_city TEXT,
  to_city TEXT,
  fare INTEGER
);

INSERT INTO trains (number, name, from_city, to_city, fare)
VALUES (12951, 'Mumbai Rajdhani', 'Mumbai', 'New Delhi', 3100);

SELECT * FROM trains;
Make a table, add one row and read everything back. NOT NULL means the column can never be empty.

SQL vs NoSQL in one paragraph: SQL databases (PostgreSQL, MySQL, SQLite) store data in tables with fixed columns and are the safe default for most apps, especially anything with money. NoSQL databases (like MongoDB) store flexible documents, similar to JSON, and suit data whose shape changes often. You will meet NoSQL in a later module.

Popular choices: PostgreSQL (powerful, free, the most common pick for new backends), MySQL (very widely used, for example by WordPress), and SQLite (a whole database in one file, used inside phones and for learning). The tasks on this site run SQLite inside your browser.

Tip: Write SQL keywords in CAPITALS (SELECT, FROM, WHERE) and table and column names in small letters with underscores (from_city). SQL does not require it, but everyone can read your queries faster.
Careful: Text values go inside single quotes: 'Pune'. Double quotes are for column or table names in many databases, so WHERE city = "Pune" may fail or behave strangely. Numbers need no quotes.

Watch the lesson

What is a database? · 1/6

What is a database?

Every serious app keeps its data in a database. Today we see why, and how data is laid out.

Prefer a video by a teacher? Find free videos on “what is a database sql tables rows columns for beginners” on YouTube ↗

In real life: Train seats on a booking site

A train booking site keeps trains, coaches, seats, passengers and bookings in a database. When two people try to book the last seat in the same coach at the same second, the database makes sure only one booking succeeds. When you check your PNR, the server runs a query like SELECT * FROM bookings WHERE pnr = '4521378901' and gets your row out of crores of rows in milliseconds, thanks to the primary key.

Practice (0/3 done)

Task 1 · Code · SQL

The table trains has the columns number, name, from_city, to_city and fare. Write a query that shows every column of every train.

Task 2 · Code · SQL

The database is empty. Write three statements, one after another: 1) CREATE TABLE books with columns id (INTEGER PRIMARY KEY), title (TEXT NOT NULL) and price (INTEGER). 2) INSERT two rows: (1, 'Wings of Fire', 299) and (2, 'Godan', 199). 3) SELECT * FROM books. The checker looks at the result of your LAST statement.

Task 3 · Practice

A kirana shop wants a billing app. Design the products table on paper: list each column with its data type, choose the primary key, and say which columns must be NOT NULL. Then write the CREATE TABLE statement.

Quick quiz

Question 1

1.Why do apps store users in a database and not in an array in the code?

Question 2

2.In a students table, what is one row?

Question 3

3.What is special about a primary key?

Question 4

4.Which statement makes a new table?

Question 5

5.Which of these is a SQL database?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Environment variables and config