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...

Power BI Journey: Blog #6

In this lesson, the focus is on "Conditional Formatting" which is very much similar to the conditional formatting in MS Excel which I could relate to following the making of the joint reporting system for personnel during my second job. Basically, we click the data columns that we want to be displayed in a tabular visualization.  Next, we select the columns that we will be applying conditional formatting to > Right-click on that column > Select Conditional Formatting > Select from among the options which is more appropriate for your application In this particular exercise, we utilized Background conditional formatting using gradient (applied on the first column of the first table) and rules (IF ELSE which was applied on the second column of the first table), Icons conditional formatting (applied on the second column of the first table), Data bars conditional formatting (applied on the second column of the first table and the fourth column of the second table). In the...

SQL Journey: Blog#10

So far, we are only "reading" from a given database or table using the SELECT command of SQL. In today's lesson, we will now start "writing" into a given database using the UPDATE and DELETE commands. Challenge: Dynamic Documents Given data: CREATE table documents (     id INTEGER PRIMARY KEY AUTOINCREMENT,     title TEXT,     content TEXT,     author TEXT);      INSERT INTO documents (author, title, content)     VALUES ("Puff T.M. Dragon", "Fancy Stuff", "Ceiling wax, dragon wings, etc."); INSERT INTO documents (author, title, content)     VALUES ("Puff T.M. Dragon", "Living Things", "They're located in the left ear, you know."); INSERT INTO documents (author, title, content)     VALUES ("Jackie Paper", "Pirate Recipes", "Cherry pie, apple pie, blueberry pie."); INSERT INTO documents (author, title, content)     VALUES ("Jackie Paper", "Boat Supplies...