[Solution] Why do I have the Wrong Platform?

Click here to view the question


Solution:

Add an “order by 1” in the subquery to ensure that the platform names are sorted alphabetically.

select
 platform
 , source
 , count(*)
from
 all_users
where
 platform = (
   select
     platform
   from
     all_platforms
   order by 1
   limit 1
 )
group by
 1
 , 2

Concepts Covered: Limits and Order By Clauses

Explanation:

SQL outputs do not have a defined order unless specified in an order by clause.  In other words, "select" returns records in no particular order if there isn't an order by clause. Therefore, the “limit 1” is randomly returning one platform each time the query was run previously. 

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
Like Follow
  • 11 mths agoLast active
  • 169Views
  • 1 Following