Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Adding Text Columns Together3:39 with Andrew Chalkley
You are not restricted by the column definitions in the database schema. You can join columns together so they're more human readable as one.
SQLite, PostgreSQL and Oracle
Use the concatenation operator
SELECT <value or column> || <value or column> || <value or column> FROM <table>;
Use the concatenation operator
SELECT <value or column> + <value or column> + <value or column> FROM <table>;
MySQL, Postgres and MS SQL
SELECT CONCAT(<value or column>, <value or column>, <value or column>) FROM <table>;
You're not restricted by the column definitions in the database schema.
Remember in a previous course,
we used the AS keyword to alias the column names to be more human readable.
Take this query for example.
We see that the first name, last name,
email, and phone all have human readable aliases.
Let's run this query.
This generates a report with two columns, the first name and the last name
that could be joined together to save a little bit of visual clutter.
But how do we join these two name columns together?
We can use an operator to join two pieces of text from two columns,
called the concatenation operator.
Concatenate means to link or join things together.
In this case, it's two strings of text.
The concatenation operator in SQL Playgrounds
is two pipes of vertical line characters.
That's how you combine two strings in SQLite.
You can type the pipe character on a US keyboard by holding down the Shift and
In other SQL environments, you may need to use a different set of characters for
the concatenation operator, or sometimes it's even a function.
I'll include details in the teacher's notes.
You can use the concatenation operator to join two text values together
in the following way.
First, you specify the value or column,
the concatenation operator, and then the second value or column.
In fact, you can join any number of values or columns together.
You can even alias the newly joined value to have an appropriate name.
Let's jump back to our customary example and join the first name and last name.
Let's replace the first name alias and the comma here with the concatenate operator.
Let's also change the alias from last name to full name,
since that's what we're doing.
Let's run this.
As you can see, the first name and last names are now joined.
But there's no space between the first name and the last name.
That's because we didn't specify it.
SQL, like other programming languages, require precise instructions.
Computers don't understand our intentions without us explicitly telling them
what we want them to do.
We need to join another piece of text in the middle, the value of the space.
Remember, we need to add quote marks around the space character for
the database to recognize it as a string of text.
When we run it now, it correctly shows the full name with a space in the middle.
Performing concatenation doesn't affect the data in the database,
it just presents it differently.
So you could still add WHERE conditions and
orderings based on the last name, and it should still work as you expect.
Now, you can join the values from columns together and
insert strings of text between them too.
Remember to check the teacher's notes to see the different ways
you can concatenate strings in SQL databases.
You need to sign up for Treehouse in order to download course files.Sign up