CodeEraFree roadmap

SQL Practice / String functions

SQL String Functions: practice questions with answers

9 questions with solved queries and real output. Outputs checked on MariaDB 10.11 (MySQL-compatible) on 23 November 2026.

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1002RobertLee694101998-09-02M2019-12-26IT
1003MaryWood395181997-11-07F2019-09-12Business Development
1004JohnCole362641999-02-01M2021-04-14HR
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1007DavidMills394001998-04-26M2021-10-17IT
1008WilliamHill263921999-10-21M2019-09-30Business Development
1009LindaPotter552311998-01-24F2021-08-07Finance
1010RichardMartin256001996-08-07M2021-03-23IT
1011SusanHall541521999-12-27F2020-08-07Finance
1012JosephTurner605561997-05-13M2019-09-02Marketing
1013MaryGill582461999-01-21F2021-04-30Business Development
1014ThomasRyan236761996-10-12M2022-03-02IT
1015CharlesWilliams572262000-01-29M2019-11-05NULL
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1017NancyParker669001996-10-24F2022-01-09Finance
1018BettyBarker352881998-11-13F2019-12-26HR
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1020DanielElliott440121999-01-21M2023-01-08IT
1021DavidMillsNULL1998-01-24M2019-12-26IT
1022SarahHarrison446921996-03-20F2020-05-03Finance
1023MatthewHill413651998-05-16M2019-05-23IT
1024AnthonyMiller567941996-06-30M2019-02-28Business Development
1025MarkWood651201998-10-29M2020-07-30Business Development
1026ElizabethWalker470301998-08-11F2021-06-18NULL
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1028JessicaClark638361998-01-24F2021-05-23IT
1029NancyCole636692000-01-26F2019-12-26Finance
1030JoeBrooks440001999-08-05M2021-03-09Business 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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1005JenniferGill489411996-06-05F2019-06-28Marketing
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1026ElizabethWalker470301998-08-11F2021-06-18NULL

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_nofirst_nameemail
1001JamesJames.Bond@demo.com
1002RobertRobert.Lee@demo.com
1003MaryMary.Wood@demo.com
1004JohnJohn.Cole@demo.com
1005JenniferJennifer.Gill@demo.com
1006MichaelMichael.Watts@demo.com
1007DavidDavid.Mills@demo.com
1008WilliamWilliam.Hill@demo.com
1009LindaLinda.Potter@demo.com
1010RichardRichard.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_nofirst_namelast_name
1001JAMESBOND
1002ROBERTLEE
1003MARYWOOD
1004JOHNCOLE
1005JENNIFERGILL
1006MICHAELWATTS
1007DAVIDMILLS
1008WILLIAMHILL
1009LINDAPOTTER
1010RICHARDMARTIN
1011SUSANHALL
1012JOSEPHTURNER

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_nobirth_month
100101
100209
100311
100402
100506
100604
100704
100810
100901
101008
101112
101205

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_nofirst_namenew_joining_date
1001James2023-11-29
1002Robert2019-12-26
1003Mary2019-09-12
1004John2021-04-14
1005Jennifer2019-06-28
1006Michael2022-06-09
1007David2021-10-17
1008William2019-09-30
1009Linda2021-08-07
1010Richard2021-03-23
1011Susan2023-08-07
1012Joseph2019-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_namelast_name
ChristopherGibson
SarahHarrison
BarbaraAnderson

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_noreversed_department
1001gnitekraM
1002TI
1003tnempoleveD ssenisuB
1004RH
1005gnitekraM
1006RH
1007TI
1008tnempoleveD ssenisuB
1009ecnaniF
1010TI
1011ecnaniF
1012gnitekraM

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1002RobertLee694101998-09-02M2019-12-26IT
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1008WilliamHill263921999-10-21M2019-09-30Business Development
1010RichardMartin256001996-08-07M2021-03-23IT
1011SusanHall541521999-12-27F2020-08-07Finance
1014ThomasRyan236761996-10-12M2022-03-02IT
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1023MatthewHill413651998-05-16M2019-05-23IT
1024AnthonyMiller567941996-06-30M2019-02-28Business 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_nonew_salary
10014530
10026941
10033951
10043626
10054894
10062380
10073940
10082639
10095523
10102560
10115415
10126055

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.