dot commands
-
.help→ Show all available dot commands -
.tables→ List all tables in the current database -
.schema→ Show SQL used to create all tables -
.schema table_name→ Show schema of a specific table -
.headers on→ Show column names in query output -
.mode column→ Pretty, aligned table output (best for learning) -
.mode list→ Simple pipe-separated output -
.nullvalue NULL→ DisplayNULLvalues clearly -
.open filename.db→ Open or create a database -
.read file.sql→ Execute SQL from a file -
.quitor.exit→ Exit SQLite
CREATE TABLE students (
roll_no INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
class INTEGER NOT NULL,
dob DATE NOT NULL
);INSERT INTO students (name, class, dob) VALUES
('Aarav', 8, '2005-01-01'),
('Ishaan', 8, '2005-01-02'),
('Riya', 8, '2005-08-03'),
('Ananya', 8, '2004-01-01'),
('Kabir', 8, '2004-08-02'),
('Rahul', 9, '2004-01-03'),
('Sneha', 9, '2004-08-01'),
('Arjun', 9, '2003-01-02'),
('Priya', 9, '2003-08-03'),
('Kunal', 9, '2004-08-02'),
('Neha', 10, '2003-01-01'),
('Aditya', 10, '2003-01-02'),
('Pooja', 10, '2003-08-01'),
('Rohan', 10, '2004-01-03'),
('Simran', 10, '2004-08-02'),
('Vikas', 8, '2005-01-03'),
('Meera', 9, '2004-08-03'),
('Sahil', 10, '2003-08-02'),
('Tanya', 9, '2004-01-01'),
('Nikhil', 8, '2005-08-01');SELECT * FROM students;CREATE TABLE table_name (
column1 datatype [constraints],
column2 datatype [constraints],
...
);Common constraints
PRIMARY KEYAUTOINCREMENT(SQLite: only withINTEGER PRIMARY KEY)NOT NULLUNIQUEDEFAULT value
INSERT INTO students (name, class, dob)
VALUES ('Amit', 9, '2004-01-01');INSERT INTO students (name, class, dob)
VALUES
('Ravi', 8, '2005-08-01'),
('Sita', 9, '2004-01-02'),
('Mohan', 10, '2003-08-03');-
Column order must match value order
-
You can omit columns with:
AUTOINCREMENTDEFAULT
- Dates in SQLite are stored as
TEXT(YYYY-MM-DDrecommended) DATEis just a type name- Value is still stored as TEXT (
'YYYY-MM-DD')
SQLite has only 5 storage classes:
NULLINTEGERREALTEXTBLOB
Everything else (DATE, VARCHAR(100), BOOLEAN) is mapped to these.
SELECT dob, typeof(dob) FROM students;- Writing
DATE→ improves readability - SQLite still stores →
TEXT
Type affinity = the preferred storage class SQLite tries to use for a column.
| Affinity | What SQLite tries to store |
|---|---|
INTEGER |
Whole numbers |
REAL |
Floating-point numbers |
TEXT |
Strings |
NUMERIC |
Numbers if possible, else text |
BLOB |
Raw bytes |
SQLite looks at the declared column type name.
Examples:
INT,INTEGER→INTEGERCHAR,VARCHAR,TEXT→TEXTREAL,FLOAT,DOUBLE→REALDATE,BOOLEAN,NUMERIC→NUMERIC- No type →
BLOB
column_name datatype CONSTRAINT1 CONSTRAINT2 ...or at table level:
CONSTRAINT constraint_name constraint_definitionid INTEGER PRIMARY KEY AUTOINCREMENTOrder doesn’t matter (logically), but this is conventional.
PRIMARY KEYAUTOINCREMENT(SQLite-specific)NOT NULLUNIQUEDEFAULT valueCHECK (condition)REFERENCES table(column)(foreign key)
name TEXT NOT NULLemail TEXT UNIQUE NOT NULLage INTEGER CHECK (age >= 0)created_at TEXT DEFAULT CURRENT_DATECREATE TABLE enrollments (
student_id INTEGER,
course_id INTEGER,
PRIMARY KEY (student_id, course_id)
);CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT,
CONSTRAINT unique_email UNIQUE (email)
);student_id INTEGER REFERENCES students(roll_no)PRAGMA foreign_keys = ON;PRAGMA = SQLite special command to configure or query database behavior.
Think of it as:
Database settings & internal info
PRAGMA table_info(students);→ column name, type, NOT NULL, default, PK
PRAGMA foreign_keys = ON;→ enforce referential integrity
SELECT column1, column2, ...
FROM table_name
WHERE condition
GROUP BY column1, column2, ...
HAVING group_condition
ORDER BY column1 ASC|DESC
LIMIT count OFFSET offset;Execution order (important):
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
= != < <= > >=AND
OR
NOTWHERE class IN (8, 9)WHERE dob BETWEEN '2004-01-01' AND '2005-12-31'WHERE name LIKE 'A%' -- starts with A
WHERE name LIKE '%a' -- ends with aLIMIT 5
LIMIT 5 OFFSET 10SQLite shorthand:
LIMIT 10, 5 -- OFFSET 10, LIMIT 5ORDER BY dob ASC
ORDER BY name DESCORDER BY class ASC, dob DESC- Sort by
classfirst - Then
dobwithin each class
Used with aggregate functions:
COUNT()
SUM()
AVG()
MIN()
MAX()SELECT class, COUNT(*) AS total_students
FROM students
GROUP BY class;Rule:
-
Every selected column must be:
- in
GROUP BY, or - inside an aggregate function
- in
WHERE → filters rows
HAVING → filters groups
SELECT class, COUNT(*) AS total
FROM students
GROUP BY class
HAVING COUNT(*) > 5;SELECT class, COUNT(*) AS total
FROM students
WHERE dob < '2005-01-01'
GROUP BY class
HAVING total >= 3
ORDER BY total DESC
LIMIT 2;