Bird
Raised Fist0
MySQLquery~10 mins

SUBSTRING and LEFT/RIGHT in MySQL - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract the first 3 characters from the column 'name'.

MySQL
SELECT [1](name, 3) AS first_three FROM users;
Drag options to blanks, or click blank then click option'
ALEFT
BRIGHT
CSUBSTRING
DMID
Attempts:
3 left
💡 Hint
Common Mistakes
Using RIGHT instead of LEFT extracts characters from the end.
Using SUBSTRING without specifying the start position correctly.
2fill in blank
medium

Complete the code to extract 4 characters starting from the 2nd character in 'description'.

MySQL
SELECT [1](description, 2, 4) AS part_desc FROM products;
Drag options to blanks, or click blank then click option'
AMID
BLEFT
CSUBSTRING
DRIGHT
Attempts:
3 left
💡 Hint
Common Mistakes
Using LEFT or RIGHT which do not allow specifying start position.
Confusing the order of parameters.
3fill in blank
hard

Fix the error in the code to get the last 5 characters of 'filename'.

MySQL
SELECT [1](filename, 5) AS last_five FROM files;
Drag options to blanks, or click blank then click option'
ARIGHT
BSUBSTRING
CLEFT
DMID
Attempts:
3 left
💡 Hint
Common Mistakes
Using LEFT extracts from the start, not the end.
Using SUBSTRING without calculating start position for the end.
4fill in blank
hard

Fill both blanks to extract 3 characters starting from the 4th character of 'address'.

MySQL
SELECT [1](address, [2], 3) AS part_address FROM locations;
Drag options to blanks, or click blank then click option'
ASUBSTRING
BLEFT
C4
DRIGHT
Attempts:
3 left
💡 Hint
Common Mistakes
Using LEFT or RIGHT which do not take start position.
Using wrong start position number.
5fill in blank
hard

Fill both blanks to extract the first 2 characters of 'code' in uppercase.

MySQL
SELECT UPPER([1](code, [2])) AS code_start FROM codes;
Drag options to blanks, or click blank then click option'
ALEFT
B2
CSUBSTRING
DRIGHT
Attempts:
3 left
💡 Hint
Common Mistakes
Using RIGHT extracts from end, not start.
Using SUBSTRING without correct parameters.

Practice

(1/5)
1. What does the MySQL function LEFT('Hello World', 5) return?
easy
A. 'World'
B. 'Hello'
C. 'Hello World'
D. 'WorldHello'

Solution

  1. Step 1: Understand LEFT function

    The LEFT function extracts a specified number of characters from the start (left side) of a string.
  2. Step 2: Apply LEFT to 'Hello World'

    Extracting 5 characters from the start of 'Hello World' gives 'Hello'.
  3. Final Answer:

    'Hello' -> Option B
  4. Quick Check:

    LEFT('Hello World', 5) = 'Hello' [OK]
Hint: LEFT gets characters from the start of the string [OK]
Common Mistakes:
  • Confusing LEFT with RIGHT function
  • Expecting the whole string instead of part
  • Using wrong number of characters
2. Which of the following is the correct syntax to extract 3 characters starting from the 2nd character in MySQL?
easy
A. RIGHT('abcdef', 2, 3)
B. LEFT('abcdef', 2, 3)
C. SUBSTRING('abcdef', 2, 3)
D. SUBSTRING('abcdef', 3, 2)

Solution

  1. Step 1: Recall SUBSTRING syntax

    SUBSTRING(string, start_position, length) extracts length characters starting at start_position.
  2. Step 2: Check each option

    SUBSTRING('abcdef', 2, 3) uses SUBSTRING with start=2 and length=3, which is correct. LEFT and RIGHT do not accept three arguments.
  3. Final Answer:

    SUBSTRING('abcdef', 2, 3) -> Option C
  4. Quick Check:

    Correct SUBSTRING syntax = SUBSTRING('abcdef', 2, 3) [OK]
Hint: SUBSTRING needs start and length; LEFT/RIGHT only need length [OK]
Common Mistakes:
  • Using LEFT or RIGHT with three arguments
  • Swapping start and length values
  • Starting count from zero instead of one
3. What is the result of this query?
SELECT SUBSTRING('Database', 5, 3) AS part1, LEFT('Database', 4) AS part2, RIGHT('Database', 2) AS part3;
medium
A. part1='bas', part2='Data', part3='se'
B. part1='base', part2='Data', part3='se'
C. part1='tab', part2='D', part3='se'
D. part1='bas', part2='Datab', part3='se'

Solution

  1. Step 1: Calculate SUBSTRING('Database', 5, 3)

    Starting at position 5: 'D'(1), 'a'(2), 't'(3), 'a'(4), 'b'(5), 'a'(6), 's'(7), 'e'(8). Characters 5 to 7 are 'b', 'a', 's' -> 'bas'.
  2. Step 2: Calculate LEFT('Database', 4) and RIGHT('Database', 2)

    LEFT takes first 4 chars: 'D', 'a', 't', 'a' -> 'Data'. RIGHT takes last 2 chars: 's', 'e' -> 'se'.
  3. Final Answer:

    part1='bas', part2='Data', part3='se' -> Option A
  4. Quick Check:

    SUBSTRING, LEFT, RIGHT results match part1='bas', part2='Data', part3='se' [OK]
Hint: Count characters carefully starting at 1 for SUBSTRING [OK]
Common Mistakes:
  • Counting start position from zero
  • Mixing up LEFT and RIGHT results
  • Extracting wrong substring length
4. Identify the error in this query:
SELECT LEFT('Example', 3, 2) AS result;
medium
A. LEFT function does not accept three arguments
B. String 'Example' is too short
C. Missing FROM clause
D. RIGHT function should be used instead

Solution

  1. Step 1: Check LEFT function syntax

    LEFT(string, length) accepts only two arguments: the string and number of characters from the left.
  2. Step 2: Analyze the query

    The query uses three arguments in LEFT, which is invalid syntax and causes an error.
  3. Final Answer:

    LEFT function does not accept three arguments -> Option A
  4. Quick Check:

    LEFT syntax error = LEFT function does not accept three arguments [OK]
Hint: LEFT only needs string and length, no extra arguments [OK]
Common Mistakes:
  • Adding extra arguments to LEFT or RIGHT
  • Confusing LEFT with SUBSTRING syntax
  • Ignoring syntax errors in function calls
5. You have a column full_name with values like 'John Smith'. Which query extracts the last 5 characters of full_name to get the last name part using MySQL functions?
hard
A. SELECT SUBSTRING(full_name, 6, 5) AS last_name FROM users;
B. SELECT LEFT(full_name, 5) AS last_name FROM users;
C. SELECT SUBSTRING(full_name, -5) AS last_name FROM users;
D. SELECT RIGHT(full_name, 5) AS last_name FROM users;

Solution

  1. Step 1: Understand the requirement

    We want the last 5 characters of the full_name, which is the last name part 'Smith' in 'John Smith'.
  2. Step 2: Choose the correct function

    RIGHT(string, length) extracts characters from the end. RIGHT(full_name, 5) returns last 5 characters.
  3. Step 3: Check other options

    LEFT extracts from start, SUBSTRING with negative start is invalid in MySQL, and SUBSTRING with fixed start 6 may fail if names vary.
  4. Final Answer:

    SELECT RIGHT(full_name, 5) AS last_name FROM users; -> Option D
  5. Quick Check:

    RIGHT gets last characters = SELECT RIGHT(full_name, 5) AS last_name FROM users; [OK]
Hint: Use RIGHT to get characters from the end of a string [OK]
Common Mistakes:
  • Using LEFT instead of RIGHT for last characters
  • Using SUBSTRING with negative start (not supported)
  • Assuming fixed position for last name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard