What is SQL?
SQL (Structured Query Language) is the standard language for communicating with relational databases. Pronounced "sequel" or "S-Q-L", it lets you create, read, update, and delete data stored in tables.
SQL was developed at IBM in the early 1970s and became an ANSI/ISO standard in 1987. Despite being over 50 years old, it remains one of the most in-demand technical skills across industries — from banking and e-commerce to healthcare and social media.
What Can You Do with SQL?
- Retrieve data — find exactly the rows and columns you need from millions of records
- Analyse data — calculate totals, averages, counts, and trends
- Modify data — insert new records, update existing ones, delete old ones
- Define structure — create tables, set constraints, manage relationships
- Control access — grant and revoke permissions for different users
What is a Relational Database?
A relational database organises data into tables (also called relations). Each table:
- Has a fixed set of columns (also called fields or attributes) — each with a defined data type
- Contains any number of rows (also called records or tuples) of actual data
- Can be linked to other tables through keys
Think of a table like a spreadsheet — but with strict data types, enforced constraints, and the power to connect to dozens of other tables efficiently.
Example: An Employee Database
employees table
┌────┬──────────────┬────────────┬──────────┬────────────┐
│ id │ name │ department │ salary │ hire_date │
├────┼──────────────┼────────────┼──────────┼────────────┤
│ 1 │ Priya Sharma │ Finance │ 75000.00 │ 2022-03-15 │
│ 2 │ Raj Patel │ Technology │ 92000.00 │ 2021-07-01 │
│ 3 │ Meera Singh │ Marketing │ 68000.00 │ 2023-01-10 │
│ 4 │ Arjun Nair │ Finance │ 81000.00 │ 2020-11-22 │
└────┴──────────────┴────────────┴──────────┴────────────┘
RDBMS — Relational Database Management Systems
An RDBMS is the software that stores and manages a relational database. It handles everything from reading data off disk to managing multiple users simultaneously. Popular options:
| RDBMS | Best For | Notes |
|---|---|---|
| PostgreSQL | General purpose, data analysis | Open source, most feature-complete |
| MySQL | Web applications | Powers WordPress, Facebook (originally) |
| SQLite | Embedded, learning | Zero setup, single file database |
| Microsoft SQL Server | Enterprise, Windows environments | Widely used in finance |
| Oracle | Large enterprise | Very mature, expensive licensing |
| BigQuery | Cloud analytics at scale | Google Cloud, SQL dialect |
For this tutorial: All examples use standard SQL that runs on PostgreSQL, MySQL, and SQLite. Differences in dialects are noted where they occur.
Setting Up Your Environment
Option 1: SQLite (Zero Setup — Recommended for Learning)
SQLite stores your entire database in a single file. No installation of a server needed.
Install SQLite Browser (GUI):
- Go to sqlitebrowser.org
- Download for your OS (Windows, macOS, Linux)
- Open the app → "New Database" → name it
meritshot.db - Click "Execute SQL" tab to start writing queries
Install SQLite CLI:
- macOS:
brew install sqliteor it's pre-installed - Windows: Download from sqlite.org/download.html
- Linux:
sudo apt install sqlite3
Run it: sqlite3 meritshot.db
Option 2: PostgreSQL (Recommended for Professional Use)
- Download from postgresql.org
- Install with the default settings (remember the password you set for
postgresuser) - Use pgAdmin (included) as the GUI, or install DBeaver for a cleaner interface
Option 3: Online SQL Editors (No Installation)
These work instantly in your browser:
- DB Fiddle — db-fiddle.com — supports PostgreSQL, MySQL, SQLite
- SQLiteOnline — sqliteonline.com
- Mode SQL Editor — great for data analysis practice
SQL Statements: The Four Categories
SQL commands are grouped into four categories:
| Category | Full Name | Commands | Purpose |
|---|---|---|---|
| DQL | Data Query Language | SELECT | Retrieve data |
| DML | Data Manipulation Language | INSERT, UPDATE, DELETE | Modify data |
| DDL | Data Definition Language | CREATE, ALTER, DROP | Define structure |
| DCL | Data Control Language | GRANT, REVOKE | Manage permissions |
The vast majority of your day-to-day SQL work will be DQL — reading and querying data.
Your First SQL Query
Let's create a sample table and run your first query. In your SQL editor, run:
-- Create a table
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
department TEXT NOT NULL,
salary REAL NOT NULL,
hire_date TEXT NOT NULL
);
-- Insert some data
INSERT INTO employees VALUES (1, 'Priya Sharma', 'Finance', 75000, '2022-03-15');
INSERT INTO employees VALUES (2, 'Raj Patel', 'Technology', 92000, '2021-07-01');
INSERT INTO employees VALUES (3, 'Meera Singh', 'Marketing', 68000, '2023-01-10');
INSERT INTO employees VALUES (4, 'Arjun Nair', 'Finance', 81000, '2020-11-22');
INSERT INTO employees VALUES (5, 'Sunita Rao', 'Technology', 88000, '2022-09-05');
-- Retrieve all data
SELECT * FROM employees;
Result:
id | name | department | salary | hire_date
---+---------------+------------+--------+----------
1 | Priya Sharma | Finance | 75000 | 2022-03-15
2 | Raj Patel | Technology | 92000 | 2021-07-01
3 | Meera Singh | Marketing | 68000 | 2023-01-10
4 | Arjun Nair | Finance | 81000 | 2020-11-22
5 | Sunita Rao | Technology | 88000 | 2022-09-05
SELECT * retrieves every column. FROM employees specifies the table. The semicolon ; ends the statement.
SQL Syntax Rules
Case Sensitivity
SQL keywords are case-insensitive — SELECT, select, and Select all work. Convention is to write keywords in UPPERCASE and table/column names in lowercase.
-- Both are identical
SELECT name FROM employees;
select name from employees;
-- Convention (recommended)
SELECT name FROM employees;
Comments
-- This is a single-line comment
/*
This is a
multi-line comment
*/
SELECT name -- you can comment at end of a line
FROM employees;
Whitespace and Formatting
SQL ignores extra whitespace. These are identical:
SELECT name FROM employees;
SELECT
name
FROM
employees;
Formatting matters for readability, not execution. Long queries should be split across multiple lines.
Semicolons
In most environments, a semicolon ; marks the end of a statement. When running multiple statements at once, semicolons are required between them:
SELECT * FROM employees;
SELECT * FROM departments;
SQL Data Types
Every column has a data type that controls what values it can store:
| Category | Common Types | Example Values |
|---|---|---|
| Integer | INT, INTEGER, BIGINT | 1, 42, 1000000 |
| Decimal | DECIMAL(p,s), NUMERIC, REAL, FLOAT | 3.14, 99.99 |
| Text | VARCHAR(n), TEXT, CHAR(n) | 'hello', 'SQL' |
| Date/Time | DATE, TIME, TIMESTAMP | '2026-01-15', '09:30:00' |
| Boolean | BOOLEAN | TRUE, FALSE (not in SQLite) |
| NULL | — | Represents missing/unknown value |
Note: SQLite uses a flexible type system (
TEXT,INTEGER,REAL,BLOB,NULL). Other databases are stricter.
NULL — The Special Value
NULL means "unknown" or "no value". It is not zero or an empty string — it's the absence of a value.
-- NULL behaves differently from other values
SELECT NULL = NULL; -- returns NULL (not TRUE!)
SELECT NULL IS NULL; -- returns TRUE
This trips up many beginners — NULLs require special handling with IS NULL and IS NOT NULL.
Common Beginner Mistakes
1. Using = for NULL comparison
-- Wrong — this never finds NULL rows
SELECT * FROM employees WHERE salary = NULL;
-- Correct
SELECT * FROM employees WHERE salary IS NULL;
2. Forgetting quotes around text values
-- Wrong — SQL thinks Finance is a column name
SELECT * FROM employees WHERE department = Finance;
-- Correct — use single quotes for text
SELECT * FROM employees WHERE department = 'Finance';
3. Confusing single quotes and double quotes
-- Single quotes = string values
WHERE name = 'Priya'
-- Double quotes = identifiers (column/table names with spaces or reserved words)
SELECT "order" FROM "my table"
-- In MySQL, backticks are used for identifiers
SELECT `order` FROM `my table`
4. Missing semicolon between multiple statements
-- Wrong — might cause errors
SELECT * FROM employees
SELECT * FROM departments
-- Correct
SELECT * FROM employees;
SELECT * FROM departments;
The Sample Database We'll Use
Throughout this tutorial, we'll use three tables that represent a company database. Here's the schema we'll build up:
-- Three interconnected tables
employees → has department_id (links to departments)
departments → has id, name, budget
orders → has employee_id (links to employees)
We'll add more columns and data as we go. By the end, you'll be able to write complex queries across all three tables using joins, subqueries, and window functions.
Practice Exercises
- Create a table called
studentswith columns forid,name,course, andgrade. Insert three rows of your own data. - Run
SELECT * FROM studentsto retrieve all the data you inserted. - What SQL category does
SELECTbelong to? What aboutCREATE TABLE? - Why does
SELECT NULL = NULLreturnNULLinstead ofTRUE? - What is the difference between
VARCHAR(50)andTEXT?
Summary
In this chapter you learned:
- SQL is the standard language for working with relational databases
- A relational database stores data in tables (rows and columns) that can be linked to each other
- Popular RDBMS options: PostgreSQL, MySQL, SQLite, SQL Server
- SQL is divided into DQL, DML, DDL, and DCL
- Your first query:
SELECT * FROM table_name - SQL keywords are case-insensitive; string values use single quotes
NULLmeans "no value" and must be compared withIS NULL
Next up: SELECT Queries — learn to retrieve exactly the columns and rows you need from any table.