SQL text practice: LIKE patterns and string functions

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Six exercises on text: matching patterns with LIKE, changing case, building labels with || and cutting text with SUBSTR. All on the shelter's pets table.

Write each query, press Check answer and get told right away whether your result matches. Stuck? Take a hint, or open the answer. New to the topic? Read the SQL string functions guide first.

Answers are checked on the engine you pick.
The tables

Paws & Whiskers Shelter

  • pets (id, name, species, breed, age, weight_kg, adopted, arrived_on)
  • adoption_requests (id, pet_name, email, phone, requested_on, budget)
  1. 1

    Find pets whose name starts with P. Show their names.

    From the lesson Patterns and NULL · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name FROM pets WHERE name LIKE 'P%';

    Expected result (both engines): 3 rows

    name
    Pickles
    Pepper
    Pumpkin
  2. 2

    Find pets whose name ends in o. Show their names.

    From the lesson Patterns and NULL · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name FROM pets WHERE name LIKE '%o';

    Expected result (both engines): 2 rows

    name
    Bruno
    Gizmo
  3. 3

    Find pets whose name is exactly four letters long. Show their names.

    From the lesson Patterns and NULL · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name FROM pets WHERE name LIKE '____';

    Expected result (both engines): 4 rows

    name
    Luna
    Tofu
    Kiwi
    Bean
  4. 4

    Show every pet's name in capitals, in one column called name_upper.

    From the lesson Text functions · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT UPPER(name) AS name_upper FROM pets;

    Expected result (both engines): first 8 of 20 rows

    name_upper
    BISCUIT
    MOCHI
    PICKLES
    LUNA
    WAFFLES
    BRUNO
    PEPPER
    NOODLE
  5. 5

    Build a label like Biscuit the dog for every pet (name, then the , then species), in a column called label.

    From the lesson Text functions · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name || ' the ' || species AS label FROM pets;

    Expected result (both engines): first 8 of 20 rows

    label
    Biscuit the dog
    Mochi the cat
    Pickles the dog
    Luna the cat
    Waffles the rabbit
    Bruno the dog
    Pepper the cat
    Noodle the dog
  6. 6

    Show every pet's name and its first letter as initial.

    From the lesson Text functions · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name, SUBSTR(name, 1, 1) AS initial FROM pets;

    Expected result (both engines): first 8 of 20 rows

    nameinitial
    BiscuitB
    MochiM
    PicklesP
    LunaL
    WafflesW
    BrunoB
    PepperP
    NoodleN

The lessons have more like these

Each lesson teaches the idea step by step first, then gives you exercises with instant feedback and a daily streak.

More practice sets

Check your level

The free SQL test takes about 10 minutes and tells you which lesson to start with.