SQL Island is a fun introduction to learning and using SQL. The challenge can solved on this website SQL ISLAND
This is my approach to solving the SQL Island challenge.
QUESTION: It seems there are a few people living in these villages. How can I see a list of all inhabitants?
SELECT *
FROM INHABITANT;
SELECT *
FROM INHABITANT
WHERE job = 'butcher';
SELECT *
FROM INHABITANT
WHERE state = 'friendly';
SELECT *
FROM INHABITANT
WHERE state = 'friendly' AND job = 'weaponsmith';
SELECT *
FROM INHABITANT
WHERE state = 'friendly' AND job LIKE '%smith';
INSERT INTO INHABITANT (name, villageid, gender, job, gold, state)
VALUES ('Stranger', 1, '?', '?', 0, '?');
SELECT personid
FROM INHABITANT
WHERE name = 'Stranger';
NOTE: This returns a personid of 20
SELECT gold
FROM INHABITANT
WHERE personid = 20;
SELECT *
FROM ITEM
WHERE owner ISNULL;
UPDATE ITEM
SET owner = 20
WHERE item = 'coffee cup';
UPDATE ITEM
SET owner = 20
WHERE owner ISNULL;
SELECT *
FROM ITEM
WHERE owner = 20;
SELECT *
FROM INHABITANT
WHERE state = 'friendly'
AND job = 'dealer' OR job = 'merchant';
UPDATE ITEM
SET owner = 15
WHERE item IN ('teapot', 'ring');
UPDATE INHABITANT
SET gold = gold + 120
WHERE personid = 20;
UPDATE INHABITANT
SET name = Ahmed
WHERE name = 'Stranger';
SELECT *
FROM INHABITANT
WHERE job = 'baker'
ORDER BY gold DESC;
Aha, Paul! I know him! Hi, you again! So, Ahmed Ayodele is your name. I saw you want to work as a baker? Okay! You will be paid 1 gold for 100 bread rolls. (8 hours later...) Here, I made ten thousand bread rolls! I quit! This should be enough money to buy a sword. Let's see what happens with my gold balance. Here's your new sword, Ahmod Ayodolo! Now you can go everywhere. My name is Ahmed Ayodele! Thanks anyway!
QUESTION: Is there a pilot on this island by any chance? He could fly me home.SELECT *
FROM INHABITANT
WHERE job = 'pilot';
personid | name | villageid | gender | job | gold | state |
8 | Arthur Tailor | 2 | m | pilot | 490 | kidnapped |
Oh no, his state is 'kidnapped'. Horrible, the pilot is held captive by Dirty Dieter!
QUESTION: I will show you a trick how to find out the name of the village where Dirty Dieter lives.SELECT village.name
FROM VILLAGE, INHABITANT
WHERE village.villageid = inhabitant.villageid
AND inhabitant.name = 'Dirty Dieter';
NOTE: This returns the village name as 'Onionville'.
SELECT inhabitant.name
FROM VILLAGE, INHABITANT
ON village.villageid = inhabitant.villageid
WHERE village.name = 'Onionville'
AND village.chief = inhabitant.personid;
This returns the inhabitant name as Fred Dix
I've got it! I will visit Fred and ask him about Dirty Dieter and the pilot.
QUESTION: Um, how many inhabitants does Onionville have?SELECT COUNT(*)
FROM INHABITANT, VILLAGE
WHERE village.villageid = inhabitant.villageid
AND village.name = 'Onionville';
This returns a total count of 8
QUESTION: Hello Ahmed, the pilot is held captive by Dirty Dieter in his sister's house. Shall I tell you how many women there are in Onionville? Nah, you can figure it out by yourself!SELECT COUNT(*)
FROM VILLAGE, INHABITANT
ON village.villageid = inhabitant.villageid
WHERE village.name = 'Onionville'
AND inhabitant.gender = 'f';
This returns a total count of 1
QUESTION: Oh, only one woman. What's her name?SELECT inhabitant.name
FROM VILLAGE, INHABITANT
ON village.villageid = inhabitant.villageid
WHERE village.name = 'Onionville'
AND inhabitant.gender = 'f';
This returns the name: Dirty Diane
Let's go!
QUESTION: Ahmed, if you hand me over the entire property of our nearby village Cucumbertown, I will release the pilot. I will show you now what this property consists of.SELECT SUM(inhabitant.gold)
FROM INHABITANT, VILLAGE
WHERE village.villageid = inhabitant.villageid
AND village.name = 'Cucumbertown';
SELECT SUM(gold)
FROM INHABITANT
WHERE job = 'baker'
OR job = 'dealer'
OR job = 'merchant';
That's not enough.
QUESTION: Let's have a look at how much average gold people own, depending on their job.SELECT job, SUM(inhabitant.gold), AVG(inhabitant.gold)
FROM inhabitant
GROUP BY job
ORDER BY AVG(inhabitant.gold);
job | SUM(inhabitant.gold) | AVG(inhabitant.gold) |
farmer | 10 | 10 |
? | 70 | 70 |
merchant | 250 | 250 |
blacksmith | 390 | 390 |
weaponsmith | 790 | 395 |
author | 420 | 420 |
pilot | 490 | 490 |
baker | 1750 | 583.333333333333 |
smith | 1250 | 625 |
dealer | 2130 | 710 |
butcher | 11370 | 2842.5 |
SELECT state, avg(gold)
FROM INHABITANT
GROUP BY state;
job | avg(gold) |
? | 70 |
evil | 512.85714285714 |
friendly | 706.363636363636 |
kidnapped | 490 |
DELETE FROM INHABITANT
WHERE name = 'Dirty Dieter';
DELETE FROM INHABITANT
WHERE name = 'Dirty Diane';
UPDATE INHABITANT
SET state = 'friendly'
WHERE job = 'pilot';
Thank's for releasing me, Ahmed! I will fly you home!
QUESTION: I take my sword, some gold and lots of useless items with me as a souvenir. What a big adventure!UPDATE INHABITANT
SET state = 'emigrated'
WHERE personid = 20;