Thuta Learning
SQL
BasicData & DatabasesBeginner

SQL HOME

SQLLesson 112 min

What you'll walk away with

  • Explain that SQL is a declarative language: you describe what you want, not how to fetch it
  • Know what the five chapters (Basic, Intermediate, Advanced, Projects, Exercises) cover
  • Read a short SELECT ... FROM ... WHERE ... query aloud like an English sentence
  • Know that this course teaches PostgreSQL and flags where other databases differ

Explain it simply

Explain it simply

Behind almost every app sits a database that stores its data. A shop's customer list, its orders, students' marks, cinema bookings: all of them live as rows inside tables. SQL (Structured Query Language) is the language you use to talk to that database.

What makes SQL special is that you only state the result you want, such as "show me the customers in Mexico". You do not write loops that walk the rows one by one; the database works out that part itself. This course takes you step by step from reading data to changing it, designing tables and finally building a small project.

Problem → Why → Solution

The problem: you cannot search growing data by hand

Imagine a shop that starts keeping its orders in a spreadsheet. With fifty rows you can still find things by eye. Once there are hundreds of thousands of rows and the app needs to answer "which customers from Mandalay bought something this month?", many developers load every row into memory and loop over them in application code.

slow-search.js
// Without SQL: download everything, then filter in the app
const allOrders = await loadEveryOrderFromDisk(); // 500,000 rows
const result = [];
for (const order of allOrders) {
  if (order.city === "Mandalay" && order.month === "2024-09") {
    result.push(order.customerName);
  }
}
// Slow, memory-hungry, and rewritten for every new question

Why is this approach a dead end?

Every new question needs a new loop, and all the data has to travel over the network into memory first. A database keeps its data organised with indexes and can find just the rows you need. With SQL you write that question as a single query and leave the searching strategy to the database.

Core concept: SQL describes the result

SQL is the standard language of relational databases. A relational database stores data in tables, and each table has columns (kinds of data) and rows (one record each). PostgreSQL, MySQL, SQLite, SQL Server and Oracle all understand SQL, but each has its own dialect with small differences in the details.

SQL is called a declarative language. In Python or JavaScript you write the steps: read each row, test it, push it into a list. In SQL you only describe the shape of the result, such as "give me the titles of the Intermediate lessons from the lessons table". The database's query planner decides whether to use an index or scan the whole table.

The result of a query is itself a table, called a result set. That is why queries can later be stacked and combined with each other.

SQL is not a language for writing a whole app. You build screens, buttons and business logic in Python, JavaScript or Java, and whenever data is needed the app sends an SQL statement to the database server. The server runs it and sends the result set back. So whichever language your app uses, the SQL part stays the same.

This course uses PostgreSQL. It is open source and follows the SQL standard closely, so most of what you learn here also works elsewhere. Where MySQL or SQLite behave differently, for example in what double quotes mean or how upper and lower case compare, the lessons point it out explicitly. The course is organised into five chapters:

ChapterLessonsWhat you will be able to do
Basic1-11Read data with SELECT, WHERE, NULL, AND/OR, ORDER BY, DISTINCT, LIKE
Intermediate12-14Change data with INSERT, UPDATE, DELETE
Advanced15-30Aggregates, JOINs, subqueries, CREATE TABLE, constraints, transactions, indexes
Projects31-33Build a bookstore database from tables to reports
Exercises34-35Practice sets for filtering, joins and aggregates
Mental model: question → query → result set
  1. Say the question in plain words
  2. Rewrite it as SELECT/FROM/WHERE
  3. The database picks how to search
  4. You get a result set (a table)

You decide what you want; the database decides how to find it.

General pattern

sql
SELECT column_a, column_b   -- which columns to show
FROM table_name             -- which table to read
WHERE condition;            -- which rows to keep

Most queries in this course start from these three parts. SELECT picks columns, FROM names the table and WHERE picks rows. Text after -- is a comment, which the database ignores.

Example: query this course itself

Imagine this course's lesson list stored in a table called lessons. It has three columns: position (the lesson number), title and chapter. There are 35 rows, one per lesson. The query below asks only for the lessons in the Intermediate chapter.

first-query.sql
SELECT position, title
FROM lessons
WHERE chapter = 'Intermediate';

