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

Keys and relations

107 min

In a real database, data does not sit in one huge table but in several linked ones. What links them are keys.

Why one table is not enough

If every student row also held the faculty name, its dean and its building, one faculty’s details would be repeated hundreds of times. Change of dean? Update hundreds of rows — and miss one, and the database starts contradicting itself.

The solution: facts about a faculty are stored once in a separate faculties table, and a group only remembers the faculty’s number (faculty_id).

Primary key

A column that tells every row apart. Usually it is id:

  • it never repeats — two students never share an id;
  • it is never empty;
  • it never changes — a name may change, an id never does.

Foreign key

A column that points to another table’s primary key: students.group_id → study_groups.id, study_groups.faculty_id → faculties.id.

university· Try changing it
SELECT id, name, faculty_id FROM study_groups LIMIT 5;

faculty_id = 1 means the group belongs to faculty number 1. To see which faculty that is, look in the second table:

university· Try changing it
SELECT id, name FROM faculties WHERE id = 1;

A foreign key protects the database: you cannot enter the number of a faculty that does not exist, and you cannot delete a faculty that still has groups. In the Practice section you will try this with your own hands.

Kinds of relationship

  • One-to-many (1:N) — the most common: one faculty has many groups, one group has many students. The foreign key sits on the “many” side.
  • Many-to-many (M:N) — a student gets grades in many subjects, and many students get grades in one subject. This needs a third table in between — here it is grades (student_id + subject_id).
  • One-to-one (1:1) — rare: for example, an employee and their passport details.

Reading a schema

In the Arena, every database has a schema: the lines show the foreign keys. Looking at the schema before writing a query is a good habit: what is in which table, and which column links them.

1Exercise

From the students table, show the group_id of the student whose id is 1.


Remember

  • A primary key is the row’s unique number.
  • A foreign key points to a row in another table and blocks broken links.
  • An M:N relationship is built with a link table.
Previous lessonGROUP BY and HAVINGNext lessonINNER JOIN

Contents

  • Why one table is not enough
  • Primary key
  • Foreign key
  • Kinds of relationship
  • Reading a schema
  • Remember
LearnPracticeProblemsArenaLeaderboard

SQLab