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/Basics

Sorting, LIMIT and DISTINCT

057 min

The database returns rows in no guaranteed order. To sort the result, take only the first few rows or remove duplicates, there are three commands.

ORDER BY — sorting

university· Try changing it
SELECT first_name, last_name, salary FROM teachers ORDER BY salary;

By default it sorts ascending (ASC). For descending order use DESC:

university· Try changing it
SELECT first_name, last_name, salary FROM teachers ORDER BY salary DESC;

By several columns: first by the first one, and on a tie by the second.

university· Try changing it
SELECT last_name, first_name, course FROM students ORDER BY course DESC, last_name;

LIMIT — only the first N rows

“The 3 best-paid teachers”:

university· Try changing it
SELECT first_name, last_name, salary FROM teachers ORDER BY salary DESC LIMIT 3;

Always use LIMIT together with ORDER BY — otherwise “which 3” you get is down to chance.

For the next “page” there is OFFSET: LIMIT 3 OFFSET 3 gives rows 4, 5 and 6.

DISTINCT — without repeats

Which cities do the students come from?

university· Try changing it
SELECT DISTINCT city FROM students ORDER BY city;

DISTINCT applies to the whole row: SELECT DISTINCT city, course gives the different city–year pairs.

Where does NULL go?

PostgreSQL treats NULL as the largest value when sorting: it comes last in ascending order and first with DESC. You can change that: ORDER BY scholarship DESC NULLS LAST.

The order of the parts

The parts of a query are always written in this order:

SELECT … FROM … WHERE … ORDER BY … LIMIT …;
1Exercise

Show name and price of the 5 cheapest products, cheapest first. If prices are equal, sort by name alphabetically.


2Exercise

Show which brands appear among the products, without repeats (brand).


Remember

  • ORDER BY column [DESC] sorts; several columns are separated by commas.
  • LIMIT n gives the first n rows; always with ORDER BY.
  • DISTINCT removes duplicates.
Previous lessonIN, BETWEEN, LIKE and NULLNext lessonExpressions, functions and CASE

Contents

  • ORDER BY — sorting
  • LIMIT — only the first N rows
  • DISTINCT — without repeats
  • Where does NULL go?
  • The order of the parts
  • Remember
LearnPracticeProblemsArenaLeaderboard

SQLab