<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<game format-version="2" db-system="sqlite" db-system-min-version="3.37.0">
    <head>
        <title>SQL Supermarket</title>
        <teaser>Ready to explore the supermarket and solve some exciting challenges?</teaser>
        <copyright>© Navneet</copyright>
    </head>
    <scenes>
        <text-scene>
            <text>Hi, I'm Max. It's great to see you! Ready to explore the supermarket together and solve some exciting challenges?</text>
        </text-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max needs to go shopping today and is looking for fresh fruit. Find all products in the "Fruit" category. Return only the product names.</text>
            <sql-solution>SELECT ProductName
FROM Products
WHERE Category = 'Fruit'</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max wants to find the total price of all dairy products. Can you help him identify all products in the "Dairy" category and add up their prices? Round the result to two decimal places.</text>
            <sql-solution>SELECT ROUND(SUM(Price), 2) AS Total_Dairy_Price
FROM Products
WHERE Category = 'Dairy'</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max wants to find the average price of all products. Can you help him? Round the result to two decimal places.</text>
            <sql-solution>SELECT ROUND(AVG(Price), 2) AS Average_Price
FROM Products</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max wants to find the names and ages of all female customers at the supermarket. Return the `Name` and `Age` columns from the `Customers` table and filter by `Gender = 'female'`.</text>
            <sql-solution>SELECT Name, Age
FROM Customers
WHERE Gender = 'female'</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max wants to know which product is the most expensive in the supermarket. Can you help him find it? Return the product name and price.</text>
            <sql-solution>SELECT ProductName, Price
FROM Products
ORDER BY Price DESC
LIMIT 1</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max wants to know how many bananas have been sold in total. Find the number sold using the quantities in `Transactions`. The product names are stored in `Products`; the two tables are linked by `Product_ID`. Return only the total quantity, or 0 if no bananas have been sold.</text>
            <sql-solution>SELECT COALESCE(SUM(Transactions.Quantity), 0) AS Bananas_Sold
FROM Transactions
JOIN Products ON Transactions.Product_ID = Products.Product_ID
WHERE Products.ProductName = 'Bananas'</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
                <text-hint>Use `JOIN` to link tables. `JOIN Products ON Transactions.Product_ID = Products.Product_ID` matches each transaction to its product. Then use `WHERE` to filter by product name.</text-hint>
                <text-hint>`SUM(Transactions.Quantity)` adds up the quantities sold. `COUNT(*)` would count the transactions instead.</text-hint>
                <text-hint>If there are no matching transactions, `SUM` returns `NULL`. `COALESCE(SUM(Transactions.Quantity), 0)` replaces that value with 0.</text-hint>
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Lisa has been shopping. Max wants to see what she bought. For each of Lisa's transactions, return the product name and the quantity bought in that transaction. Keep separate transactions for the same product as separate rows.</text>
            <sql-solution>SELECT Products.ProductName, Transactions.Quantity
FROM Transactions
JOIN Products ON Transactions.Product_ID = Products.Product_ID
JOIN Customers ON Transactions.Customer_ID = Customers.Customer_ID
WHERE Customers.Name = 'Lisa'</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
                <text-hint>Join `Transactions` to `Products` using `Product_ID` and to `Customers` using `Customer_ID`. This makes the product name, quantity and customer name available in the same query.</text-hint>
                <text-hint>The second join is `JOIN Customers ON Transactions.Customer_ID = Customers.Customer_ID`. Filter with `WHERE Customers.Name = 'Lisa'` and select `Products.ProductName` and `Transactions.Quantity`.</text-hint>
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max wants to find the number of transactions per day at the supermarket. For each date that appears in `Transactions`, return one row with the date and the number of transactions.</text>
            <sql-solution>SELECT Date, COUNT(*) AS Transaction_Count
FROM Transactions
GROUP BY Date</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
                <text-hint>`GROUP BY Date` puts transactions with the same date into one group. Each group produces one result row.</text-hint>
                <text-hint>Select `Date` and `COUNT(*)`. `COUNT(*)` counts the transactions within each group; `SUM(Quantity)` would add up the quantities sold instead.</text-hint>
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max wants to know how many units Lisa bought in total. Add up the quantities from all her transactions and return only the total quantity. You need the sum of the quantities, not the number of distinct products.</text>
            <sql-solution>SELECT SUM(Transactions.Quantity) AS Total_Quantity
