Sunday , November 29 2026
Learn SQL from scratch: SELECT, WHERE, ORDER BY, aggregates, GROUP BY, HAVING, JOINs and data changes, with tested outputs.

SQL Basics: SELECT, WHERE, GROUP BY and JOIN with Examples

Updated: 28 November 2026Level: BeginnerReading time: about 12 minutesStandard SQL · tested on SQLite

SQL (Structured Query Language) is the language used to store, read and change data in relational databases such as MySQL, PostgreSQL, SQL Server and SQLite. It is one of the most important skills for data analysts, data scientists and developers.

What You Will Learn

  • What tables, rows, columns and keys are
  • SELECT, WHERE, ORDER BY and LIMIT
  • Aggregate functions with GROUP BY and HAVING
  • Joining tables
  • INSERT, UPDATE and DELETE

Prerequisites

None – this is the starting point.

Key Terms

Term Meaning
Table Data in rows and columns, like a spreadsheet sheet
Row (record) One item, e.g. one student
Column (field) One property, e.g. name or age
Primary key A column that uniquely identifies each row (id)
Foreign key A column that refers to a primary key in another table

Sample Database

Sample database used in this series
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT, city TEXT, age INTEGER);
CREATE TABLE courses  (id INTEGER PRIMARY KEY, title TEXT, fee INTEGER);
CREATE TABLE enrollments (student_id INTEGER REFERENCES students(id),
                          course_id  INTEGER REFERENCES courses(id),
                          marks INTEGER);

INSERT INTO students VALUES (1,'Aman','Ludhiana',21), (2,'Riya','Jalandhar',23),
  (3,'Karan','Ludhiana',25), (4,'Simran','Amritsar',22), (5,'Neha','Jalandhar',24);
INSERT INTO courses VALUES (1,'Python',15000), (2,'Data Science',30000), (3,'Deep Learning',25000);
INSERT INTO enrollments VALUES (1,1,82),(1,2,75),(2,2,91),(3,1,68),(3,3,88),(4,1,95),(5,2,59);
Note: You can run every query here without installing anything: Python has SQLite built in. Run import sqlite3; con = sqlite3.connect(":memory:"), execute the script above with con.executescript(...), then con.execute(query).fetchall(). All outputs on this page were produced that way.

SELECT and WHERE

Example
SELECT name, city
FROM students
WHERE city = 'Ludhiana'
ORDER BY age DESC;

Explanation: SELECT picks the columns, FROM the table, WHERE filters rows, and ORDER BY ... DESC sorts from oldest to youngest.

Output
name   | city
Karan  | Ludhiana
Aman   | Ludhiana
Operator Example
=, <>, >, <, >=, <= age >= 23
AND, OR, NOT city = 'Ludhiana' AND age > 21
IN city IN ('Ludhiana', 'Amritsar')
BETWEEN age BETWEEN 21 AND 23
LIKE name LIKE 'K%' (starts with K)
IS NULL city IS NULL

Aggregates, GROUP BY and HAVING

Example
SELECT COUNT(*) AS total, MIN(age), MAX(age), AVG(age)
FROM students;
Output
total | MIN(age) | MAX(age) | AVG(age)
5     | 21       | 25       | 23.0
Example
SELECT city, COUNT(*) AS students, AVG(age) AS avg_age
FROM students
GROUP BY city
HAVING COUNT(*) >= 2;

Explanation: GROUP BY makes one group per city; aggregates are calculated per group. HAVING filters groups (Amritsar has only one student, so it is removed); WHERE filters rows before grouping.

Output
city      | students | avg_age
Jalandhar | 2        | 23.5
Ludhiana  | 2        | 23.0

JOIN: Combine Tables

Example
SELECT s.name, c.title, e.marks
FROM enrollments e
JOIN students s ON s.id = e.student_id
JOIN courses  c ON c.id = e.course_id
WHERE e.marks >= 80
ORDER BY e.marks DESC;

Explanation: Each enrollment row is matched with its student and its course using the foreign keys. s, c and e are short aliases.

Output
name   | title         | marks
Simran | Python        | 95
Riya   | Data Science  | 91
Karan  | Deep Learning | 88
Aman   | Python        | 82
Join Returns
INNER JOIN (JOIN) Only rows that match in both tables
LEFT JOIN All rows from the left table, matches from the right (NULL if none)
RIGHT JOIN All rows from the right table
FULL OUTER JOIN All rows from both

Changing Data

Example
INSERT INTO students (id, name, city, age) VALUES (6, 'Arjun', 'Patiala', 20);
UPDATE students SET city = 'Mohali' WHERE id = 6;
DELETE FROM students WHERE id = 6;
Note: Always use a WHERE with UPDATE and DELETE. Without it, every row is changed or deleted.

Order of a Query

Written order: SELECT → FROM → JOIN → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. The database actually runs FROM/JOIN first, then WHERE, GROUP BY, HAVING, SELECT, ORDER BY and LIMIT – which is why you cannot use a SELECT alias inside WHERE.

Common Errors

Problem Cause and fix
no such column Spelling, or the column belongs to another table – use the table alias.
Strings without quotes Text values need single quotes: 'Ludhiana'.
Column must appear in GROUP BY Every selected column must be grouped or aggregated.

Series

SQL series: SQL Basics · MySQL · PostgreSQL · MongoDB · Database Design · SQL for Data Analytics · Advanced SQL Queries · Python MySQL series

Interview Questions

Q: WHERE vs HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation.

Q: Primary key vs foreign key?
A primary key uniquely identifies a row; a foreign key links to a primary key in another table.

Q: What does LEFT JOIN return for non-matching rows?
The left row with NULLs in the right table’s columns.

Practice Exercises

  1. List students from Jalandhar older than 22.
  2. Show each course title with its number of students.
  3. Find the student with the highest marks in Python.

Summary

SQL reads data with SELECT, filters with WHERE, summarises with GROUP BY and aggregates, filters groups with HAVING, and combines tables with JOIN. INSERT, UPDATE and DELETE change data.

Next: MySQL · SQL for Data Analytics

About Machine Learning

Check Also

Database Design: Keys, Relationships and Normalization

Design relational databases: entities, keys, constraints, one-to-many and many-to-many relationships, 1NF-3NF and denormalization.