The Wayback Machine - https://web.archive.org/web/20240530050719/https://www.geeksforgeeks.org/sql-concatenation-operator/
Open In App

SQL | Concatenation Operator

Last Updated : 21 Mar, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Basic Select statement, Insert into clause, SQL Create Clause, SQL Aliases

|| or concatenation operator is use to link columns or character strings. We can also use a literal. A literal is a character, number or date that is included in the SELECT statement.

Let’s demonstrate it through an example:

Syntax:

SELECT id, first_name, last_name, first_name || last_name, 
               salary, first_name || salary FROM myTable
Output (Third and Fifth Columns show  values concatenated by operator ||)
id  first_name  last_name    first_name||last_name  salary   first_name||salary    
1   Rajat        Rawat          RajatRawat          10000    Rajat10000                                                               
2   Geeks        ForGeeks      GeeksForGeeks        20000    Geeks20000                    
3   Shane        Watson         ShaneWatson         50000    Shane50000                           
4   Kedar        Jadhav         KedarJadhav         90000     Kedar90000                           

Note: Here above we have used || which is known as Concatenation operator which is used to link 2 or as many columns as you want in your select query and it is independent of the datatype of column. Here above we have linked 2 columns i.e, first_name+last_name as well as first_name+salary.

We can also use literals in the concatenation operator. Let’s see:

Example 1: Using character literal
Syntax:

SELECT id, first_name, last_name, salary, 
      first_name||' has salary '||salary as "new"  FROM myTable
Output : (Concatenating three values and giving a name 'new')
id  first_name  last_name  salary            new
1   Rajat        Rawat     10000      Rajat has salary 10000
2   Geeks        ForGeeks  20000      Geeks has salary 20000
3   Shane        Watson    50000      Shane has salary 50000
4   Kedar        Jadhav    90000      Kedar has salary 90000

Note: Here above we have used has salary as a character literal in our select statement. Similarly we can use number literal or date literal according to our requirement.

Example 2: Using character as well as number literal
Syntax:

SELECT id, first_name, last_name, salary, first_name||100||' 
                          has id '||id AS "new" FROM myTable
Output (Making readable output by concatenating a string
with values)

id  first_name   last_name     salary     new
1    Rajat       Rawat         10000    Rajat100 has id 1
2    Geeks       ForGeeks      20000    Geeks100 has id 2
3    Shane       Watson        50000    Shane100 has id 3
4    Kedar       Jadhav        90000    Kedar100 has id 4

Here above we have used has salary as a character literal as well as 100 as number literal in our select statement.

References:
1) About Concatenation operator: Oracle Docs
2) Performing SQL Queries Online: Oracle Live SQL

Note: For performing SQL Queries online you must have account on Oracle, if you don’t have then you can make by opening above link.


Previous Article
Next Article

Similar Reads

Concatenation of strings in PL/SQL
Prerequisite - PL/SQL Introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given two strings and the task is to concatenate them and store it in another string. Examples: Input: st
1 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL | Alternative Quote Operator
This post is a continuation of the SQL Concatenation Operator. Now, suppose we want to use apostrophe in our literal value but we can't use it directly. See Incorrect code: SELECT id, first_name, last_name, salary, first_name||' has salary's '||salary AS "new" FROM one So above we are getting error, because Oracle server thinking that the first apo
3 min read
Inequality Operator in SQL
Pre-requisite - Operators in SQL In this article, we will discuss the overview of operators in SQL, and mainly our focus will be on Inequality Operators in SQL. Let's discuss them one by one.SQL Operators are widely used to access information on the basis of conditions. In the Inequality operator, we will check the condition on a specific column. O
3 min read
How to Use the IN Operator with a SubQuery in SQL?
In this article, we will see the use of IN Operator with a SubQuery in SQL. IN operator is used to compare the column values with the list of values. The list of values is defined after IN query in SQL. If we don't know the exact list of values to be compared, we can generate the list of values using the query. Syntax: Without subquery: SELECT colu
2 min read
SQL | BETWEEN & IN Operator
Pre-requisites: SQL Operators Operators are the foundation of any programming language. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands.  In this article, we will see  BETWEEN & IN Operator of SQL.  Between Op
3 min read
SQL IS NOT NULL Operator
In SQL, the IS NOT NULL Operator is a logical operator. Its application is just the opposite of IS NULL Operator. Unlike IS NULL Operator, this operator returns True, if the value is not a NULL value. It is usually used with a WHERE clause to filter the table data. It filters out the data and separates it from records where specified columns have N
5 min read
SQL IN Operator
The SQL IN operator filters data based on a list of specific values. In general, we can only use one condition in the WHEN clause, but the IN operator allows us to specify multiple values. In this article, we will learn about the IN operator in SQL by understanding its syntax and examples. IN OperatorThe IN Operator in SQL is used to specify multip
4 min read
SQL LIKE Operator
SQL LIKE operator is used with the WHERE clause to search for a specified pattern in a column. LIKE operator finds and returns the rows that fit in the given pattern. LIKE operator is case-insensitive by default in most database systems. This means that if you search for "apple" using the LIKE operator, it will return results that include "Apple",
4 min read