Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    SQLSQL

    First questions to the data

    • What are SQL and databases?
    • SELECT: choosing columns
    • WHERE: filtering rows
    • ORDER BY and LIMIT: sort and take the top

    Summarizing and grouping

    • Aggregates: COUNT, SUM, AVG
    • GROUP BY and HAVING
    • CASE, COALESCE and text functions

    Joining tables

    • Keys and relationships
    • INNER JOIN
    • LEFT JOIN: leave nobody out
    • Three-table joins and self joins
    • Subqueries and CTEs

    Building tables and changing data

    • CREATE TABLE and constraints
    • INSERT, UPDATE and DELETE
    • Transactions: all or nothing
    • SQL injection: the most dangerous mistake

    Professional level

    • Indexes: why is my query slow?
    • Window functions: rankings and totals without losing rows
    • Designing tables: normalization
    • Project: analytics for a learning platform
    SQL

    Summarizing and grouping · Lesson 7 of 20

    CASE, COALESCE and text functions

    An if inside a query, a fallback for NULL, and shaping text and numbers.

    9 minutes2 games

    CASE is SQL's if: it tests conditions in order and returns the value of the first true one, or the ELSE value if none match.

    SQL
    SELECT name,
           xp,
           CASE
             WHEN xp >= 300 THEN 'gold'
             WHEN xp >= 150 THEN 'silver'
             WHEN xp IS NULL THEN 'new'
             ELSE 'bronze'
           END AS badge
    FROM students;

    Order matters: put xp >= 150 first and Laila (320) gets silver, since that's the first true condition.

    COALESCE: a fallback for NULL

    COALESCE(a, b, c) returns the first value that isn't NULL. Handy for display and maths: COALESCE(xp, 0) makes Youssef zero instead of NULL.

    SQL
    SELECT UPPER(name) AS name,
           LENGTH(name) AS letters,
           name || ' from ' || city AS intro,
           COALESCE(xp, 0) AS xp
    FROM students
    WHERE city = 'Alexandria';
    FunctionWhat it does

    UPPER(s) / LOWER(s)

    upper / lower case

    LENGTH(s)

    number of characters

    a || b

    join text (MySQL: CONCAT)

    SUBSTR(s, 1, 3)

    part of the text

    ROUND(x, 2)

    round to 2 decimals

    COALESCE(a, b)

    first non-NULL value

    Important

    Date functions differ most between databases. SQLite: strftime('%Y', joined); PostgreSQL: EXTRACT(YEAR FROM joined); MySQL: YEAR(joined). When moving a query between systems, check the dates first.

    What's the output?

    SELECT name,
           CASE WHEN xp >= 300 THEN 'gold' ELSE 'other' END AS level
    FROM students
    WHERE id IN (1, 5);

    What will this code print?

    Interview question

    When do you use COALESCE, and how is it different from NULLIF?

    Exercise

    For courses in the web track: show title and a column kind that is free when the price is zero and paid otherwise.

    Expected result:
    title | kind
    HTML & CSS | free
    JavaScript | paid
    React | paid
    SQL
    SELECT title
    FROM courses
    WHERE track = 'web';

    Learn more from

    • PostgreSQL docs: Conditional expressionspostgresql.org
    • SQLite: Core functionssqlite.org
    • SQLite: Expressions (CASE)sqlite.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousGROUP BY and HAVINGNextKeys and relationships
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.