LEFT()

SQL · String Functions

Theory

LEFT(string, n) returns the first n characters of a string. LEFT('Jaipur', 3) returns 'Jai'. If n is larger than the string length, you simply get the whole string; if n is 0 you get an empty string. It is the same as SUBSTRING(string, 1, n), just shorter to read.

LEFT exists in MySQL, SQL Server, Postgres and Snowflake, but not in Oracle or SQLite, where you write SUBSTR(string, 1, n) instead. It is often used to build short codes and prefixes: UPPER(LEFT(city, 3)) || '-' || id turns Jaipur and id 1 into 'JAI-1'. It is also handy for grouping by a prefix, such as the first two digits of a PIN code (region) or the first letter of a name for an alphabetical index. When the data has leading spaces, apply TRIM first, otherwise the spaces count as characters.

Why we use it

Prefixes carry meaning in many codes: the first letters of a city, the first digits of a PIN code or account number, an initial. LEFT gets them in one short call, which keeps queries readable.

When to use it

Use LEFT for fixed-length prefixes, codes and initials. Where it does not exist (SQLite, Oracle), use SUBSTR(string, 1, n). If the prefix length varies (up to a separator), combine SUBSTRING with INSTR/POSITION instead.

How it works

  1. Take the string and n
  2. n > string length? Yes: Return the whole string. No: Take characters 1 to n.
  3. Leading spaces count as characters
  4. Return the prefix

Syntax

LEFT(string, n)

Example

SELECT LEFT(name, 3) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic