Want to level up your SQL skills? Our SQL Fundamentals Course covers everything from basics to advanced queries.
🔥 Join the Udemy Course today and start learning!
📌 Click here to enroll now!
You are working for an e-commerce platform that organizes products into categories and subcategories. Each category can have a parent category, but some categories are at the top level (root categories). Your task is to find all root categories (categories with no parent).
Write an SQL query to fetch all root categories from the category
table. A root category is defined as one where parent_id IS NULL
.
- Select the following columns:
id
(Category ID)name
(Category Name)slug
(Category Slug)is_active
(Category Active Status)
- Only include categories where
parent_id IS NULL
. - Order the results by
name
in ascending order.
Here’s an example of the category
table (The actual data can be found in init.sql
):
id | name | slug | parent_id | is_active | level |
---|---|---|---|---|---|
1 | Electronics | electronics | NULL | TRUE | 0 |
2 | Laptops | laptops | 1 | TRUE | 1 |
3 | Phones | phones | 1 | TRUE | 1 |
4 | Clothing | clothing | NULL | FALSE | 0 |
You can view the database ERD here:
Write a query to return only the root categories from this table.
- 📌 Read the Setup Instructions to get everything up and running.
- 📝 Run your query.
- 💡 Need help? Check out the
solutions.sql
file.
Watch the first Challenge if you need more help getting the code started. 🔗 Link to first Challenge 001
You only need to set up the database once for all challenges in Scenario A (see challenge name). Each scenario uses slightly different data, tailored to match the specific challenge requirements.