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

Expressions, functions and CASE

069 min

SELECT does more than show columns — it calculates. This lesson covers arithmetic, working with text and “if … then” with CASE.

Arithmetic

+ - * / and % (remainder) all work:

shop· Try changing it
SELECT name, price, price * 0.88 AS discounted FROM products LIMIT 5;

Watch out when dividing a whole number by a whole number. In PostgreSQL 7 / 2 = 3 (the fraction is dropped), while MySQL gives 3.5000. If you need a fraction, make one of the numbers a decimal: 7 * 1.0 / 2.

Rounding: ROUND

shop· Try changing it
SELECT name, ROUND(price / 12, 0) AS per_month FROM products LIMIT 5;

ROUND(number, n) rounds to n decimal places. There are also CEIL (up) and FLOOR (down).

Text functions

Function What it does
UPPER(s) / LOWER(s) upper / lower case
LENGTH(s) number of characters
TRIM(s) removes spaces at the start and end
SUBSTRING(s, 1, 3) 3 characters starting at the 1st
REPLACE(s, 'a', 'b') replace

Joining strings:

In PostgreSQL use the || operator or the CONCAT function:

university· Try changing it
SELECT first_name || ' ' || last_name AS full_name FROM students LIMIT 5;

Calculating with NULL: COALESCE

Any calculation involving NULL gives NULL: 5 + NULL = NULL. COALESCE(a, b) takes b when a is empty:

university· Try changing it
SELECT first_name, COALESCE(scholarship, 0) AS scholarship FROM students LIMIT 8;

CASE — “if … then”

university· Try changing it
SELECT student_id, score,
       CASE
         WHEN score >= 86 THEN 'excellent'
         WHEN score >= 71 THEN 'good'
         WHEN score >= 56 THEN 'pass'
         ELSE 'fail'
       END AS mark
FROM grades
LIMIT 8;

The conditions are checked top to bottom and the first match wins. If none matches, ELSE is used, and if there is no ELSE, the result is NULL.

1Exercise

Show each product’s name and the total value of its stock (price * stock).


2Exercise

Show drivers’ first_name and a category by rating: 4.8 and above — 'top', the rest — 'oddiy', and those without a rating — 'yangi'.


Remember

  • You can calculate inside SELECT; be careful when dividing whole numbers.
  • COALESCE(x, 0) puts a value in place of NULL.
  • CASE WHEN … THEN … ELSE … END picks a value by condition.
Previous lessonSorting, LIMIT and DISTINCTNext lessonWorking with dates

Contents

  • Arithmetic
  • Rounding: ROUND
  • Text functions
  • Calculating with NULL: COALESCE
  • CASE — “if … then”
  • Remember
LearnPracticeProblemsArenaLeaderboard

SQLab