11 November 2026

SQL for Beginners: What SQL Is and How Databases Work

  Asp.Net CS By Example       11 November 2026

SQL BEGINNER COURSE · FIRST LESSON

SQL (Structured Query Language) lets you store and ask questions about data in a relational database. If a school app needs to find students in each department, SQL can read the student records and return just the result you need. This lesson starts with a tiny table you can create yourself, so every query has real data to work with.

How an app uses SQL
School appSQL questionStudents table

What are tables, rows, and columns?

A table groups records of one kind. In our students table, each row is one student and each column describes one fact, such as student_name or department. A database can hold several related tables. An application can send SQL to the database instead of loading every record and searching it in application code.

The example table after inserting four rows
student_idstudent_namedepartmentactive
1AshaScience1
2RaviArts1
3MeeraScience1
4IrfanArts0

Run your first complete SQL example

Use a fresh SQLite database or a practice database in another SQL tool. Run these statements in order. The example supplies its own table and rows; you do not need a download.

CREATE TABLE students (
  student_id INTEGER PRIMARY KEY,
  student_name VARCHAR(50) NOT NULL,
  department VARCHAR(30) NOT NULL,
  active INTEGER NOT NULL
);

INSERT INTO students (student_id, student_name, department, active)
VALUES
  (1, 'Asha', 'Science', 1),
  (2, 'Ravi', 'Arts', 1),
  (3, 'Meera', 'Science', 1),
  (4, 'Irfan', 'Arts', 0);

SELECT student_name, department
FROM students
WHERE active = 1
ORDER BY student_name;

CREATE TABLE defines columns. INSERT adds records. SELECT chooses output columns, FROM names the table, WHERE keeps only active students, and ORDER BY sorts names. Here 1 means active and 0 means inactive by our example's rule; these values are not a universal database rule.

Expected result: Asha | Science; Meera | Science; Ravi | Arts. Irfan is excluded because his active value is 0.

Count active students by department

Now ask a different question of the same four rows:

SELECT department, COUNT(*) AS student_count
FROM students
WHERE active = 1
GROUP BY department
ORDER BY student_count DESC, department;

WHERE removes the inactive row before counting. GROUP BY puts the remaining rows into department groups. COUNT(*) counts rows in each group. The result is Science | 2, then Arts | 1. The second sort key, department, makes ties predictable.

Common beginner mistakes

  • “No such table”: run the CREATE TABLE and INSERT statements in the same database before the SELECT.
  • Creating the table twice: use a fresh practice database, or remove the old practice table before repeating the setup.
  • Wrong column name: SQL reads the names defined in CREATE TABLE; check their spelling.
  • Missing WHERE: your count will include Irfan and change Arts from 1 to 2.

Practice and next lesson

Add an active student named Priya in Arts. Predict both query results before running them. Then change Meera to inactive with UPDATE students SET active = 0 WHERE student_id = 3; and explain the new counts. Practice on a copy of your data; UPDATE changes stored rows.

← Complete SQL learning path · Next: SELECT and WHERE

The example and diagram were written for this blog. For details of how SQLite evaluates WHERE, GROUP BY, and ORDER BY, see the official SQLite SELECT documentation.

logoblog

Thanks for reading SQL for Beginners: What SQL Is and How Databases Work

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.