Concatenate multiple columns in SQL

The following example uses ipython-sql. To install:
pip install ipython-sql

%sql is a Jupyter magic command and can be ignored when not using Jupyter notebooks.

Configure SQL for Jupyter notebook

#Load sql
%load_ext sql
#connect to sqlite
%sql sqlite://
The sql extension is already loaded. To reload it, use: 
%reload_ext sql

Create a table


CREATE TABLE players3 (first_name, last_name, age);
INSERT INTO players3 (first_name, last_name, age) VALUES
('Lebron', 'James', 33),
('Steph', 'Curry', 30),
('Mike', 'Jordan', 55),
('Mike', 'Bibby', 40);

Concatenate multiple columns in SQL


--Here, we will replace the first name with Goat for all Mikes over 45 years of age
--This is an example of modifying a field in a SQL table based on multiple conditions
--in sqlite, this syntax is first_name || last_name
--for MySQL, Postgress, MS Access, syntax is as shown below
concat(first_name, last_name) AS first_last_name
FROM players3
first_name last_name age first_last_name
Lebron James 33 LebronJames
Steph Curry 30 StephCurry
Mike Jordan 55 MikeJordan
Mike Bibby 40 MikeBibby

Ace your next data science interview

Get better at data science interviews by solving a few questions per week

Find a bug? Submit a suggested change on Github, or message me on Twitter.