Help sorting data

Hello,
I have two tables.
One table has 2 fields
categoryid, categoryname.

Table 2 has 3 fields
subcategoryid, subcategoryname, parentid.

Obviously table 2 is holding sub categories for table 1.

Question is how do I query this to show this data in this format.
Is this done on the MySQL side or PHP side? Really confused on this.

What is this called?

category name 1
-----------------sub cat1 (parent is 1)
-----------------sub cat2 (parent is 1)
-----------------sub cat 3 (parent is 1)

category name 2
-----------------sub cat4 (parent is 2)
-----------------sub cat5 (parent is 2)

Thanks!

Did you search the forum for Category and Subcategory to see how many threads there have been on the topic?

Note that the query itself is handled in the database and, depending on the formatting needs, those are handled in your front end application.

So the specific layout you are looking for is handled in PHP but the query would be used to return the rows and their sub categories.

I did do a search but i didnt know the “official term” was category and sub category as the results were not what i was looking for. Ill continue to look. Thanks for your help