91. Generate All Possible User-Category Pairs

A recommendation system wants to precompute all possible combinations of users and content categories so that personalized suggestions can later be applied.

Table: Users

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘   user_id   ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘    name     ā•‘ varchar  ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • user_id is the primary key.
  • Each row represents a registered user.

Table: Categories

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘    Type  ā•‘
╠═════════════╬══════════╣
ā•‘ category_id ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘  category   ā•‘ varchar  ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • category_id is the primary key.
  • Each row represents a content category.

Write an SQL query to generate all possible combinations of users and categories. The result can be returned in any order.

Example 1:

Input:

Users:

╔══════════╦══════════╗
ā•‘ user_id  ā•‘   name   ā•‘
╠══════════╬══════════╣
ā•‘    1     ā•‘  Aarav   ā•‘
║──────────┼──────────║
ā•‘    2     ā•‘   Neha   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Categories:

╔═════════════╦══════════╗
ā•‘ category_id ā•‘ category ā•‘
╠═════════════╬══════════╣
ā•‘     10      ā•‘  Sports  ā•‘
║─────────────┼──────────║
ā•‘     20      ā•‘  Music   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Output:

╔══════════╦══════════╦═════════════╦══════════╗
ā•‘ user_id  ā•‘   name   ā•‘ category_id ā•‘ category ā•‘
╠══════════╬══════════╬═════════════╬══════════╣
ā•‘    1     ā•‘  Aarav   ā•‘     10      ā•‘  Sports  ā•‘
║──────────┼──────────┼─────────────┼──────────║
ā•‘    1     ā•‘  Aarav   ā•‘     20      ā•‘  Music   ā•‘
║──────────┼──────────┼─────────────┼──────────║
ā•‘    2     ā•‘   Neha   ā•‘     10      ā•‘  Sports  ā•‘
║──────────┼──────────┼─────────────┼──────────║
ā•‘    2     ā•‘   Neha   ā•‘     20      ā•‘  Music   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Explanation

There are 2 users and 2 categories, so total rows = 2 Ɨ 2 = 4.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Ā 
Test Case

Input:

Users
Categories