String functions clean, combine and cut text. The ones used below are LENGTH, CONCAT, UPPER, SUBSTRING, REPLACE, REVERSE and LEFT. These are MySQL names. Other databases use close variants: SQL Server has LEN instead of LENGTH, and Oracle has SUBSTR and the || operator for joining text.
LENGTH(text) CONCAT(a, b, c)
UPPER(text) LOWER(text)
SUBSTRING(text, start, length)
REPLACE(text, old, new) REVERSE(text)
LEFT(text, n) RIGHT(text, n)
These questions use the employees table (30 rows). Create it first with the copy-and-paste setup script. Here is the data, so you can check your answers by eye.
See the employees table
Table: employees
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT |
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
| 1021 | David | Mills | NULL | 1998-01-24 | M | 2019-12-26 | IT |
| 1022 | Sarah | Harrison | 44692 | 1996-03-20 | F | 2020-05-03 | Finance |
| 1023 | Matthew | Hill | 41365 | 1998-05-16 | M | 2019-05-23 | IT |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business Development |
How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.
Question 1Get the details of employees whose first name is 8 or more characters long.
Show the answer and the output
SELECT * FROM employees WHERE LENGTH(first_name) >= 8;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
Question 2Retrieve emp_no, first_name and an email in the form first_name.last_name@demo.com, for employees with emp_no 1010 or below.
Show the answer and the output
SELECT emp_no, first_name,
CONCAT(first_name, '.', last_name, '@demo.com') AS email
FROM employees
WHERE emp_no <= 1010;Output
| emp_no | first_name | |
|---|---|---|
| 1001 | James | James.Bond@demo.com |
| 1002 | Robert | Robert.Lee@demo.com |
| 1003 | Mary | Mary.Wood@demo.com |
| 1004 | John | John.Cole@demo.com |
| 1005 | Jennifer | Jennifer.Gill@demo.com |
| 1006 | Michael | Michael.Watts@demo.com |
| 1007 | David | David.Mills@demo.com |
| 1008 | William | William.Hill@demo.com |
| 1009 | Linda | Linda.Potter@demo.com |
| 1010 | Richard | Richard.Martin@demo.com |
Question 3Retrieve emp_no, first name and last name of employees in uppercase.
Show the answer and the output
SELECT emp_no, UPPER(first_name) AS first_name, UPPER(last_name) AS last_name
FROM employees;Output
| emp_no | first_name | last_name |
|---|---|---|
| 1001 | JAMES | BOND |
| 1002 | ROBERT | LEE |
| 1003 | MARY | WOOD |
| 1004 | JOHN | COLE |
| 1005 | JENNIFER | GILL |
| 1006 | MICHAEL | WATTS |
| 1007 | DAVID | MILLS |
| 1008 | WILLIAM | HILL |
| 1009 | LINDA | POTTER |
| 1010 | RICHARD | MARTIN |
| 1011 | SUSAN | HALL |
| 1012 | JOSEPH | TURNER |
Showing the first 12 of 30 rows.
Question 4Retrieve the month from the birth date of each employee as 'birth_month'.
Show the answer and the output
SELECT emp_no, SUBSTRING(birth_date, 6, 2) AS birth_month FROM employees;Output
| emp_no | birth_month |
|---|---|
| 1001 | 01 |
| 1002 | 09 |
| 1003 | 11 |
| 1004 | 02 |
| 1005 | 06 |
| 1006 | 04 |
| 1007 | 04 |
| 1008 | 10 |
| 1009 | 01 |
| 1010 | 08 |
| 1011 | 12 |
| 1012 | 05 |
Showing the first 12 of 30 rows.
A date written as 1998-01-19 has the month in characters 6 and 7, so SUBSTRING(birth_date, 6, 2) picks it out as text. MONTH(birth_date) returns it as a number.
Different from the PDF: The PDF answer selected only birth_month. emp_no is added so each month can be matched to an employee.
Question 5Display 2023 in place of 2020 in the joining date of each employee, and also retrieve emp_no and first name.
Show the answer and the output
SELECT emp_no, first_name,
REPLACE(joining_date, '2020', '2023') AS new_joining_date
FROM employees;Output
| emp_no | first_name | new_joining_date |
|---|---|---|
| 1001 | James | 2023-11-29 |
| 1002 | Robert | 2019-12-26 |
| 1003 | Mary | 2019-09-12 |
| 1004 | John | 2021-04-14 |
| 1005 | Jennifer | 2019-06-28 |
| 1006 | Michael | 2022-06-09 |
| 1007 | David | 2021-10-17 |
| 1008 | William | 2019-09-30 |
| 1009 | Linda | 2021-08-07 |
| 1010 | Richard | 2021-03-23 |
| 1011 | Susan | 2023-08-07 |
| 1012 | Joseph | 2019-09-02 |
Showing the first 12 of 30 rows.
REPLACE changes every part of the text that matches, so any 2020 in the date text becomes 2023.
Different from the PDF: The question said "birth date", but the answer (and the sample data) uses the joining date, which is where the year 2020 appears. The question now says joining date.
Question 6Retrieve the first name and last name of employees whose last name has 'son' as its last three characters.
Show the answer and the output
SELECT first_name, last_name FROM employees WHERE SUBSTRING(last_name, -3) = 'son';Output
| first_name | last_name |
|---|---|
| Christopher | Gibson |
| Sarah | Harrison |
| Barbara | Anderson |
A negative start position counts from the end of the text, so SUBSTRING(last_name, -3) is the last three characters. RIGHT(last_name, 3) does the same.
Question 7Get the emp_no of the employees and display the department name in reverse order.
Show the answer and the output
SELECT emp_no, REVERSE(department) AS reversed_department FROM employees;Output
| emp_no | reversed_department |
|---|---|
| 1001 | gnitekraM |
| 1002 | TI |
| 1003 | tnempoleveD ssenisuB |
| 1004 | RH |
| 1005 | gnitekraM |
| 1006 | RH |
| 1007 | TI |
| 1008 | tnempoleveD ssenisuB |
| 1009 | ecnaniF |
| 1010 | TI |
| 1011 | ecnaniF |
| 1012 | gnitekraM |
Showing the first 12 of 30 rows.
Question 8Get the details of employees where the length of the first name is greater than the length of the last name.
Show the answer and the output
SELECT * FROM employees WHERE LENGTH(first_name) > LENGTH(last_name);Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1023 | Matthew | Hill | 41365 | 1998-05-16 | M | 2019-05-23 | IT |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development |
Showing the first 12 of 15 rows.
Question 9Retrieve the first 4 characters of the salary of each employee as 'new_salary'.
Show the answer and the output
SELECT emp_no, LEFT(salary, 4) AS new_salary FROM employees;Output
| emp_no | new_salary |
|---|---|
| 1001 | 4530 |
| 1002 | 6941 |
| 1003 | 3951 |
| 1004 | 3626 |
| 1005 | 4894 |
| 1006 | 2380 |
| 1007 | 3940 |
| 1008 | 2639 |
| 1009 | 5523 |
| 1010 | 2560 |
| 1011 | 5415 |
| 1012 | 6055 |
Showing the first 12 of 30 rows.
LEFT treats the number as text. Employees with no salary stay NULL.
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
