TalentJR

🗄️ SQL for beginners · Class 12 · Lesson 1 of 18 · Free

SQL Basics – Tables, SELECT, WHERE, ORDER BY and LIMIT

⏱ About 30 minutes · ▶ run real SQL here — nothing to install · no sign-in

In this lesson you will

  • Explain what a database, a table, a row and a column are
  • Pick columns and rows with SELECT and WHERE
  • Sort and cut results with ORDER BY and LIMIT

Your school office keeps marks, fees and attendance for hundreds of students. When the principal asks "Who are the top 3 in Maths?", nobody wants to scroll through a huge register. With one short line of SQL, a computer finds the answer in a blink.

What is SQL and what is a database?

A database is an organised store of data. A relational database keeps data in tables. A table looks like a sheet in a register: each row is one record (one student), and each column is one detail (name, class, city).

SQL (Structured Query Language, said "S-Q-L" or "sequel") is the language you use to ask a database questions. You write a query, and the database sends back a table of results.

The boxes on this page run SQLite, a small but real database, right inside your browser. MySQL and PostgreSQL, used by many websites, understand the same core SQL with small differences. We point those out when they matter.

Press ▶ Run. The first query works without any table. The second shows every row and column of our practice table, students. Try it: change the greeting text and run again.

📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
🗄️ SQL — you can edit this query
Result
┌───────────────┐
│ 'Hello, SQL!' │
├───────────────┤
│ Hello, SQL!   │
└───────────────┘
(1 row)

┌────┬─────────────┬───────┬─────────┬─────────┬────────────┐
│ id │ name        │ class │ section │ city    │ joined     │
├────┼─────────────┼───────┼─────────┼─────────┼────────────┤
│ 1  │ Riya Sharma │ 12    │ A       │ Pune    │ 2019-06-10 │
│ 2  │ Aman Verma  │ 12    │ A       │ Mumbai  │ 2020-06-15 │
│ 3  │ Sara Khan   │ 12    │ B       │ Pune    │ 2019-06-10 │
│ 4  │ Dev Patil   │ 11    │ A       │ Nagpur  │ 2021-06-14 │
│ 5  │ Meera Iyer  │ 11    │ B       │ Chennai │ 2021-06-14 │
│ 6  │ Kabir Singh │ 12    │ B       │ Delhi   │ 2022-07-01 │
│ 7  │ Anaya Joshi │ 11    │ A       │ Pune    │ 2023-06-12 │
│ 8  │ Rohan Das   │ 12    │ A       │ NULL    │ 2023-06-12 │
└────┴─────────────┴───────┴─────────┴─────────┴────────────┘
(8 rows)

💡 Every Run starts with a fresh copy of the practice school database (made-up data), so you cannot break anything.

How do you SELECT columns from a table in SQL?

SELECT says which columns you want. FROM says which table to read. The star (*) means "all columns", but real apps usually name only the columns they need.

You can also calculate a new column. Here, amount * 12 shows a monthly fee as a yearly total in ₹.

Keywords like SELECT and FROM are written in CAPITALS by habit, so they stand out. SQL does not mind the case of keywords. End every statement with a semicolon (;).

Try it: add the column section to the first query, separated by a comma.

📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
🗄️ SQL — you can edit this query
Result
┌─────────────┬───────┬─────────┐
│ name        │ class │ city    │
├─────────────┼───────┼─────────┤
│ Riya Sharma │ 12    │ Pune    │
│ Aman Verma  │ 12    │ Mumbai  │
│ Sara Khan   │ 12    │ Pune    │
│ Dev Patil   │ 11    │ Nagpur  │
│ Meera Iyer  │ 11    │ Chennai │
│ Kabir Singh │ 12    │ Delhi   │
│ Anaya Joshi │ 11    │ Pune    │
│ Rohan Das   │ 12    │ NULL    │
└─────────────┴───────┴─────────┘
(8 rows)

┌────────────┬─────────┬────────┬─────────────┐
│ student_id │ month   │ amount │ amount * 12 │
├────────────┼─────────┼────────┼─────────────┤
│ 1          │ 2026-06 │ 4500   │ 54000       │
│ 1          │ 2026-07 │ 4500   │ 54000       │
│ 2          │ 2026-06 │ 4500   │ 54000       │
│ 2          │ 2026-07 │ 4500   │ 54000       │
│ 3          │ 2026-06 │ 4500   │ 54000       │
│ 4          │ 2026-06 │ 4000   │ 48000       │
│ 4          │ 2026-07 │ 4000   │ 48000       │
│ 5          │ 2026-06 │ 4000   │ 48000       │
│ 6          │ 2026-06 │ 4500   │ 54000       │
│ 7          │ 2026-06 │ 4000   │ 48000       │
│ 8          │ 2026-06 │ 4500   │ 54000       │
└────────────┴─────────┴────────┴─────────────┘
(11 rows)

