SUBSTRING Function in SQL

Level 366 keystrokes

SUBSTRING extracts a portion of a string from a specified position.

SQL code

-- Return each user's name and the start of their email
1SELECT
2 name,
-- SUBSTRING(string, start, length): positions start at 1, not 0
-- Take the first 10 characters of email, e.g. 'alice@exam'
3 SUBSTRING(email, 1, 10) AS prefix
4FROM
5 users
-- Return at most 5 rows; without ORDER BY, which rows come back is not guaranteed
6LIMIT
7 5;

Output

+-------+------------+
| name  | prefix     |
+-------+------------+
| Alice | alice@exam |
| Bob   | bob@exampl |
| Carol | carol@test |
+-------+------------+
3 rows in set
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this SQL code and how often they appear.

  • ,× 3
  • ;× 1
  • (× 1
  • )× 1

More SQL examples at level 3

← SQL typing practice