Querydrill

SQL string function practice problems

Real data arrives with stray spaces, mixed capitals and numbers stored as text. Cleaning it inside the query, with TRIM, UPPER, REPLACE and a cast, is everyday work that tutorials tend to skip.

These problems include pattern matching with LIKE, building text with ||, and the messy-data exercises where the same category is spelled three ways until you normalize it.

Try one here: Pattern match

Try it yourself

Return the names of products whose name contains the letter o, in upper or lower case.

products (8 rows)

idnamecategoryprice
1Desk LampHome35
2Office ChairHome180
3NotebookStationery4.5
4Pen SetStationery12
5HeadphonesElectronics95
6WebcamElectronics60
7MugHome9
8BackpackTravel70

Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.

Basics

  • Pattern matchReturn the names of products whose name contains the letter o, in upper or lower case.
  • Glue text togetherReturn one text column per customer in the form name (country), for example Anna (NL).
  • Upper case and lengthReturn each product name in upper case, together with the number of characters in the original name.
  • Search without caring about case (ILIKE)Return the names of customers whose name contains the letter e, in upper or lower case. Chloe and Elif both count.

Aggregation

  • Paid revenue per quarterReturn each quarter as text like 2025-Q1 together with its paid revenue. January to March is Q1, April to June is Q2, and so on. In PostgreSQL, to_char(order_date, 'Q') gives the quarter number.

Work mode

  • Clean up the country reportFrom Marta in Marketing: "Our country report shows US, us and ' Us' as three different countries. Send me each country once, in capitals with no spaces around it, with the number of signups. Leave out signups that have no country." Return two columns: country and signups.
  • Find duplicate accountsFrom Sam in Support: "Some people signed up more than once with the same email, typed differently (capitals, extra spaces). List each email that has more than one account, in lowercase with no spaces, and how many accounts it has. Most accounts first, then alphabetically." Return email and accounts.
  • Keep one account per personFrom Sam in Support: "For everyone who signed up more than once with the same email typed differently, I only want their earliest signup. The signup dates come in mixed formats, and if two signups fall on the same day the lowest id wins. Send me one row per person." Return the id of the account to keep and the email in lowercase with no spaces around it.
  • Total revenue from text amountsFrom Priya in Finance: "The amount column is text, like $1,200.00 and ' 30.50'. What is our total revenue from paid payments? Count a payment as paid whatever the capitals or spaces in its status." Return one number, rounded to 2 decimals.
  • Monthly revenue from mixed datesFrom Priya again: "Same paid revenue, but by month please, oldest month first. Careful: paid_at comes in three formats." Return month as YYYY-MM and revenue rounded to 2 decimals.
  • Signups per planFrom Leo in Product: "How many signups are on each plan? Write the plan names in lowercase, and count signups without a plan as unknown." Return plan and signups.
  • Paying customers by countryFrom Marta: "For each country, cleaned as before, how many different people made at least one paid payment? Count a person once even if they have several accounts. Most payers first, then by country." Return country and payers.
  • First payment per customerFrom Sam: "For each customer who paid at least once, what was the date of their first paid payment? One row per email, cleaned to lowercase, oldest first, then by email." Return email and first_paid as YYYY-MM-DD.
  • Failed payments by monthFrom Priya in Finance: "How many payments failed each month, and how much money is that? Status capitals and spaces vary, and the amounts are text like $1,200.00. Oldest month first." Return month as YYYY-MM, failed (a count) and amount rounded to 2 decimals.
  • Revenue per planFrom Leo in Product: "What is our paid revenue per plan? Plan names arrive in any capitals, and some signups have no plan: call those unknown. Amounts are text, and a payment is paid whatever the capitals or spaces in its status." Return plan and revenue rounded to 2 decimals.
  • Signed up, never paidFrom Marta in Marketing: "Which people signed up but never made a paid payment? Treat emails as the same person whatever the capitals or spaces, and count a person as paid if any of their accounts has a paid payment. One row per email, in lowercase." Return email.
  • Days from signup to first paymentFrom Leo: "For each signup that has a paid payment, how many days passed between the signup and the first paid payment? Both dates come in mixed formats: a plain date, a date with a time, or a date with a T and a time." Return the signup id and the number of days as a whole number.

Read first if you are stuck

Other practice sets

Your first 8 exercises are free, with no account. Every answer is checked on the rows you see and again on changed data.