Hi,
You can do it in two sql statements.
The below query will get all your parent categories.
Select id, name from categories where parentid is NULL
Then when the user selects a category you know its Id, so then you just select all the child categories which have a parent category matching the selected category.
Select id, name from categories where parentid = @ParentId