How does the WHERE clause filter rows in SQL?

WHERE keeps only the rows where a condition is true. Compare with =, <> (not equal), <, >, <= and >=.

Text values go inside single quotes: 'Pune'. Numbers do not need quotes: class = 12.

Join two conditions with AND (both must be true) or OR (at least one is true). Use brackets when you mix them, so the meaning is clear.

Try it: change 'Pune' to 'Mumbai', or change 80 to 90 in the second query.

📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
🗄️ SQL — you can edit this query
Result
┌─────────────┬───────┬─────────┐
│ name        │ class │ section │
├─────────────┼───────┼─────────┤
│ Riya Sharma │ 12    │ A       │
│ Sara Khan   │ 12    │ B       │
│ Anaya Joshi │ 11    │ A       │
└─────────────┴───────┴─────────┘
(3 rows)

┌────────────┬────────────┬───────┐
│ student_id │ subject_id │ marks │
├────────────┼────────────┼───────┤
│ 1          │ 1          │ 92    │
│ 3          │ 1          │ 81    │
│ 5          │ 1          │ 95    │
└────────────┴────────────┴───────┘
(3 rows)

How do you sort results with ORDER BY?

A database does not promise any row order. If order matters, say so with ORDER BY.

ASC sorts small to big (A to Z). It is the default. DESC sorts big to small (Z to A).

You can sort by more than one column. The second column breaks ties in the first. Below, students are sorted by class (12 first), then by name A to Z inside each class.

Try it: remove DESC and run again. Which class comes first now?

📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
🗄️ SQL — you can edit this query
Result
┌─────────────┬───────┬────────────┐
│ name        │ class │ joined     │
├─────────────┼───────┼────────────┤
│ Aman Verma  │ 12    │ 2020-06-15 │
│ Kabir Singh │ 12    │ 2022-07-01 │
│ Riya Sharma │ 12    │ 2019-06-10 │
│ Rohan Das   │ 12    │ 2023-06-12 │
│ Sara Khan   │ 12    │ 2019-06-10 │
│ Anaya Joshi │ 11    │ 2023-06-12 │
│ Dev Patil   │ 11    │ 2021-06-14 │
│ Meera Iyer  │ 11    │ 2021-06-14 │
└─────────────┴───────┴────────────┘
(8 rows)

How do LIMIT and OFFSET work in SQL?

LIMIT n keeps only the first n rows of the result. With ORDER BY, it answers "top 3" questions.

OFFSET skips rows first. LIMIT 3 OFFSET 3 gives rows 4 to 6. Websites use this for "page 2" of a list.

LIMIT works in SQLite, MySQL and PostgreSQL. SQL Server uses TOP instead, and the SQL standard also has FETCH FIRST 3 ROWS ONLY.

Try it: change subject_id = 1 (Maths) to 4 (Computer Science) to see the top 3 there.

📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
🗄️ SQL — you can edit this query
Result
┌────────────┬───────┐
│ student_id │ marks │
├────────────┼───────┤
│ 5          │ 95    │
│ 1          │ 92    │
│ 3          │ 81    │
└────────────┴───────┘
(3 rows)

┌────────────┬───────┐
│ student_id │ marks │
├────────────┼───────┤
│ 2          │ 75    │
│ 7          │ 73    │
│ 4          │ 64    │
└────────────┴───────┘
(3 rows)

💡 Lines starting with -- are comments. The database skips them.

In what order does SQL run the clauses of a query?

You must write the clauses in this order: SELECT … FROM … WHERE … ORDER BY … LIMIT. Any other order is a syntax error.

The database thinks in a different order. It first reads the table (FROM), then keeps matching rows (WHERE), then picks the columns (SELECT), then sorts (ORDER BY), and last cuts the list (LIMIT).

That is why LIMIT 2 below gives the 2 class 11 students who joined most recently, not any 2 rows. Two students joined on the same day, so name breaks the tie. Try it: change 11 to 12 and run again.

📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
🗄️ SQL — you can edit this query
Result
┌─────────────┬────────────┐
│ name        │ joined     │
├─────────────┼────────────┤
│ Anaya Joshi │ 2023-06-12 │
│ Dev Patil   │ 2021-06-14 │
└─────────────┴────────────┘
(2 rows)

🤔 Predict the output

Think like SQL: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this query return?

📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
SELECT name
FROM students
WHERE city = 'Pune' AND class = 12
ORDER BY name;

Question 2. Computer Science is subject 4. What does this return?

📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
SELECT marks
FROM marks
WHERE subject_id = 4
ORDER BY marks DESC
LIMIT 2;