Code breakdown

  1. Line 1SELECT position, title

    The result set will contain only the position and title columns. The chapter column exists in the table, but because it is not requested here it does not appear in the output.

  2. Line 2FROM lessons

    The data comes from the lessons table. If no such table exists, PostgreSQL answers with the error relation "lessons" does not exist.

  3. Line 3WHERE chapter = 'Intermediate';

    Out of 35 rows, only those whose chapter equals 'Intermediate' are kept. Text values go inside single quotes, and the ; marks the end of the statement.

  4. Output

    Three rows come back: INSERT INTO, UPDATE and DELETE. The (3 rows) footer is added by psql, PostgreSQL's command-line client.

You should see
 position |    title
----------+-------------
       12 | INSERT INTO
       13 | UPDATE
       14 | DELETE
(3 rows)

How to study with this course

1. Get somewhere to run PostgreSQL

Installing PostgreSQL on your own machine and connecting with psql is best. If that is not possible yet, any online SQL playground that supports PostgreSQL works too.

2. Build the sample tables once

Lesson 3 (Database Intro) contains a script that creates the Customers and Orders tables. Run it once and you can try the queries of the following lessons yourself.

3. Predict the result before you run it

Before running a query, guess how many rows and which columns will come back. When your guess and the result disagree, that gap is exactly where the learning is.

In real projects

Where this shows up in real projects

In a web backend, every login, product page view and add-to-cart click turns into one or more SQL statements behind the scenes. Even when you use a tool such as Prisma or the Django ORM, that tool is generating SQL for you, and you need to read SQL to debug a slow query or a wrong result.

SQL is useful outside pure development too. Data analysts write it daily to produce sales reports, product managers use it to check how many people use a feature, and support teams use it to look up a customer's order history. A query is faster than clicking filters in a spreadsheet, and you can save it and run it again next week.

Common mistakes

Common mistakes

1. Thinking about SQL as a loop

People coming from a programming language often think "take the first row, then the next one" and end up writing something complicated where one simple WHERE would do. In SQL, think in sets of rows: write the condition that describes which rows you want, and stop there.

2. Reading examples without running them

SQL reads so easily that it is tempting to think "I get it" and move on. Small errors such as a missing single quote or a forgotten comma only show up when you type the query yourself. Type and run every lesson's query in your own PostgreSQL.

3. Assuming every database speaks identical SQL

An example found online is often written for MySQL and fails in PostgreSQL. Before copying an example, check which database it was written for.

Best practices

Best practices

  • Follow the lessons in order: SELECT and WHERE from the Basic chapter underpin every later lesson.
  • When you check a result, look at the row count and the column names, not just the values.
  • Save the queries you write in a .sql file; it becomes a notebook you can come back to.

Mini challenge

Mini challenge: query the course map

Using the lessons table above (position, title, chapter), write two queries.

  • First query: show only the titles of the lessons in the Projects chapter
  • Second query: show the position and title of the lessons in the Exercises chapter
  • End each statement with ;
course-map.sql
-- TODO 1: titles of the Projects lessons
--   which column?  which table?  which rows?

-- TODO 2: position and title of the Exercises lessons

Expected: The first query returns the three Bookstore Project titles (Parts 1, 2 and 3); the second returns 34 Practice Set 1: SELECT & Filtering and 35 Practice Set 2: Joins & Aggregates.

Show hint

Follow the example's three parts: SELECT, FROM, WHERE. Put the chapter name in single quotes and match its capitalisation exactly; 'projects' and 'Projects' are not equal.

Show solution
course-map.sql
SELECT title
FROM lessons
WHERE chapter = 'Projects';

SELECT position, title
FROM lessons
WHERE chapter = 'Exercises';

Quick quiz 1 / 3

What does it mean that SQL is a "declarative" language?

Quick quiz 2 / 3

How many rows does the example query (WHERE chapter = 'Intermediate') return?

Quick quiz 3 / 3

Which database does this course teach, and how does it handle differences from other databases?

What to remember

  • SQL is the standard language for querying and changing tables in relational databases.
  • SQL is declarative: you say what you want and the database decides how to get it.
  • Every query returns a table called a result set.
  • The course runs Basic → Intermediate → Advanced → Projects → Exercises, using PostgreSQL throughout.

PostgreSQL SQL Tutorial — PostgreSQL Global Development Group

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

SQL HOME | Thuta Learning