← All posts SQL

SQL simplified: study notes for beginners

By AiTech Notes · · 9 min read

SQL (Structured Query Language) simplified study notes cover

These notes explain what SQL is, the three main groups of SQL commands, how to turn a real-world problem into database tables, and two common ways of modelling data.

1. What is SQL?

SQL stands for Structured Query Language.

  • SQL is a language used to communicate with relational databases.
  • It is used to create, manage, retrieve, update and delete data in databases.
  • It lets users interact with the tables in a database.

Example: a school database can use SQL to store and manage student records, staff details, courses and classrooms.

2. The three main categories of SQL

Data Definition Language (DDL)

DDL defines or changes the structure of a database. It is commonly used by database administrators and database engineers.

CommandFunction
CREATECreates a database or table
ALTERModifies an existing table
DROPDeletes a table or database structure
TRUNCATERemoves all records from a table
CREATE TABLE Students (
    Student_id INT PRIMARY KEY,
    Firstname VARCHAR(255),
    Lastname VARCHAR(255)
);

Data Manipulation Language (DML)

DML works with the data stored inside tables. It is commonly used by data analysts and database users, and it supports CRUD operations.

LetterMeaningSQL command
CCreateINSERT
RRetrieveSELECT
UUpdateUPDATE
DDeleteDELETE
-- CREATE (insert a record)
INSERT INTO Students
(Student_id, Firstname, Lastname)
VALUES (1, 'John', 'James');

-- READ (retrieve records)
SELECT * FROM Students;

-- UPDATE (modify a record)
UPDATE Students
SET Firstname = 'Peter'
WHERE Student_id = 1;

-- DELETE (remove a record)
DELETE FROM Students
WHERE Student_id = 1;
Remember: in CRUD, Create means inserting a record, not creating a table.

Data Control Language (DCL)

DCL controls access and permissions. It is commonly handled by database administrators and database engineers.

  • GRANT gives a user permission to access or perform certain operations on a database.
  • REVOKE removes permissions that were granted before.
GRANT SELECT ON Students TO analyst;

REVOKE SELECT ON Students FROM analyst;

3. Modelling real-world problems as database problems

Database modelling means taking information from a real-world situation and organising it into tables.

Example: a tech academy wants a database called academy_db to manage students, staff, courses and classrooms. Each of these becomes a table.

4. Creating table schemas

A table schema describes the structure of a table: its columns, data types and constraints.

Students table

ColumnData typeConstraint
Student_idINTPRIMARY KEY
FirstnameVARCHAR(255)
LastnameVARCHAR(255)
GenderVARCHAR(10)
PhonenumberTEXT
AgeINT
Course_idINTFOREIGN KEY

Courses table

ColumnData typeConstraint
Course_idINTPRIMARY KEY
TitleTEXT

Understanding the relationship

  • Student_id uniquely identifies each student.
  • Course_id uniquely identifies each course.
  • Course_id in the Students table is a foreign key that references the Courses table.

Courses

Course_idTitle
1Data Analysis
2Web Development
3Cybersecurity

Students

Student_idFirstnameCourse_id
101John1
102Mary2
103David1

John and David are enrolled in Data Analysis, while Mary is enrolled in Web Development.

SQL implementation

CREATE TABLE Courses (
    Course_id INT PRIMARY KEY,
    Title TEXT
);

CREATE TABLE Students (
    Student_id INT PRIMARY KEY,
    Firstname VARCHAR(255),
    Lastname VARCHAR(255),
    Gender VARCHAR(10),
    Phonenumber TEXT,
    Age INT,
    Course_id INT,
    FOREIGN KEY (Course_id)
    REFERENCES Courses(Course_id)
);

Create the Courses table first, because the Students table refers to it.

5. Modelling of tables

There are two common modelling approaches.

A. Flat table modelling

All the information is stored in one table.

Student_idStudent_nameCourseStaffClassroom
1JohnSQLMr JamesRoom A
2MaryPythonMrs GraceRoom B
3DavidSQLMr JamesRoom A

Advantages

  • Easy to understand.
  • Simple for small datasets.
  • Related information is easy to view together.

Disadvantages

  • Data is repeated.
  • Hard to maintain as the data grows.
  • Can lead to inconsistencies.

B. Star schema modelling

In a star schema, a central fact table connects to several dimension tables.

  • Fact table: stores measurable events or transactions, such as student enrolments.
  • Dimension tables: store descriptive information, such as students, courses, staff and classrooms.
             Students
                 |
                 |
Courses ---- Enrolments ---- Staff
                 |
                 |
             Classrooms

Advantages

  • Makes reporting and analysis easier.
  • Reduces unnecessary repetition in analytical datasets.
  • Helps organise large amounts of data.
  • Useful for business intelligence and data warehousing.

6. Test your understanding

Try each question first, then open the answer.

  1. What does SQL stand for?
    • Structured Query Language
    • Simple Question Language
    • System Query Logic
    Show answer

    Structured Query Language

  2. Which SQL category is used to create and modify table structures?
    • DML
    • DDL
    • DCL
    Show answer

    DDL

  3. Which command retrieves records from a table?
    • INSERT
    • UPDATE
    • SELECT
    Show answer

    SELECT

  4. What does CRUD stand for?
    • Create, Read, Update, Delete
    • Create, Remove, Undo, Drop
    • Copy, Retrieve, Update, Display
    Show answer

    Create, Read, Update, Delete

  5. What is the central table in a star schema called?
    • Dimension table
    • Fact table
    • Flat table
    Show answer

    Fact table

Spotted a mistake in this note? Tell us and we will fix it.