Chapter 1 of 18

Introduction to SQL & Databases

Understand what SQL is, how relational databases work, and set up your environment to start writing queries.

Meritshot9 min read
SQLDatabasesRDBMSGetting Started
All SQL Chapters

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:

RDBMSBest ForNotes
PostgreSQLGeneral purpose, data analysisOpen source, most feature-complete
MySQLWeb applicationsPowers WordPress, Facebook (originally)
SQLiteEmbedded, learningZero setup, single file database
Microsoft SQL ServerEnterprise, Windows environmentsWidely used in finance
OracleLarge enterpriseVery mature, expensive licensing
BigQueryCloud analytics at scaleGoogle 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

SQLite stores your entire database in a single file. No installation of a server needed.

Install SQLite Browser (GUI):

  1. Go to sqlitebrowser.org
  2. Download for your OS (Windows, macOS, Linux)
  3. Open the app → "New Database" → name it meritshot.db
  4. Click "Execute SQL" tab to start writing queries

Install SQLite CLI:

  • macOS: brew install sqlite or it's pre-installed
  • Windows: Download from sqlite.org/download.html
  • Linux: sudo apt install sqlite3

Run it: sqlite3 meritshot.db

  1. Download from postgresql.org
  2. Install with the default settings (remember the password you set for postgres user)
  3. 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 Fiddledb-fiddle.com — supports PostgreSQL, MySQL, SQLite
  • SQLiteOnlinesqliteonline.com
  • Mode SQL Editor — great for data analysis practice

SQL Statements: The Four Categories

SQL commands are grouped into four categories:

CategoryFull NameCommandsPurpose
DQLData Query LanguageSELECTRetrieve data
DMLData Manipulation LanguageINSERT, UPDATE, DELETEModify data
DDLData Definition LanguageCREATE, ALTER, DROPDefine structure
DCLData Control LanguageGRANT, REVOKEManage 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:

CategoryCommon TypesExample Values
IntegerINT, INTEGER, BIGINT1, 42, 1000000
DecimalDECIMAL(p,s), NUMERIC, REAL, FLOAT3.14, 99.99
TextVARCHAR(n), TEXT, CHAR(n)'hello', 'SQL'
Date/TimeDATE, TIME, TIMESTAMP'2026-01-15', '09:30:00'
BooleanBOOLEANTRUE, FALSE (not in SQLite)
NULLRepresents 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

  1. Create a table called students with columns for id, name, course, and grade. Insert three rows of your own data.
  2. Run SELECT * FROM students to retrieve all the data you inserted.
  3. What SQL category does SELECT belong to? What about CREATE TABLE?
  4. Why does SELECT NULL = NULL return NULL instead of TRUE?
  5. What is the difference between VARCHAR(50) and TEXT?

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
  • NULL means "no value" and must be compared with IS NULL

Next up: SELECT Queries — learn to retrieve exactly the columns and rows you need from any table.