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

 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

 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

 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.

Introduction to Pattern Matching using %

Pattern matching in PostgreSQL is primarily facilitated through the use of the % wildcard within the LIKE and ILIKE operators. The % wildcard matches any sequence of characters, including an empty sequence.

The LIKE operator performs case-sensitive pattern matching while the ILIKE operator performs case-insensitive pattern matching.

Placing % at the end of a text query matches any string that starts with the text placed before %. For example, the pattern A% matches any string that starts with the letter "A" followed by any sequence of characters, including an empty sequence.

Placing % at the start of a text query matches any string that ends with the text placed after %. For example, the pattern %a matches any string that ends with the letter "a".

Finding Characters Starting with "A"

Suppose we want to find characters with names that begin with "A". The query is:

SELECT DISTINCT character_name
FROM characters
WHERE character_name LIKE 'A%';

The output is:

        character_name         
-------------------------------
 Ayo
 Aunt May
 Alexei Shostakov/Red Guardian
 Aldrich Killian
 America Chavez
 Agent Coulson
 Adrian Toomes/Vulture
 Alexander Pierce
(8 rows)

Here, any character name starting with "A" (case-sensitive) followed by any sequence of characters is selected.

Find Characters Ending with "a"

The query to find character names that end with "a" (case-sensitive) preceded by any sequence of characters is:

SELECT DISTINCT character_name
FROM characters
WHERE character_name LIKE '%a';

The output is:

        character_name        
------------------------------
 Ramonda
 Yelena Belova
 Hela
 Nakia
 Raza
 Clea
 Steve Rogers/Captain America
 Nebula
 Thena
 Gamora
 Frigga
 Minn-Erva
 Yondu Udonta
(13 rows)

Any character name ending with "a" will be included, regardless of the preceding sequence of characters.

Exact Matches

In PostgreSQL, you can use the = operator to find exact matches in text-based queries. This operator ensures only entries that exactly match the specified text are returned.

To find exact matches for the character name "Tony Stark", you can use the following query:

SELECT character_name
FROM characters
WHERE character_name = 'Tony Stark';

The output is:

 character_name 
----------------
 Tony Stark
(1 row)

This query returns only the entries where the character_name is exactly "Tony Stark", with no additional characters or variations in case.

Substring Matches

Placing % at the beginning and end of a text finds all entries that contain that substring. The % at the beginning means any sequence of characters (including an empty sequence) can come before the substring, and the % at the ends means any sequence of characters (including an empty sequence) can come after the substring. Let's take a look at an example:

SELECT DISTINCT character_name
FROM characters
WHERE character_name LIKE '%Tony Stark%';

The output of this query is:

   character_name    
---------------------
 Tony Stark
 Tony Stark/Iron Man
(2 rows)

The output indicates that there are two distinct entries in the character_name column that contain "Tony Stark": "Tony Stark" and "Tony Stark/Iron Man".

"Tony Stark/Iron Man" did not appear in the query containing WHERE character_name = 'Tony Stark' because "Tony Stark/Iron Man" is not an exact match.

Exploring Case Sensitivity with ILIKE

PostgreSQL provides two pattern-matching functions: LIKE and ILIKE. LIKE performs case-sensitive pattern matching, while ILIKE performs case-insensitive pattern matching.

Let's revisit the example of finding the characters whose names begin with "A". In the characters table, the first letter of each character's name is capitalized.

SELECT character_name
FROM characters
WHERE character_name LIKE 'a%';

The above query does not return any entries because no character names start with lowercase "a". The output is:

 character_name 
----------------
(0 rows)

We can use ILIKE to find all names that begin with the letter "A"/"a" regardless of case.

SELECT DISTINCT character_name
FROM characters
WHERE character_name ILIKE 'a%';

The output of this query is:

        character_name         
-------------------------------
 Ayo
 Aunt May
 Alexei Shostakov/Red Guardian
 Aldrich Killian
 America Chavez
 Agent Coulson
 Adrian Toomes/Vulture
 Alexander Pierce
(8 rows)

The output shows how ILIKE can be used to query text without regard to case.

Handling Case Sensitivity

Sometimes, you might want to handle case sensitivity explicitly. We can do this using the UPPER and LOWER functions. These functions return their input in all capital letters and all lowercase letters, respectively.

To find names that are fully uppercase, we use the UPPER function as follows:

SELECT character_name 
FROM characters 
WHERE character_name = UPPER(character_name);

UPPER(character_name) returns the character_name in all caps. Using WHERE character_name = UPPER(character_name);, we find all character names that remain unchanged when passed into the UPPER function.

The output is:

 character_name 
----------------
 JARVIS
 MJ
 MJ
(3 rows)

The output shows the names of characters that only contain capital letters.

Similarly, to find names in lowercase, we use the LOWER function.

SELECT actor 
FROM characters
WHERE actor = LOWER(actor);

The output is:

 actor 
-------
(0 rows)

This query returns an empty table, as no actor names are in all lowercase.

Summary and Next Steps

In this lesson, we:

  • Reviewed the Marvel movies database
  • Explored pattern matching using % and =
  • Found exact matches and substring matches in text data
  • Handled case sensitivity using the UPPER and LOWER functions

You are now well-equipped to handle text-based queries in PostgreSQL. The next steps involve practicing these queries through exercises to reinforce your understanding. Happy querying!

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