Mastering Text-Based Queries in PostgreSQL

Introduction and Overview

Welcome to the first lesson of our course on Advanced Query Techniques and Conditional Logic in PostgreSQL. In this lesson, we will focus on mastering text-based queries.

By the end of this lesson, you will know how to search for patterns in text and handle case sensitivity effectively using PostgreSQL.

Dataset Overview

This course uses the Marvel movies dataset from previous courses. The dataset has a movies table, movie_details table, and a characters table. As a refresher, the tables are:

Movies Table

text
 movie_id |                 movie_name                  | release_date | phase 
----------+---------------------------------------------+--------------+-------
        1 | Iron Man                                    | 2008-05-02   |     1
        2 | The Incredible Hulk                         | 2008-06-13   |     1
        3 | Iron Man 2                                  | 2010-05-07   |     1
        4 | Thor                                        | 2011-05-06   |     1
        5 | Captain America: The First Avenger          | 2011-07-22   |     1
        6 | The Avengers                                | 2012-05-04   |     1
        7 | Iron Man 3                                  | 2013-05-03   |     2
        8 | Thor: The Dark World                        | 2013-11-08   |     2
        9 | Captain America: The Winter Soldier         | 2014-04-04   |     2
       10 | Guardians of the Galaxy                     | 2014-08-01   |     2
       11 | Avengers: Age of Ultron                     | 2015-05-01   |     2
       12 | Ant-Man                                     | 2015-07-17   |     2
       13 | Captain America: Civil War                  | 2016-05-06   |     3
       14 | Doctor Strange                              | 2016-11-04   |     3
       15 | Guardians of the Galaxy Vol. 2              | 2017-05-05   |     3
       16 | Spider-Man: Homecoming                      | 2017-07-07   |     3
       17 | Thor: Ragnarok                              | 2017-11-03   |     3
       18 | Black Panther                               | 2018-02-16   |     3
       19 | Avengers: Infinity War                      | 2018-04-27   |     3
       20 | Ant-Man and The Wasp                        | 2018-07-06   |     3
       21 | Captain Marvel                              | 2019-03-08   |     3
       22 | Avengers: Endgame                           | 2019-04-26   |     3
       23 | Spider-Man: Far From Home                   | 2019-07-02   |     3
       24 | Black Widow                                 | 2021-07-09   |     4
       25 | Shang-Chi and the Legend of the Ten Rings   | 2021-09-03   |     4
       26 | Eternals                                    | 2021-11-05   |     4
       27 | Spider-Man: No Way Home                     | 2021-12-17   |     4
       28 | Doctor Strange in the Multiverse of Madness | 2022-05-06   |     4
       29 | Thor: Love and Thunder                      | 2022-07-08   |     4
       30 | Black Panther: Wakanda Forever              | 2022-11-11   |     4
       31 | Ant-Man and The Wasp: Quantumania           | 2023-02-17   |     5
       32 | Guardians of the Galaxy Vol. 3              | 2023-05-05   |     5
       33 | The Marvels                                 | 2023-11-10   |     5
(33 rows)

The movies table includes the first 33 Marvel movies. Each entry in the table includes a value for movie_id, movie_name, release_date and phase. The movie_id column corresponds with the movie_id columns from the movie_details and characters tables.

Movie Details Table

text
 movie_id | budget_million_usd | box_office_million_usd | imdb_rating | runtime_minutes 
