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/Advanced queries

Subqueries

1410 min

A subquery is a query written in brackets inside another query. It runs first and hands its result to the outer query.

A subquery that returns one value

“Teachers who earn more than the average” — we do not know the average in advance, but the database can work it out:

university· Try changing it
SELECT first_name, last_name, salary
FROM teachers
WHERE salary > (SELECT AVG(salary) FROM teachers)
ORDER BY salary DESC;

The subquery returns one number, and the outer query compares against it.

A subquery that returns a list: IN

“Students who got at least one score of 95 or more”:

university· Try changing it
SELECT first_name, last_name
FROM students
WHERE id IN (SELECT student_id FROM grades WHERE score >= 95);

If a subquery returns several rows, use IN, not =. With = the database complains that the subquery returns more than one row.

EXISTS — “is there any?”

EXISTS is true when the subquery returns at least one row. It usually refers to the outer row:

shop· Try changing it
SELECT p.name
FROM products p
WHERE NOT EXISTS (SELECT 1 FROM order_items oi WHERE oi.product_id = p.id);

Products that have never been sold. SELECT 1 — what comes back does not matter, only whether anything does.

Correlated subqueries

The subquery uses a value from the outer row and runs again for every row. “Products more expensive than the average of their own category”:

shop· Try changing it
SELECT p.name, p.price
FROM products p
WHERE p.price > (SELECT AVG(x.price) FROM products x WHERE x.category_id = p.category_id);

A subquery in FROM

A result can be used like a temporary table. It must be given an alias:

shop· Try changing it
SELECT AVG(n) AS avg_orders
FROM (SELECT customer_id, COUNT(*) AS n FROM orders GROUP BY customer_id) t;
1Exercise

Show name and credits of the subjects that have more credits than the average.


2Exercise

How many riders have taken at least one trip? Use IN or EXISTS.


Remember

  • One value — compare with =, >; a list — use IN.
  • EXISTS / NOT EXISTS check whether rows exist.
  • A subquery in FROM needs an alias.
Previous lessonSeveral tables and the self joinNext lessonUNION, INTERSECT, EXCEPT

Contents

  • A subquery that returns one value
  • A subquery that returns a list: IN
  • EXISTS — “is there any?”
  • Correlated subqueries
  • A subquery in FROM
  • Remember
LearnPracticeProblemsArenaLeaderboard

SQLab