{"id":19963246,"url":"https://github.com/bilbottom/sql-problems","last_synced_at":"2025-03-01T16:44:13.747Z","repository":{"id":236395635,"uuid":"792529726","full_name":"Bilbottom/sql-problems","owner":"Bilbottom","description":"Solutions to SQL problems.","archived":false,"fork":false,"pushed_at":"2025-01-08T07:34:10.000Z","size":169,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-12T08:17:26.792Z","etag":null,"topics":["sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Bilbottom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-26T21:31:36.000Z","updated_at":"2025-01-08T07:34:12.000Z","dependencies_parsed_at":"2024-06-29T09:43:11.022Z","dependency_job_id":"e3a83081-7c84-4e68-8a27-ae0dc00a0370","html_url":"https://github.com/Bilbottom/sql-problems","commit_stats":null,"previous_names":["bilbottom/sql-problems"],"tags_count":0,"template":false,"template_full_name":"Bilbottom/python-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bilbottom%2Fsql-problems","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bilbottom%2Fsql-problems/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bilbottom%2Fsql-problems/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bilbottom%2Fsql-problems/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bilbottom","download_url":"https://codeload.github.com/Bilbottom/sql-problems/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241396754,"owners_count":19956405,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["sql"],"created_at":"2024-11-13T02:15:22.289Z","updated_at":"2025-03-01T16:44:13.719Z","avatar_url":"https://github.com/Bilbottom.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n[![Python](https://img.shields.io/badge/Python-3.11+-blue.svg)](https://www.python.org/downloads/release/python-3110/)\n[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)\n[![GitHub last commit](https://img.shields.io/github/last-commit/Bilbottom/sql-problems)](https://shields.io/badges/git-hub-last-commit)\n\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Bilbottom/sql-problems/main.svg)](https://results.pre-commit.ci/latest/github/Bilbottom/sql-problems/main)\n\n\u003c/div\u003e\n\n---\n\n# SQL Problems\n\nSolutions to SQL problems.\n\nI've only bothered solving the hardest (free) problems, and I'll add some notes on what I thought of the platform below.\n\n\u003e [!WARNING]\n\u003e\n\u003e This repository will contain spoilers for the problems. If you want to solve them yourself, do not look at the solutions.\n\n\u003e [!NOTE]\n\u003e\n\u003e Some notes on terms/phrases I use here:\n\u003e\n\u003e - \"_Recommended solution_\" means a solution provided by the platform itself.\n\u003e - \"_Community solution_\" means a solution provided by the community.\n\n## Pre-requisites\n\nThis project uses Poetry to manage the Python dependencies and Docker to spin up the databases.\n\nTo install these, follow the instructions on their websites:\n\n- https://python-poetry.org/docs/#installation\n- https://www.python.org/downloads/\n- https://docs.docker.com/get-docker/\n\n## Quick start\n\nAfter installing the pre-requisites and cloning this repo, just run Docker's `compose` command.\n\n```bash\npoetry install\ndocker compose --profile build up --detach\ndocker compose down --volumes  # When you're finished\n```\n\nThis will take a little while to run since there's a fair bit of data to chunk through. To build all services, skip the `--profile build` flag; to customise which services to build, adjust the `docker-compose.yaml` file.\n\nYou can connect to the databases using any of your favourite SQL clients.\n\n---\n\n## DataLemur\n\n\u003e [!NOTE]\n\u003e\n\u003e The problems are available at:\n\u003e\n\u003e - https://datalemur.com/\n\u003e\n\u003e The platform uses PostgreSQL only (version 14.11 at the time of writing).\n\u003e\n\u003e There are only 8 free hard problems.\n\n### Pros ✔️\n\nThe problems do get you to think and use some not-so-common logic, which is good!\n\nThe problem statements have sample data and expected results, which is great for testing your solutions -- feels a lot like LeetCode/TDD 😉\n\nThe platform is easy to use, has a fast response time, and the discussion tab has some good alternative community solutions.\n\nThe solutions tab sometimes provides alternative approaches too -- one even showed off a recursive CTE!\n\n### Cons ❌\n\nSome of the recommended solutions could be better:\n\n- There are some database features that the solutions could use to make the queries more concise and readable.\n- The recommended solution for the [updated-status](src/data-lemur/updated-status.sql) problem wraps all the business logic into a case statement. This is bad practice given that the solution could have used a lookup table\u003csup\u003e\\*\u003c/sup\u003e, which is what SQL is all about!\n- I frankly disagree with the approach for one solution ([median-search-freq](src/data-lemur/median-search-freq.sql)).\n\n\u003csup\u003e\\*\u003c/sup\u003eThe DataLemur platform wasn't accepting several variant of the `VALUES` clause even though it was valid SQL. This feels like a bug in the platform.\n\nI'm not sure about the premium hard questions, but based on the free ones, the problems are only moderately difficult -- I have to solve harder problems on a daily basis as an analytics engineer, so I don't think these are great preparation for a senior technical role.\n\n---\n\n## Analyst Builder\n\n\u003e [!NOTE]\n\u003e\n\u003e The problems are available at:\n\u003e\n\u003e - https://www.analystbuilder.com/\n\u003e\n\u003e The platform uses a mix of databases and languages.\n\u003e\n\u003e There are only 3 free hard problems, and 0 free very hard problems.\n\n### Pros ✔️\n\nThe platform looks and feels really nice!\n\nSimilar to DataLemur, the problem statements have sample data and expected results.\n\nSimilar to DataLemur, the platform is easy to use, has a fast response time, and the solutions tab has some good alternative community solutions -- but an added bonus is that Analyst Builder has a video explanation, too!\n\nThe PostgreSQL version that Analyst Builder uses is 16.1, two versions ahead of DataLemur.\n\nCorrect solutions have a nice animation when you submit them 😄\n\nFor the most part, the recommended solutions are good.\n\n### Cons ❌\n\nThe \"hard\" problems are _waaay_ easier than the DataLemur ones; they're more like \"medium\" DataLemur ones.\n\nHopefully the \"very hard\" problems are actually hard, but I can't say for sure.\n\nThese questions are good for someone who's just starting out with SQL, but not for someone who's been working with SQL for a while.\n\n---\n\n## LeetCode\n\n\u003e [!NOTE]\n\u003e\n\u003e The problems are available at:\n\u003e\n\u003e - https://leetcode.com/problemset/database/?difficulty=HARD\n\u003e\n\u003e The platform uses a mix of databases and even Pandas.\n\u003e\n\u003e There are only 3 free hard problems.\n\n### Pros ✔️\n\nIt's LeetCode -- the platform is decent 😜\n\nLike the rest of LeetCode, the test cases are a decent feature: especially since your solution is run against multiple inputs and outputs, encouraging you to write a more general solution rather than one that just works for the given data.\n\nThe questions have a walkthrough to help you understand the problem and the expected output.\n\nThe community shares a good variety of answers.\n\n### Cons ❌\n\nIf you know common query patterns, the \"hard\" questions are easy.\n\nSmall gripe, but I'm never happy when SQL problems want column names to be case-sensitive or have spaces in them, like `Cancellation Rate` (rather than `cancellation_rate`).\n\nSame as DataLemur: these are good for checking that you know what you need to know for a mid-level engineer, but are not great preparation for a senior technical role.\n\n---\n\n## DataExpert.io\n\n\u003e [!NOTE]\n\u003e\n\u003e The problems are available at:\n\u003e\n\u003e - https://dataexpert.io/questions\n\u003e\n\u003e The platform uses Trino only.\n\u003e\n\u003e The platform is still a WIP, so I'll save the pros and cons for later.\n\n---\n\n## SQL Short Reads\n\n\u003e [!NOTE]\n\u003e\n\u003e The problems are available at:\n\u003e\n\u003e - https://sqlshortreads.com/sql-practice-problems/\n\u003e\n\u003e These questions are created by [Chris Perry](https://www.linkedin.com/in/chrismperry/).\n\u003e\n\u003e The site only includes the questions (and solutions), but doesn't provide a live query editor -- instead, it points you to [https://livesql.oracle.com/](https://livesql.oracle.com/) which uses Oracle 19c.\n\n### Pros ✔️\n\nThe site is pretty slick and easy to navigate, with plenty of problems to work on.\n\nI like that the intention is to provide the requirements like a \"real-world\" scenario rather than framed like a technical question.\n\nThe author is clearly a fan of `DENSE_RANK` 😄\n\nThe [member-type-upgrade.sql](src/sql-short-reads/member-type-upgrade.sql) question is a good one, I like when a question has a bit of Maths in it.\n\n### Cons ❌\n\nThe [livesql.oracle.com](https://livesql.oracle.com/) site requires you to sign up to use it, which is a bit annoying -- in particular because it asks for more personal information than I'd like to give (phone number, company name, etc.).\n\nAlso, Oracle 🤮\n\nTo reiterate, Oracle 🤮\n\nThe recommended solutions seem overcomplicated in a few cases and, like Analyst Builder, the \"hard\" questions are mostly not that hard.\n\nI disagree with the solution to [consecutive-top-product-category-performances.sql](src/sql-short-reads/consecutive-top-product-category-performances.sql).\n\n---\n\n## AdvancedSQLPuzzles\n\n\u003e [!NOTE]\n\u003e\n\u003e The problems are available at:\n\u003e\n\u003e - https://advancedsqlpuzzles.com/\n\u003e - https://github.com/smpetersgithub/AdvancedSQLPuzzles/tree/main/Advanced%20SQL%20Puzzles\n\u003e\n\u003e The site is a [GitHub](https://github.com/) repo which only includes the questions (and solutions), but doesn't provide a live query editor.\n\u003e\n\u003e The problems are created by [Scott Peters](https://github.com/smpetersgithub).\n\n### Pros ✔️\n\nThere are _loads_ of problems to work on, which is great!\n\nThe problems aren't just about writing a `SELECT` statement, but also tests DDL and DML knowledge, as well as Maths/stats knowledge.\n\nThere is a lot of repetition in the problems, which is good for reinforcing the patterns and techniques.\n\nMany of these questions _are_ hard, and are good preparation for a senior technical role.\n\n### Cons ❌\n\nThe DDL is restricted to SQL Server, so it's not as general as the other platforms.\n\nThere's a lot of overlap between the DDL and the solutions -- I guess this is for convenience, but it makes the solutions harder to read.\n\nThe problems are written in a PDF.\n\n---\n\n## Advent of SQL\n\n\u003e [!NOTE]\n\u003e\n\u003e The problems are available at:\n\u003e\n\u003e - https://adventofsql.com/\n\u003e\n\u003e Although the questions are based on PostgreSQL, I'll solve them with DuckDB since there is significant overlap between the two (and I prefer DuckDB for stuff like this).\n\n### Pros ✔️\n\nFor the most part, they're phrased like \"real life\" problems.\n\nEach problem had a sample to practise with.\n\nThe author was very receptive to feedback on the [corresponding sub-Reddit community](https://www.reddit.com/r/adventofsql/).\n\nThere was a good variety of topics and data types (JSON, geometry, etc).\n\n### Cons ❌\n\nThe difficulty varies _wildly_, and some of the challenges were way too easy.\n\nSince it was the first year, there were a fair few bugs (as expected) and the samples didn't always correctly reflect the \"real\" data.\n\nWould have been awesome to have a leaderboard 😛\n\n---\n\n## Challenging SQL Problems\n\n\u003e [!TIP]\n\u003e\n\u003e This is my own set of SQL problems 😄\n\n\u003e [!NOTE]\n\u003e\n\u003e The problems are available at:\n\u003e\n\u003e - https://bilbottom.github.io/sql-learning-materials/challenging-sql-problems/challenging-sql-problems/\n\u003e\n\u003e The site only includes the questions (and solutions), but doesn't provide a live query editor.\n\u003e\n\u003e The problems' DDL is written in generic SQL to support multiple databases.\n\u003e\n\u003e I'm not going to add a pros and cons list: I've just included this for completeness.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbilbottom%2Fsql-problems","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbilbottom%2Fsql-problems","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbilbottom%2Fsql-problems/lists"}