Learn SQL
-- tables.string_functions

String Functions in SQL

CONCAT, SUBSTRING, TRIM, LOWER — text without app round-trips

--concept

Compute text near the data: || (standard) or CONCAT(a,b) joins, SUBSTRING(x FROM 2 FOR 3) slices, TRIM/LTRIM/RTRIM strips, LOWER/UPPER normalizes, LENGTH counts, REPLACE/OVERLAY rewrites. Case-insensitive compare wants LOWER(col) + expression index — or a CITEXT/collation column. Functional predicates without matching indexes scan.

--how it works
1

Concat near data

|| standard or CONCAT() — display labels without app trips.

2

Normalize + strip

LOWER/UPPER for compare; TRIM for dirty input; REPLACE canonicalizes.

3

Slice + measure

SUBSTRING/LEFT prefixes; LENGTH validations and buckets.

4

Index the function

LOWER(email) predicates need expression indexes or they scan.

--visualization
LOWER + LENGTH + SUBSTRING per row — LOWER(email) needs expression index
namelowlenpre
Alicealice5Ali
Bobbob3Bob
Cynthiacynthia7Cyn
Stage 1 / 1
Compute near data: concat labels, trim hygiene, prefix buckets — one pass.
--example
query.sql
1SELECT name, LOWER(name) AS low, LENGTH(name) AS len, SUBSTRING(name FROM 1 FOR 3) AS pre FROM employees ORDER BY name LIMIT 3;
Result
namelowlenpre
Alicealice5Ali
Bobbob3Bob
Cynthiacynthia7Cyn

Each row computes three text derivations in one pass. LOWER powers case-insensitive ORDER BY; LENGTH feeds validations; prefix feeds autocomplete buckets.

--variants

Display labels + lowers

Three derivations in one pass.

query.sql
1SELECT name, LOWER(name) AS low, LENGTH(name) AS len FROM employees ORDER BY name LIMIT 2;
Result
namelowlen
Alicealice5
Bobbob3
1LOWER feeds case-insensitive sort.
2LENGTH feeds validation buckets.
3All per-row CPU, no extra trips.

Case-insensitive login (fast)

Functional predicate made sargable.

query.sql
1CREATE INDEX idx_lower_email ON users (LOWER(email));
2SELECT * FROM users WHERE LOWER(email) = LOWER('ADA@X.COM');
Result
emailhow
ada@x.comfound via expression index
1LOWER both sides normalizes.
2Expression index seeks instead of scanning.
3CITEXT/collation columns are the schema-level alternative.
--key facts
|| vs CONCATstandard operator vs portable function
LOWER + indexfunctional predicate needs expression index
TRIM/LENGTHdirty-input hygiene inside the query
--use cases
Search/autocomplete prefixes + login normalization
ETL hygiene: trim, canonicalize, validate lengths
--under the hood
Text functions are per-row CPU; wrapping indexed columns without expression indexes forces scans.
Collation decides equality/sort — query-level COLLATE overrides per statement.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.