0
0
MySQLquery~10 mins

SUBSTRING and LEFT/RIGHT in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SUBSTRING and LEFT/RIGHT
Start with input string
Choose function: SUBSTRING / LEFT / RIGHT
Determine start position and length
Extract substring accordingly
Return extracted substring
This flow shows how MySQL extracts parts of a string using SUBSTRING, LEFT, or RIGHT functions by specifying start positions and lengths.
Execution Sample
MySQL
SELECT SUBSTRING('Hello World', 7, 5) AS sub,
       LEFT('Hello World', 5) AS left_part,
       RIGHT('Hello World', 5) AS right_part;
Extracts 'World' using SUBSTRING, 'Hello' using LEFT, and 'World' using RIGHT from the string 'Hello World'.
Execution Table
StepFunctionInput StringParametersExtracted SubstringExplanation
1SUBSTRING'Hello World'start=7, length=5'World'Start at 7th char, take 5 chars
2LEFT'Hello World'length=5'Hello'Take first 5 chars from left
3RIGHT'Hello World'length=5'World'Take last 5 chars from right
4ENDAll functions executed, extraction complete
💡 All substring extractions done as per parameters, no more steps.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Input String'Hello World''Hello World''Hello World''Hello World''Hello World'
SUBSTRING ResultNULL'World''World''World''World'
LEFT ResultNULLNULL'Hello''Hello''Hello'
RIGHT ResultNULLNULLNULL'World''World'
Key Moments - 3 Insights
Why does SUBSTRING('Hello World', 7, 5) return 'World'?
Because the 7th character in 'Hello World' is 'W', and taking 5 characters from there gives 'World' as shown in execution_table row 1.
What happens if LEFT('Hello World', 5) is used?
It extracts the first 5 characters from the left, which is 'Hello', as shown in execution_table row 2.
How does RIGHT('Hello World', 5) differ from LEFT?
RIGHT takes characters from the end (right side) of the string, so it returns 'World' as in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what substring does SUBSTRING('Hello World', 7, 5) extract?
A'World'
B'Hello'
C'World!'
D'Hello W'
💡 Hint
Check execution_table row 1 under Extracted Substring column.
At which step does LEFT('Hello World', 5) get executed and what is the result?
AStep 1, 'World'
BStep 2, 'Hello'
CStep 3, 'World'
DStep 2, 'World'
💡 Hint
Refer to execution_table row 2 for LEFT function details.
If we change RIGHT('Hello World', 3), what would be the extracted substring?
A'orld'
B'ld'
C'rld'
D'World'
💡 Hint
RIGHT extracts from the end; 3 characters means last 3 letters of 'Hello World'.
Concept Snapshot
SUBSTRING(str, start, length): Extracts substring from 'start' position for 'length' chars.
LEFT(str, length): Extracts first 'length' chars from left.
RIGHT(str, length): Extracts last 'length' chars from right.
Positions start at 1.
If length exceeds string, returns up to string end.
Useful for slicing strings in MySQL.
Full Transcript
This lesson shows how to extract parts of a string in MySQL using SUBSTRING, LEFT, and RIGHT functions. SUBSTRING takes a string, a start position, and a length to return a substring. LEFT returns the first N characters from the start of the string. RIGHT returns the last N characters from the end. Positions start counting at 1. For example, SUBSTRING('Hello World', 7, 5) returns 'World' because it starts at the 7th character and takes 5 characters. LEFT('Hello World', 5) returns 'Hello', the first 5 characters. RIGHT('Hello World', 5) returns 'World', the last 5 characters. These functions help you get parts of strings easily in SQL queries.