Senior
-- Condition -- Find categories where the parent category is the same as the category itself -- Sort the result by id in ascending order -- EXAMPLE -- +----+----------+-----------+ -- | id | name | parent_id | -- +----+----------+-----------+ -- | 1 | pharma | 1 | -- | 2 | products | 4 | -- | 3 | games | 3 | -- | 4 | music | 2 | -- +----+----------+-----------+ -- EXPECTED RESULT -- +--------+ -- | name | -- +--------+ -- | pharma | -- | games | -- +--------+
sobes.tech AI
Answer from AI
To solve the SQL problem, you can use a simple query that selects categories where parent_id equals their own id, and sorts the result by ascending id:
SELECT name
FROM categories
WHERE id = parent_id
ORDER BY id ASC;
If the table is called categories, this query will return all categories where the parent category is themselves, sorted by id.