FROM Transactions
JOIN Customers ON Transactions.Customer_ID = Customers.Customer_ID
WHERE Customers.Name = 'Lisa'</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
                <text-hint>Join `Transactions` and `Customers` using `Customer_ID` and filter by `Customers.Name = 'Lisa'`, just as you did when listing her purchases.</text-hint>
                <text-hint>`SUM(Transactions.Quantity)` returns the total quantity bought. `COUNT(*)` counts transactions, while `COUNT(DISTINCT Product_ID)` counts distinct products.</text-hint>
            </hints>
        </select-scene>
        <select-scene is-row-order-relevant="false" is-col-order-relevant="false" are-col-names-relevant="false">
            <text>Max wants to know the date of Lisa's most recent purchase. Can you help him?</text>
            <sql-solution>SELECT MAX(Date) AS Latest_Purchase_Date
FROM Transactions
WHERE Customer_ID = (SELECT Customer_ID FROM Customers
WHERE Name = 'Lisa')</sql-solution>
            <sql-placeholder></sql-placeholder>
            <hints>
                <expected-result-hint />
            </hints>
        </select-scene>
        <text-scene>
            <text>Congratulations, you've solved all ten tasks in SQL Supermarket! You've filtered products, calculated prices and quantities, linked purchase data with `JOIN` and grouped transactions by date. Thanks for playing, and see you on your next shopping adventure!</text>
        </text-scene>
    </scenes>
    <initial-sql-script>BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "Products" (
    "Product_ID" INTEGER,
    "ProductName" TEXT,
    "Category" TEXT,
    "Price" REAL
);
CREATE TABLE IF NOT EXISTS "Customers" (
    "Customer_ID" INTEGER,
    "Name" TEXT,
    "Age" INTEGER,
    "Gender" TEXT
);
CREATE TABLE IF NOT EXISTS "Transactions" (
    "Transaction_ID" INTEGER,
    "Customer_ID" INTEGER,
    "Product_ID" INTEGER,
    "Quantity" INTEGER,
    "Date" TEXT
);
INSERT INTO "Products" VALUES (1,'Apples','Fruit',2.50);
INSERT INTO "Products" VALUES (2,'Bananas','Fruit',1.80);
INSERT INTO "Products" VALUES (3,'Milk','Dairy',1.20);
INSERT INTO "Products" VALUES (4,'Bread','Bakery',2.00);
INSERT INTO "Products" VALUES (5,'Tomatoes','Vegetables',3.00);
INSERT INTO "Products" VALUES (6,'Yogurt','Dairy',0.90);
INSERT INTO "Products" VALUES (7,'Potatoes','Vegetables',1.50);
INSERT INTO "Products" VALUES (8,'Chicken breast','Meat',5.50);
INSERT INTO "Products" VALUES (9,'Orange juice','Drinks',2.20);
INSERT INTO "Products" VALUES (10,'Chocolate','Sweets',1.80);
INSERT INTO "Customers" VALUES (1,'Lisa',32,'female');
INSERT INTO "Customers" VALUES (2,'Max',45,'male');
INSERT INTO "Customers" VALUES (3,'Anna',28,'female');
INSERT INTO "Customers" VALUES (4,'Tim',40,'male');
INSERT INTO "Customers" VALUES (5,'Sarah',36,'female');
INSERT INTO "Customers" VALUES (6,'Tom',25,'male');
INSERT INTO "Customers" VALUES (7,'Luca',36,'male');
INSERT INTO "Transactions" VALUES (1,1,1,2,'2024-04-27');
INSERT INTO "Transactions" VALUES (2,1,3,1,'2024-04-27');
INSERT INTO "Transactions" VALUES (3,2,5,1,'2024-04-27');
INSERT INTO "Transactions" VALUES (4,3,6,3,'2024-04-28');
INSERT INTO "Transactions" VALUES (5,4,8,2,'2024-04-28');
INSERT INTO "Transactions" VALUES (6,5,10,1,'2024-04-29');
INSERT INTO "Transactions" VALUES (7,1,4,3,'2024-04-30');
INSERT INTO "Transactions" VALUES (8,6,7,4,'2024-05-01');
INSERT INTO "Transactions" VALUES (9,7,9,2,'2024-04-29');
COMMIT;</initial-sql-script>
</game>
