Blue Week Special Offer | Brighten your week!
days
hours
minutes
seconds

Not sure why the syntax is incorrect in this "Ordering Results" problem

Screen Link: Learn data science with Python and R projects

My Code:

SELECT customer_id, first_name, last_name, 
FROM customer
ORDER BY (CASE country  WHEN 'Canada' THEN 0
            WHEN 'USA' THEN 1
            ELSE 2
     END) AS country;

What I expected to happen:

I expected this to work even though it is different from the answer. I guess I don’t see why it wouldn’t work.

What actually happened:

OperationalError: near "FROM": syntax error

So basically, I think I’m missing something fundamental about how the SQL engine runs when processing this type of query. Also, in the LEARN section the approach required us create essentially 2 queries. And earlier, I thought that if you add a comma after the last column name in the SELECT statement, then it essentially holds a place for the column that is created, which in this case we also are required to use to order the column by. So maybe I’m a bit confused…does ORDER BY then need to come after the FROM statement?

1 Like

Hi @ratnesh.bh,

Just remove the comma before FROM.

However, I see also a couple of other issues:

  • Add country to the columns in the SELECT statement (as it’s requested in the task).
  • You shouldn’t alias the CASE statement as country. Instead, you should first order the rows of the resulting table by the CASE statement (first goes Canada, then the USA), second by country. Just put a comma instead of AS :slightly_smiling_face:
1 Like