SQL LEFT

A shortcut for grabbing the first N characters — the same result SUBSTRING(text FROM 1 FOR n) would give, with simpler syntax.

What & Why

LEFT(text, n) returns the first n characters. It's a convenience function for exactly one common slicing pattern — always starting from position 1.

See How It Works

BUSINESS QUESTION

Marketing wants short prefixes and suffixes for campaign-name quality review.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  id AS campaign_id,
  name,
  LEFT(name, 4) AS name_prefix,
  RIGHT(name, 4) AS name_suffix
FROM marketing.campaigns
ORDER BY campaign_id;
RESULT — exact output from the displayed Queryflo rows
campaign_idnamename_prefixname_suffix
1Spring LaunchSpriunch
2Retention WebinarReteinar
3Finance RetargetingFinating
4Enterprise SearchEntearch

LEFT and RIGHT extract four characters from the real campaign names.

Now You Try

Practice this concept

Marketing wants four-character prefixes and suffixes for every campaign name.

Available schema
marketing

Prefix tables with marketing.table_name.

campaign_idnamename_prefixname_suffix
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario