Menu
Coddy logo textTech

LENGTH, UPPER, LOWER

Part of the Beyond the Basics section of Coddy's SQL journey — lesson 6 of 27.

Three small functions you'll use everywhere on text columns.

  • LENGTH(s): number of characters in s
  • UPPER(s): same string in upper case
  • LOWER(s): same string in lower case
SELECT name, LENGTH(name) AS chars, UPPER(name) AS upper_name
FROM users

They take a string and return one. That makes them perfect for normalising messy data inside a WHERE clause: WHERE LOWER(email) = 'bob@x.com' matches 'BOB@X.COM' too.

challenge icon

Challenge

Easy

Available tables and columns:

  • <strong>countries</strong>: <strong>code</strong>, <strong>name</strong>

The code column has inconsistent casing. Return only countries whose name is between 5 and 7 characters long (inclusive), with:

  • code: upper-cased
  • name: lower-cased
  • name_length: character count

Order alphabetically by the upper-cased code.

Cheat sheet

Common string functions for text columns:

  • LENGTH(s): number of characters in s
  • UPPER(s): converts string to upper case
  • LOWER(s): converts string to lower case
SELECT name, LENGTH(name) AS chars, UPPER(name) AS upper_name
FROM users

Useful for normalising data in WHERE clauses:

WHERE LOWER(email) = 'bob@x.com'

Try it yourself

SELECT -- cleaned code, lowered name, length
FROM countries
WHERE -- name length between 5 and 7
ORDER BY -- cleaned code
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Beyond the Basics