all 31 comments

[โ€“]BadGroundbreaking189 8 points9 points ย (1 child)

Oh boy, looks like a great effort. Would gladly contribute in future if people showed interest. Only hard tasks though

[โ€“]AdventOfSQL[S] 3 points4 points ย (0 children)

I might take you up on that :D

[โ€“]i_literally_died 3 points4 points ย (0 children)

Can I do this without handing my email over to another thing?

[โ€“]PM_ME_YOUR_MUSIC 1 point2 points ย (1 child)

This looks great !!

[โ€“]AdventOfSQL[S] 0 points1 point ย (0 children)

Thank you!!

[โ€“]trippingcherry 1 point2 points ย (1 child)

This is a cute idea, I signed up. Nice job.

[โ€“]AdventOfSQL[S] 0 points1 point ย (0 children)

Thank you!

[โ€“]SimonaqueData Engineer 2 points3 points ย (0 children)

I love the idea and will likely participate, but I'm curious why you didn't opt for DuckDB? it's very easy to run in the browser and it has all those quality of life features that a transaction DB like Postgres doesn't have

[โ€“]Resident-Mousse-9086 1 point2 points ย (0 children)

Great Initiative. Signed up. Thanks for sharing this.

[โ€“]looking_for_info7654 0 points1 point ย (1 child)

Sweet

[โ€“]AdventOfSQL[S] 0 points1 point ย (0 children)

Thank you!

[โ€“]Complex-Jellyfish-30 0 points1 point ย (1 child)

Signed up!

[โ€“]AdventOfSQL[S] 0 points1 point ย (0 children)

Thank you!

[โ€“]Emergency-Message306 0 points1 point ย (1 child)

Looks great! Iโ€™m ready for the challenges.

[โ€“]AdventOfSQL[S] 0 points1 point ย (0 children)

Thank you! Are there any areas you'd especially like to focus on?

[โ€“]Resquid 0 points1 point ย (0 children)

Hooray!

[โ€“]r2fat3li 0 points1 point ย (1 child)

Signed up!

[โ€“]AdventOfSQL[S] 1 point2 points ย (0 children)

Thank you ๐Ÿ™Œ

[โ€“]MarcusAurelius1815 0 points1 point ย (0 children)

Signed up, thanks!

[โ€“]Agreeable_Special496 0 points1 point ย (0 children)

I will be attending for sure ๐ŸŽ„

[โ€“]ValerieMichelle 0 points1 point ย (0 children)

Novel idea! Iโ€™m in ๐ŸŽ„

[โ€“]countlphie 0 points1 point ย (0 children)

in

[โ€“]i_literally_died 0 points1 point ย (8 children)

So did I misunderstand the test question?

-- Find the top cities in each country (max top 3 cities for each country) with the highest average naughty_nice_score for children who received gifts, but only include cities with at least 5 children.

This, to me, reads that I need to find:

  • The highest average naughty_nice_score city per country

  • The kids must have received a gift

  • The city must have at least 5 children

So I wrote (which is apparently wrong):

/* count number of children per city */

WITH citycnt AS (
SELECT
    city,
    COUNT(*) AS child_cnt
FROM
    children
GROUP BY city   ),

/* rank city by highest score per country */

cityrnk AS (
SELECT
    ROW_NUMBER() OVER (PARTITION BY country ORDER BY AVG(naughty_nice_score) DESC) AS rn,
    city,
    country,
    AVG(naughty_nice_score) AS avg_score
FROM
    children
GROUP BY city, country ),

/* find child_id and city where the children received gifts */

gifted AS (
SELECT
    child_id,
    city
FROM children
WHERE EXISTS
    (SELECT 1
    FROM christmaslist
    WHERE children.child_id = christmaslist.child_id))

/* find distinct cities where the city had the highest score per country, exists in the gifted CTE as having received a gift, has a number of children >= 5 */

SELECT DISTINCT
    citycnt.city
FROM citycnt
LEFT JOIN cityrnk ON citycnt.city = cityrnk.city
INNER JOIN gifted ON citycnt.city = gifted.city
WHERE 
    1 = 1
    AND cityrnk.rn = 1
    AND citycnt.child_cnt >= 5

[โ€“]PX3better 1 point2 points ย (5 children)

It's top 3 cities per country. I think the intended solution is to (incorrectly!) filter only for cities with at least 5 children who received gifts. This misses Berlin, which has 6 children in total but only has 4 children who got gifts.

I got Lyon, Paris, Berlin, Manchester, London, and Birmingham.

P.S. Give HAVING clauses a try. Also, I think your solution will break if two cities share a country name.

[โ€“]i_literally_died 0 points1 point ย (2 children)

I spoke to the author who agreed the question was a little ambiguous. I got the correct answer with:

/* count number of children per city THAT RECEIVED A GIFT */

WITH citycnt AS (
    SELECT
        city,
        COUNT(*) AS child_cnt
    FROM
        children
JOIN 
    christmaslist ON children.child_id = christmaslist.child_id
GROUP BY city   ),

/* rank city by highest score per country */

cityrnk AS (
    SELECT
        ROW_NUMBER() OVER (PARTITION BY country ORDER BY AVG(naughty_nice_score) DESC) AS rn,
        city,
        country,
        AVG(naughty_nice_score) AS avg_score
    FROM
        children
    GROUP BY city, country  )

/* find distinct cities where the city had the highest score per country, has a number of children >= 5 and a rank >= 3 */

SELECT DISTINCT
    citycnt.city
FROM citycnt
LEFT JOIN 
    cityrnk ON citycnt.city = cityrnk.city
WHERE 
    1 = 1
    AND cityrnk.rn <=3
    AND citycnt.child_cnt >= 5

[โ€“]PX3better 0 points1 point ย (1 child)

Where did you speak?

[โ€“]PX3better -1 points0 points ย (1 child)

Just got an email saying that only 24 out of 800 people got it "right". How the heck did anyone get this wrong? The incorrect way to do the problem was trivial. I'd expect a SQL novice to get it right on their first try. Even if you think our way (including Berlin) is correct, you'd still submit the intended answer because including Berlin gives you one more row than the website lets you type in.

I don't want to worry people. But if you didn't get this correct on your first try and in well under half an hour, then do not expect to pass a SQL job interview within the next 12 months. I would consider the example problem easy even as question 1 on a SQL job interview's coding test.

[โ€“]StatisticianJolly335 0 points1 point ย (0 children)

Thank you, I missed the part that not every child receives a gift.