1

I have 3 tables with the following schema

Table1 - plan
id | count

Table2 - subscription
id | plan_id

Table3 - users
id | subscription_id 

I have to select count from plans where its plan.id matches with subscription.plan_id matches and users.subscription_id matches with subscription.id and users.id = '5'

How can I use a JOIN here?

I tried using with no JOIN as below:

SELECT count 
    FROM users, subscription, plan 
    WHERE users.id = '5' and users.subscription_id = subscription.id and subscription.plan_id = plan.id;
e4c5
  • 52,766
  • 11
  • 101
  • 134
Siddharthan Asokan
  • 4,321
  • 11
  • 44
  • 80

1 Answers1

0

Try to avoid implicit sql joins rather use explicit joins (e.g. inner join,left join..) in order to retain readability.

A cleaner way would be the following:

SELECT
    count
FROM
    users
INNER JOIN  subscription ON users.subscription_id = subscription.id
INNER JOIN  plan ON subscription.plan_id = plan.id
WHERE   users.id = '5'

Explicit vs implicit SQL joins

Community
  • 1
  • 1
1000111
  • 13,169
  • 2
  • 28
  • 37