CONCAT()

SQL · String Functions

Theory

CONCAT joins two or more strings into one, in the order given: CONCAT(first_name, ' ', last_name) turns 'Asha' and 'Rao' into 'Asha Rao'. Numbers are converted to text automatically in most databases, so CONCAT('ORD-', 101) gives 'ORD-101'. The SQL-standard way to join strings is the || operator, which Postgres, Oracle and SQLite support; SQL Server uses + (and also has CONCAT), and MySQL has CONCAT (there || means OR unless a mode is changed).

The big difference between dialects is NULL handling. With ||, any NULL makes the whole result NULL: 'Meera' || ' ' || NULL is NULL. Postgres and SQL Server CONCAT treat NULL as an empty string, but MySQL CONCAT returns NULL if any argument is NULL. Oracle is the odd one out, treating NULL as empty even with ||. The safe, portable habit is to wrap columns that may be NULL in COALESCE(col, '').

Why we use it

Data is often stored in parts (first and last name, house number and street, code prefix and number) but needs to be shown or matched as one string. CONCAT builds full names, labels, keys and messages directly in the query.

When to use it

Use CONCAT or || whenever you build display text or composite keys. When several parts share the same separator, CONCAT_WS is cleaner. Always think about NULL parts: wrap them in COALESCE or the result may silently become NULL.

How it works

  1. Take the list of string arguments
  2. Convert non-text values (numbers, dates) to text
  3. Is any argument NULL? Yes: Result NULL, or NULL treated as ''. No: Use the value as is.
  4. Append each piece in order
  5. Return the joined string

Syntax

CONCAT(str1, str2, ...)

Example

SELECT CONCAT(first_name, ' ', last_name) AS full_name 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