Question 3. Notice the small p in 'pune'. What does this return in SQLite?

📋 The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
SELECT name
FROM students
WHERE city = 'pune';

🧩 Your challenge

The fees clerk wants a list of unpaid fees (paid = 0). Show student_id, month and amount, with the biggest amount first. When amounts are equal, sort by student_id from small to big. Expected first row: 2 | 2026-07 | 4500.

Expected result :
┌────────────┬─────────┬────────┐
│ student_id │ month   │ amount │
├────────────┼─────────┼────────┤
│ 2          │ 2026-07 │ 4500   │
│ 6          │ 2026-06 │ 4500   │
│ 4          │ 2026-07 │ 4000   │
└────────────┴─────────┴────────┘
(3 rows)
📋 Tables in this example: students, subjects, teachers, marks, fees — tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons)
CREATE TABLE students (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  class   INTEGER NOT NULL,
  section TEXT NOT NULL,
  city    TEXT,
  joined  TEXT NOT NULL
);
INSERT INTO students VALUES
 (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'),
 (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'),
 (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'),
 (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'),
 (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'),
 (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'),
 (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'),
 (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12');
CREATE TABLE subjects (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE
);
INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English');
CREATE TABLE teachers (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  subject_id INTEGER REFERENCES subjects(id)
);
INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL);
CREATE TABLE marks (
  student_id INTEGER NOT NULL REFERENCES students(id),
  subject_id INTEGER NOT NULL REFERENCES subjects(id),
  marks      INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100),
  PRIMARY KEY (student_id, subject_id)
);
INSERT INTO marks VALUES
 (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84),
 (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90),
 (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88),
 (4,1,64),(4,2,58),(4,4,88),(4,5,71),
 (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86),
 (6,1,55),(6,2,62),(6,3,49),(6,5,67),
 (7,1,73),(7,4,99),(7,5,80);
CREATE TABLE fees (
  student_id INTEGER NOT NULL REFERENCES students(id),
  month      TEXT NOT NULL,
  amount     INTEGER NOT NULL,
  paid       INTEGER NOT NULL DEFAULT 0
);
INSERT INTO fees VALUES
 (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0),
 (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1),
 (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
🗄️ SQL — you can edit this query

⚠️ Common mistakes (and how to fix them)

❌ Wrong

SELECT name FROM students WHERE city = Pune;

Error: no such column: Pune

✅ Right

SELECT name FROM students WHERE city = 'Pune';

Without quotes, SQL thinks Pune is a column name. Text values always go inside single quotes.

❌ Wrong

SELECT name FROM students ORDER BY name WHERE class = 11;

Error: near "WHERE": syntax error

✅ Right

SELECT name FROM students WHERE class = 11 ORDER BY name;

Clauses have a fixed order: FROM, then WHERE, then ORDER BY, then LIMIT.

❌ Wrong

SELECT name city FROM students ORDER BY id;

Runs, but gives the wrong result: ┌─────────────┐ │ city │ ├─────────────┤ │ Riya Sharma │ │ Aman Verma │ │ Sara Khan │ │ Dev Patil │ │ Meera Iyer │

✅ Right

SELECT name, city FROM students ORDER BY id;

Without the comma, SQL reads city as a new heading for the name column. You get one column of names called city, and no error to warn you.

📌 Remember

  • A relational database stores data in tables made of rows (records) and columns (details).
  • SELECT picks columns, FROM picks the table, and * means all columns.
  • WHERE keeps rows that match a condition; text goes in single quotes, numbers do not.
  • ORDER BY sorts (ASC is the default, DESC reverses); never rely on row order without it.
  • LIMIT keeps the first n rows, and OFFSET skips rows first.
  • Write clauses in the order SELECT, FROM, WHERE, ORDER BY, LIMIT.

Finished the quiz and the challenge?

Questions students ask

Is SQL a programming language?

SQL is a query language. You describe what data you want, and the database works out how to get it. It is not used to build whole apps alone, but almost every app that stores data uses SQL behind the scenes.

What is the difference between SQL, SQLite and MySQL?

SQL is the language. SQLite, MySQL and PostgreSQL are database programs that understand it. SQLite keeps a whole database in one file and runs inside apps (and this page). MySQL and PostgreSQL run as servers that many users share.

Is SQL case-sensitive?

Keywords are not: select and SELECT both work. Text values can be: in SQLite and PostgreSQL, 'Pune' = 'pune' is false. MySQL usually treats them as equal, depending on its settings.

Do I need to install anything to practise SQL?

Not for this course. Every ▶ Run box runs a real SQLite database in your browser. Later you can install SQLite, MySQL or PostgreSQL on a computer to keep your own databases.