⚡0 XP🔥0

Introduction to Databases and SQL

Lesson 1 of 8 ⏱ 4 min read ✎ 3 exercises ❓ 5-question quiz ▶ Runs SQL

What relational databases are, tables, rows, columns, primary and foreign keys, the practice "shop" database, popular database systems, and your first SELECT queries.

What is a database?#

A database is an organised collection of data stored so that it can be searched, updated and protected efficiently. Most business data - bills, bank transactions, student records, orders - lives in relational databases.

A relational database stores data in tables (like Excel sheets) that are related to each other through key columns.

Excel Database
Workbook Database
Sheet Table
Row Row / record
Column Column / field
Anyone can type anything anywhere Each column has a data type and rules
~1 million rows max, one user editing Billions of rows, thousands of users at once
हिंदी में समझें

Database = data रखने की एक व्यवस्थित जगह, जहाँ हज़ारों लोग एक साथ लाखों-करोड़ों records पढ़/लिख सकें - जैसे बैंक, IRCTC, Amazon का data।

Relational database में data tables में रहता है (Excel sheets जैसी), और tables आपस में keys से जुड़ी होती हैं।

SQL वह भाषा है जिससे हम database से सवाल पूछते हैं - "मुझे Indore के सारे customers दिखाओ" को SQL में लिखते हैं: SELECT * FROM customers WHERE city = 'Indore';

Keys and relationships#

  • Primary key (PK): a column that uniquely identifies each row (customer_id in customers).
  • Foreign key (FK): a column that points to a primary key in another table (customer_id in orders).

This avoids repeating data: the customer's name and city are stored once in customers; each order just stores the customer's ID.

The practice database: shop#

Every SQL editor in this module is connected to this database (an online electronics, furniture and stationery shop):

customers (40 rows)            orders (200 rows)                order_items (405 rows)        products (20 rows)
───────────────────            ─────────────────                ──────────────────────        ──────────────────
customer_id  PK      ◄───┐     order_id     PK      ◄───┐       item_id     PK                product_id  PK
name                     └──── customer_id  FK          └────── order_id    FK      ┌──────►  name
city                           order_date                       product_id  FK ─────┘         category
state                          status                           quantity                      sub_category
segment                        payment_mode                     discount                      price
signup_date                                                                                    cost

employees (15 rows): emp_id PK, name, dept, manager_id (→ emp_id), salary, city, join_date
Table Description
customers who buys: name, city, state, segment (Consumer / Corporate / Small Business), signup date
products what we sell: name, category, sub-category, selling price, cost
orders one row per order: customer, date (2024-2025), status (Delivered, Shipped, Cancelled, Returned), payment mode
order_items one row per product in an order: quantity and discount (0 - 0.15)
employees staff with their manager (for self-joins)
Download
  • shop.sql - SQLite script to create the database on your computer
  • shop_csv.zip - the same tables as CSV files (for Excel / Power BI)

Your first query#

SELECT * FROM customers;
  • SELECT - which columns (* = all)
  • FROM - which table
  • ; ends the statement

Press Run - try changing customers to products or orders:

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

Choosing columns and limiting rows#

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

SQL style rules#

  • Keywords are not case-sensitive (select = SELECT), but writing them in CAPITALS makes queries readable.
  • Text values go in single quotes: 'Indore'.
  • Put each clause on its own line for longer queries.
  • Comments: -- single line or /* multi-line */.

Looking at the structure#

In SQLite you can list tables and columns:

▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run
▶ Try it Yourselfdatabase: shopSQL
Ctrl + Enter to run

(In MySQL you'd use SHOW TABLES; and DESCRIBE orders;.)

Database Where you'll meet it
MySQL / MariaDB websites, PHP/Python apps, many Indian companies
PostgreSQL modern startups, analytics, cloud
Microsoft SQL Server enterprises, ERP/finance, Power BI sources
Oracle banks, telecom, large enterprises
SQLite mobile apps, small tools - used on this site
Cloud warehouses - BigQuery, Snowflake, Redshift, Azure Synapse large-scale analytics

The core SQL you learn here (SELECT, WHERE, GROUP BY, JOIN, CTEs, window functions) works in all of them. Differences are mainly in date and text functions - we'll point them out.

Tip

To practise on your own computer, install DB Browser for SQLite (free) and open shop.sql via File → Import → Database from SQL file. For MySQL, install MySQL Community Server + MySQL Workbench.

Summary#

  • Relational databases store data in related tables; SQL is the language to query them.
  • Primary keys identify rows; foreign keys link tables.
  • SELECT columns FROM table LIMIT n; is the starting point of every query.
  • The shop database (customers, products, orders, order_items, employees) powers every editor in this module.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

All products

Show all columns of the products table.

Database: shop (see the tables in this lesson)
2

Customer names and cities

Show only the name and city columns of every customer.

Database: shop (see the tables in this lesson)
3

First 5 orders

Show the first 5 rows of the orders table (all columns).

Database: shop (see the tables in this lesson)

Quick Quiz

Q1.

What does SQL stand for?

Q2.

What is a primary key?

Q3.

In the orders table, customer_id refers to customers. It is a...

Q4.

Which statement returns all columns of the products table?

Q5.

Why do companies store data in databases instead of Excel files?