Creating New Columns and Mathematical Operations in PostgreSQL

Introduction and Context Setting

Welcome to this lesson on creating new columns and performing mathematical operations in PostgreSQL. This lesson builds on what you've learned so far about text-based queries and subqueries. We will dive into ways to derive insightful data by applying mathematical operations and creating new columns.

Dataset Review

For convenience, the tables and columns in our Marvel movies dataset are:

Movies Table

text
 movie_id | movie_name | release_date | phase 
----------+------------+--------------+-------

Movie Details Table

text
 movie_id | budget_million_usd | box_office_million_usd | imdb_rating | runtime_minutes 
----------+--------------------+------------------------+-------------+-----------------

Characters Table

text
 character_id | movie_id | character_name | actor | screen_time_minutes 
--------------+----------+----------------+-------+---------------------

Reviewing Column Aliases

The SELECT statement is used to retrieve data from a database. Aliases are used to give columns a temporary name, which makes the output more readable.

We can use an alias to rename the movie_name column "Film Name" as follows:

SQL
SELECT movie_name AS "Film Name" 
FROM movies;

The output is:

text
                  Film Name                  
---------------------------------------------
 Iron Man
 The Incredible Hulk
 Iron Man 2
 Thor
 Captain America: The First Avenger
 The Avengers
 Iron Man 3
 Thor: The Dark World
 Captain America: The Winter Soldier
 Guardians of the Galaxy
 Avengers: Age of Ultron
 Ant-Man
 Captain America: Civil War
 Doctor Strange
 Guardians of the Galaxy Vol. 2
 Spider-Man: Homecoming
 Thor: Ragnarok
 Black Panther
 Avengers: Infinity War
 Ant-Man and The Wasp
 Captain Marvel
 Avengers: Endgame
 Spider-Man: Far From Home
 Black Widow
 Shang-Chi and the Legend of the Ten Rings
 Eternals
 Spider-Man: No Way Home
 Doctor Strange in the Multiverse of Madness
 Thor: Love and Thunder
 Black Panther: Wakanda Forever
 Ant-Man and The Wasp: Quantumania
 Guardians of the Galaxy Vol. 3
 The Marvels
(33 rows)

The output selects all movie names, but changes the column name in the output from "movie_name" to "Film Name".

Creating New Columns with String Concatenation

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal