0
0
SQLquery~5 mins

String quoting and concatenation differences in SQL

Choose your learning style9 modes available
Introduction
We use quotes to tell the database where a text starts and ends. Concatenation joins pieces of text together to make one longer text.
When you want to store or search for words or sentences in a database.
When you need to combine first and last names into a full name.
When you want to add a greeting before a name, like 'Hello, John'.
When you want to write a query that checks for exact text values.
When you want to build a message by joining several text parts.
Syntax
SQL
SELECT 'text' AS example_text;
-- Concatenate strings using || or CONCAT function
SELECT 'Hello' || ' ' || 'World' AS greeting;
-- Or using CONCAT function
SELECT CONCAT('Hello', ' ', 'World') AS greeting;
Use single quotes (' ') to write text values in SQL.
Double quotes (") are usually for column or table names, not text.
Concatenation operator (||) joins strings in many SQL databases like PostgreSQL.
Some databases like MySQL use CONCAT() function for joining strings.
Examples
This selects the text 'apple' as a column named fruit.
SQL
SELECT 'apple' AS fruit;
This joins three strings with spaces to say 'Hello World'.
SQL
SELECT 'Hello' || ' ' || 'World' AS greeting;
This uses CONCAT function to join strings into 'Good Morning'.
SQL
SELECT CONCAT('Good', ' ', 'Morning') AS greeting;
Double quotes are used to refer to column or table names exactly.
SQL
SELECT "column_name" FROM table_name;
Sample Program
This query shows how to write text, and how to join first and last names using two methods.
SQL
SELECT 'John' AS first_name, 'Doe' AS last_name,
       'Hello, ' || 'John' || ' ' || 'Doe' AS greeting,
       CONCAT('Hello, ', 'John', ' ', 'Doe') AS greeting_concat;
OutputSuccess
Important Notes
Always use single quotes for text values to avoid errors.
Concatenation syntax can differ between SQL databases; check your database's rules.
Double quotes are for identifiers like column names, not for text strings.
Summary
Use single quotes to write text in SQL.
Join text using || operator or CONCAT() function depending on your database.
Double quotes are for column or table names, not for text.