Skip to content
Advertisement

Counting and Joining in SQL (oracle)

My database contains 2 tables called products and product_categories.

Product_categories contain category_id and category_name Products contain category_id and other irrelevant info for this question.

I need to count the number of products using category_id from the products table and also display their name which is in the product_categories table.

displays :

And

displays (condensed):

Current statement :

Output:

DESIRED RESULT: I need to display the category id followed by category name and finally the total number of products.

Advertisement

Answer

Join those tables:

You might want to turn it to outer join if you want to display categories that don’t exist in the products table.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement