Write a query to retrieve the first four characters of the name

Description: Show the first four letters of every student name.

Tables: the exercises use the students (id, name, city, marks, course_id) and courses (id, course_name) tables, with the data from our MySQL INSERT chapter. The students are: Robin (Delhi, 85), Maria (Mumbai, 92), John (Pune, 78), Asha (Jaipur, 88), Vikram (Delhi, 65), Neha (Mumbai, 45).

Hint:

  • Use LEFT(name, 4) or SUBSTRING(name, 1, 4).

MySQL query for the first four characters

SELECT name, LEFT(name, 4) AS first_four
FROM students;

Output:

Tutorials Class - Output Window
name first_four
Robin Robi
Maria Mari
John John
Asha Asha
Vikram Vikr
Neha Neha

How it works

  • LEFT(text, n) returns the first n characters.
  • Short names are returned fully.

Read String Functions.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.