Skip to main content

SQL Journey: Blog #9

Challenge: Famous People

Instructions:

In this project, you’re going to make your own table with some small set of “famous people”, then make more tables about things they do and join those to create nice human-readable lists.

For example, here are types of famous people and the questions your data could answer:

  • Movie stars: What movies are they in? Are they married to each other?
  • Singers: What songs did they write? Where are they from?
  • Authors: What books did they write?
  • Fictional characters: How are they related to other characters? What books do they show up in?

Created Tables:

/* Create table about the people and what they do here */

CREATE TABLE famous_people (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT, last_name TEXT);

INSERT INTO famous_people(first_name,last_name) VALUES ("Amber", "Heard");
INSERT INTO famous_people(first_name,last_name) VALUES ("Johnny", "Depp");
INSERT INTO famous_people(first_name,last_name) VALUES ("Angelina", "Jolie");
INSERT INTO famous_people(first_name,last_name) VALUES ("Brad", "Pitt");
INSERT INTO famous_people(first_name,last_name) VALUES ("Jennifer", "Aniston");
INSERT INTO famous_people(first_name,last_name) VALUES ("Daniel", "Radcliffe");
INSERT INTO famous_people(first_name,last_name) VALUES ("Emma", "Watson");
INSERT INTO famous_people(first_name,last_name) VALUES ("Keira", "Knightley");
INSERT INTO famous_people(first_name,last_name) VALUES ("Orlando", "Bloom");
INSERT INTO famous_people(first_name,last_name) VALUES ("John", "Krasinski");
INSERT INTO famous_people(first_name,last_name) VALUES ("Emily", "Blunt");
INSERT INTO famous_people(first_name,last_name) VALUES ("Nancy Ellen", "Walls");
INSERT INTO famous_people(first_name,last_name) VALUES ("Steve", "Carell");
INSERT INTO famous_people(first_name,last_name) VALUES ("Nicole", "Kidman");
INSERT INTO famous_people(first_name,last_name) VALUES ("Tom", "Cruise");

CREATE TABLE marriage_partners (id INTEGER PRIMARY KEY AUTOINCREMENT, person_id1 INTEGER, person_id2 INTEGER);

INSERT INTO marriage_partners (person_id1,person_id2) VALUES (1,2);
INSERT INTO marriage_partners (person_id1,person_id2) VALUES (3,4);
INSERT INTO marriage_partners (person_id1,person_id2) VALUES (10,11);
INSERT INTO marriage_partners (person_id1,person_id2) VALUES (12,13);
INSERT INTO marriage_partners (person_id1,person_id2) VALUES (14,15);

CREATE TABLE movie_partners (id INTEGER PRIMARY KEY AUTOINCREMENT, person_id1 INTEGER, person_id2 INTEGER, movie_title TEXT);

INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (1,2, "The Rum Diary");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (3,4, "Mr. and Mrs. Smith");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (4,5,"Fast Times at Ridgemont High Table Read");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (6,7, "Harry Potter");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (8,9, "Pirates of the Caribbean");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (2,8, "Pirates of the Caribbean");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (2,8, "Berlin I love you");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (2,9, "Pirates of the Caribbean");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (2,3, "The Tourist");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (10,11, "A Quiet Place");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (12,13, "40-year old virgin");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (10,13, "The Office");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (10,4, "The Big Short");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (14,15, "Days of Thunder");
INSERT INTO movie_partners (person_id1,person_id2, movie_title) VALUES (11,15, "Edge of Tomorrow 2");

For Movies made together, here is the code and the corresponding query results.

Code:

SELECT movie_partners.movie_title, a.first_name || ' ' || a.last_name AS actor1, b.first_name || ' ' || b.last_name AS actor2 
    FROM movie_partners
    JOIN famous_people a
    ON movie_partners.person_id1 = a.id
    JOIN famous_people b
    ON movie_partners.person_id2 = b.id;

Query Results:

movie_titleactor1actor2
The Rum DiaryAmber HeardJohnny Depp
Mr. and Mrs. SmithAngelina JolieBrad Pitt
Fast Times at Ridgemont High Table ReadBrad PittJennifer Aniston
Harry PotterDaniel RadcliffeEmma Watson
Pirates of the CaribbeanKeira KnightleyOrlando Bloom
Pirates of the CaribbeanJohnny DeppKeira Knightley
Berlin I love youJohnny DeppKeira Knightley
Pirates of the CaribbeanJohnny DeppOrlando Bloom
The TouristJohnny DeppAngelina Jolie
A Quiet PlaceJohn KrasinskiEmily Blunt
40-year old virginNancy Ellen WallsSteve Carell
The OfficeJohn KrasinskiSteve Carell
The Big ShortJohn KrasinskiBrad Pitt
Days of ThunderNicole KidmanTom Cruise
Edge of Tomorrow 2Emily BluntTom Cruise

For famous people who were married, the code is:

SELECT a.first_name || ' ' || a.last_name AS actor1, b.first_name || ' ' || b.last_name AS actor2 
    FROM marriage_partners
    JOIN famous_people a
    ON marriage_partners.person_id1 = a.id
    JOIN famous_people b
    ON marriage_partners.person_id2 = b.id;

Query results:

actor1actor2
Amber HeardJohnny Depp
Angelina JolieBrad Pitt
John KrasinskiEmily Blunt
Nancy Ellen WallsSteve Carell
Nicole KidmanTom Cruise

Note: In this particular exercise, I utilize the concatenate operator "||" in order to concatenate two separate columns for the first name and the last name.

Comments

Popular posts from this blog

Privacy Policy of ShinStats: descriptives calc

Privacy Policy Shin Nix built the ShinStats app as an Ad Supported app. This SERVICE is provided by Shin Nix at no cost and is intended for use as is. This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service. If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy. The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions, which are accessible at ShinStats unless otherwise defined in this Privacy Policy. Information Collection and Use For a better experience, while using our Service, I may require you to provide us with certain personally identifiable information. The information that I request will be retaine...

Gears Update

It has been a while since my last post and many things have happened since then. For one, I decided to upgrade my laptop as I saw it fit for the direction I am moving towards particularly on data analytics. It's been almost 10 years since I bought "Julian", my first work laptop, and there were so many milestones that we shared together. I bought my first laptop during my second job in Taguig City. It served as an extension of myself as I work to earn for my family particularly in helping my siblings with their education as I am the eldest and breadwinner of the family. That laptop was able to create a joint personnel reporting system excel file which was used by the Philippine Army to be able to account their personnel on a national level during my stint as an Engineer / Researcher in the aforementioned organization. Julian was my laptop when I finished my Master's degree at the Ateneo de Davao University where I also created the Programmable Logic Controller Trainer ...

Coursehero: The Comments that I received from Students all over the world (2022)

What's up Nixers! I was inspired to do a blog today since I was supposed to watch some netflix videos but figured why not pass by here for a while. I recently visited my account and found that my latest answer was tagged as helpful by the student with a custom-made message with a heart. For those of you who do not know, there are three things that you get to receive from students when answering questions in Coursehero.  (1) Unhelpful which means your answer may either be incorrect, was no longer needed by the student (since the submission of the assignment has already lapsed and you were too late to answer the question) or they just don't feel connected to your solution. You see, in this platform, you really cannot please everybody. I find it a hard and bitter pill to swallow. After all, I did my part. I dedicated brain cells to answer the question and yet, it was unhelpful? Worst of all, I won't get paid for that time I spent answering the question. But yeah, one thing tha...