SQLab
LearnPracticeProblemsArenaLeaderboard
© 2026 SQLab · Tashkent
SQLab

Learn SQL right in the browser — nothing to install.

Platform

  • Learn
  • Practice
  • Problems
  • Arena
  • Leaderboard

Who it is for

  • Students
  • Teachers
  • Universities
© 2026 SQLab. All rights reserved.Made in Uzbekistan, for students.
LearnPracticeProblemsArenaLeaderboard
0/26

Basics

  • What is a database?
  • Your first SELECT
  • WHERE: picking the rows you need
  • IN, BETWEEN, LIKE and NULL
  • Sorting, LIMIT and DISTINCT

Calculations

  • Expressions, functions and CASE
  • Working with dates
  • COUNT, SUM, AVG, MIN, MAX
  • GROUP BY and HAVING

Joining tables

  • Keys and relations
  • INNER JOIN
  • LEFT JOIN and rows without a match
  • Several tables and the self join

Advanced queries

  • Subqueries
  • UNION, INTERSECT, EXCEPT
  • WITH: named queries
  • Window functions

Creating and changing tables

  • CREATE TABLE: types and constraints
  • INSERT, UPDATE, DELETE
  • ALTER, DROP, TRUNCATE
  • Designing a database well

Going professional

  • Indexes and EXPLAIN
  • Transactions and ACID
  • Views: saved queries
  • Functions, procedures and triggers
  • Users, privileges and backups
Learn/Joining tables

Several tables and the self join

138 min

Many questions need three or four tables, not two. And sometimes a table has to be joined with itself.

Joins in a chain

Each JOIN adds one more table to the result. Student → group → faculty:

university· Try changing it
SELECT s.first_name, s.last_name, g.name AS group_name, f.name AS faculty
FROM students s
JOIN study_groups g ON g.id = s.group_id
JOIN faculties f ON f.id = g.faculty_id
LIMIT 8;

Through a link table (M:N) — student, subject and score:

university· Try changing it
SELECT s.first_name, sub.name AS subject, gr.score
FROM grades gr
JOIN students s ON s.id = gr.student_id
JOIN subjects sub ON sub.id = gr.subject_id
ORDER BY gr.score DESC
LIMIT 8;

Every JOIN gets its own ON. Build the chain from the schema: each next table must connect to one already in the query.

Self join — a table joined with itself

In the company database an employee’s manager lives in the same employees table (manager_id). To see employees next to their managers, take the table twice under two aliases:

company· Try changing it
SELECT e.first_name AS employee, m.first_name AS manager
FROM employees e
JOIN employees m ON m.id = e.manager_id
LIMIT 8;

e plays the employee, m plays the manager. Without aliases this query cannot be written.

A calculation across tables

Four tables: order lines → orders → products → categories. Revenue per category from delivered orders:

shop· Try changing it
SELECT c.name AS category, SUM(oi.quantity * oi.unit_price) AS revenue
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
JOIN products p ON p.id = oi.product_id
JOIN categories c ON c.id = p.category_id
WHERE o.status = 'delivered'
GROUP BY c.name
ORDER BY revenue DESC;
1Exercise

For every grade, show its score, the student’s first_name and the subject name (subjects.name).


2Exercise

For every employee who has a manager, show their first_name and the manager’s first_name.


3Exercise

For every completed trip, show the driver’s first_name, the car’s model and the fare.


Remember

  • Every extra table is another JOIN … ON ….
  • Self join: one table, two different aliases.
  • Find the “path” in the schema before writing the query.
Previous lessonLEFT JOIN and rows without a matchNext lessonSubqueries

Contents

  • Joins in a chain
  • Self join — a table joined with itself
  • A calculation across tables
  • Remember
LearnPracticeProblemsArenaLeaderboard

SQLab