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

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

SQL Journey: Blog #14

I am now on what Alex the Analyst would call the advanced SQL queries. First off, the Common Table Expression or CTEs. Based on what I understood in the video, CTEs is similar to a function or class in Python which you can call out. It is just how I felt when I first encountered this expression. Now, in this particular case, I tried to replicate what Alex is doing but also not copying the codes that he is using. I am just simply trying to understand what the expression that was used (which starts with WITH) and then observe how he used the said expression. This was my code. Now this code, resulted in the following error. Msg 8156, Level 16, State 1, Line 84 The column 'EmployeeID' was specified multiple times for 'CTE_Employee'. What does someone do in this day and age if we encounter something that bogs us down? We go to the internet especially to AI tools to aid us out. And, apparently, these tools will really come in handy. It stated that the error occurred because I...