Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active July 3, 2021 13:52
Show Gist options
  • Save rpivo/54c64291bc920c6489422767193361fc to your computer and use it in GitHub Desktop.
Save rpivo/54c64291bc920c6489422767193361fc to your computer and use it in GitHub Desktop.
Using the Left Function in MySQL

Using the Left Function in MySQL

The following SQL schema will produce the table below.

Create table If Not Exists Employees (employee_id int, name varchar(30), salary int);
Truncate table Employees;
insert into Employees (employee_id, name, salary) values ('2', 'Meir', '3000');
insert into Employees (employee_id, name, salary) values ('3', 'Michael', '3800');
insert into Employees (employee_id, name, salary) values ('7', 'Addilyn', '7400');
insert into Employees (employee_id, name, salary) values ('8', 'Juan', '6100');
insert into Employees (employee_id, name, salary) values ('9', 'Kannon', '7700');
employee_id name salary
2 Meir 3000
3 Michael 3800
7 Addilyn 7400
8 Juan 6100
9 Kannon 7700

Let's say we want to select the name column where the name begins with the letter "M".

We can do this by using the LEFT() function inside of a WHERE clause.

SELECT name
FROM Employees
WHERE LEFT(name, 1) = "M";
name
Meir
Michael
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment