Module 5 · Advanced queries

Transform text with functions

By the end you will be able to change the presentation of a text or count its characters.

About 10 min

0 of 52 lessons

Before you start, you need

Create categories with CASE

Transform or measure a text

A report may need categories in upper case or names in lower case. A function takes a value and returns another. For example, UPPER(category) takes a product's category and returns its upper-case version.

UPPER converts to upper case and LOWER to lower case. LENGTH counts characters: LENGTH('SQL') returns 3. Write the value or the column in parentheses after the name of the function.

When you apply a function to a column, the calculation is done for each row. You can keep the product name and show its transformed category next to it.

The function in SELECT does not change the stored text. Use an alias to explain the new column, such as AS label. Joining text with || has its own lesson straight after this one, and dates are covered in the one after that. A word of warning: in SQLite, UPPER and LOWER only the unaccented letters change, so Martín becomes MARTíN. It is not your mistake.

Minimal syntax
SELECT LOWER(name) AS name_lowercase
FROM employees;

See it in action

We want to show the categories in upper case.

SQL
SELECT name, UPPER(category) AS label
FROM products;
Example result: 8 of 9 rows
namelabel
MouseACCESSORIES
KeyboardACCESSORIES
Monitor 24DISPLAYS
Pro LaptopCOMPUTERS
HeadphonesAUDIO
Monitor 27DISPLAYS
SSD DriveSTORAGE
Cable USB-CACCESSORIES
  1. UPPER takes each product's category and returns it in upper case.
  2. AS label gives the calculated value a heading. The stored name and category stay as they are.
  3. LOWER does the reverse presentation change and LENGTH measures characters. The challenge combines both decisions without changing the data.
  4. For accents and other characters, check the result on the engine you are using; do not assume every engine converts them the same way.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Show names in lower case

Not started

Write a query that returns each employee's name in lower case as name_lowercase, together with their joined_at.

Expected result: 10 rows with the columns name_lowercase, joined_at, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Build a label

Not started

Show UPPER(category) as label and LENGTH(category) as characters for each product. Keep every row.

Expected result: 9 rows with the columns label, characters, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Key takeaways

  • LOWER and UPPER change how text is presented.
  • LENGTH counts characters.
  • Functions in SELECT leave the source unchanged.

Next: Join text values into a readable column

Go deeper

In standard SQLite, UPPER and LOWER convert ASCII letters and may leave accented letters unchanged. LENGTH counts characters, not bytes. The lab is a teaching tool: check formats and characters before moving a transformation to another engine.