----------+--------------------+------------------------+-------------+-----------------
        1 |                140 |                  585.2 |         7.9 |             126
        2 |                150 |                  263.4 |         6.7 |             112
        3 |                200 |                  623.9 |         7.0 |             124
        4 |                150 |                  449.3 |         7.0 |             115
        5 |                140 |                  370.6 |         6.9 |             124
        6 |                220 |                 1519.6 |         8.0 |             143
        7 |                200 |                 1215.4 |         7.2 |             130
        8 |                170 |                  644.6 |         6.9 |             112
        9 |                170 |                  714.3 |         7.7 |             136
       10 |                170 |                  773.3 |         8.0 |             121
       11 |                250 |                 1405.4 |         7.3 |             141
       12 |                130 |                  519.3 |         7.3 |             117
       13 |                250 |                 1153.3 |         7.8 |             147
       14 |                165 |                  677.7 |         7.5 |             115
       15 |                200 |                  863.8 |         7.6 |             136
       16 |                175 |                  880.2 |         7.4 |             133
       17 |                180 |                  850.8 |         7.9 |             130
       18 |                200 |                 1346.9 |         7.3 |             134
       19 |                321 |                 2048.4 |         8.4 |             149
       20 |                162 |                  622.7 |         7.1 |             118
       21 |                175 |                 1128.3 |         6.9 |             123
       22 |                356 |                 2797.8 |         8.4 |             181
       23 |                160 |                 1131.9 |         7.5 |             129
       24 |                200 |                  378.5 |         6.8 |             134
       25 |                200 |                  430.0 |         7.6 |             132
       26 |                200 |                  402.9 |         6.8 |             157
       27 |                260 |                 1995.4 |         8.4 |             148
       28 |                180 |                 1594.7 |         7.8 |             132
       29 |                200 |                  714.3 |         7.5 |             130
       30 |                250 |                    859 |         7.3 |             161
       31 |                200 |                    476 |         6.2 |             125
       32 |                250 |                    845 |         8.1 |             150
       33 |                250 |                    200 |         6.1 |             124
(33 rows)

The movie_details table includes details for the first 33 Marvel movies. Each entry in the table includes a value for movie_id, budget_million_usd, box_office_million_usd, imdb_rating, and runtime_minutes. The movie_id column corresponds with the movie_id columns from the movies and characters tables.

Characters Table

text
 character_id | movie_id |           character_name            |         actor          | screen_time_minutes 
--------------+----------+-------------------------------------+------------------------+---------------------
            1 |        1 | Tony Stark                          | Robert Downey Jr.      |                 120
            2 |        1 | Pepper Potts                        | Gwyneth Paltrow        |                  40
            3 |        1 | James Rhodes                        | Terrence Howard        |                  30
            4 |        1 | Obadiah Stane                       | Jeff Bridges           |                  25
            5 |        1 | Happy Hogan                         | Jon Favreau            |                  20
            6 |        1 | Agent Coulson                       | Clark Gregg            |                  15
            7 |        1 | Raza                                | Faran Tahir            |                  10
            8 |        1 | Yinsen                              | Shaun Toub             |                  10
            9 |        2 | Bruce Banner/Hulk                   | Edward Norton          |                 110
           10 |        2 | Betty Ross                          | Liv Tyler              |                  35
           11 |        2 | Thaddeus Ross                       | William Hurt           |                  25
           12 |        2 | Emil Blonsky/Abomination            | Tim Roth               |                  20
           13 |        2 | Leonard Samson                      | Ty Burrell             |                  15
           14 |        2 | General Ross                        | William Hurt           |                  15
           15 |        2 | Jack McGee                          | Tim Blake Nelson       |                  10
           16 |        3 | Natasha Romanoff/Black Widow        | Scarlett Johansson     |                 100
           17 |        3 | Nick Fury                           | Samuel L. Jackson      |                  30
           18 |        3 | James Rhodes/War Machine            | Don Cheadle            |                  25
           19 |        3 | Ivan Vanko/Whiplash                 | Mickey Rourke          |                  20
           20 |        3 | Justin Hammer                       | Sam Rockwell           |                  15
           21 |        3 | JARVIS                              | Paul Bettany           |                  10
           22 |        3 | Howard Stark                        | John Slattery          |                  10
......
(243 rows)

The characters table contains 243 entries for characters that appear in the first 33 Marvel movies. Each entry has a character_id, movie_id, character_name, actor, and screen_time_minutes. The movie_id column corresponds with the movie_id column from the movies and movie_details table.

Now that we understand the dataset, let's dive into text-based queries.

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