0

Please , can someone point out my mistake ? It's an SQL task.

I'm working on an SQL task which asks me to sort cars by model year. My code : SELECT company, year FROM cars ORDER BY company WHERE year = 2020; When i run it , the app says this : "ERRO: syntax error at or near "WHERE" LINE 4: WHERE year = 2020; ^ " Any ideas ? As always , i greatly appreciate this community ! :).

6th Feb 2021, 5:52 AM
Kirill Chernyatin
Kirill Chernyatin - avatar
14 Respostas
+ 11
Try to place filter specification first (WHERE), then followed by sorting specification (ORDER BY). SELECT company, year FROM cars WHERE year = 2020 ORDER BY company;
6th Feb 2021, 5:58 AM
Ipang
+ 6
Well, that's odd, let's see what others has to say ... Did the task specify to filter cars built around 2020? (WHERE year = 2020)? maybe it can be removed. BTW, your Description said "to sort cars by model year" I think the ORDER BY should use 'year' rather than 'company'.
6th Feb 2021, 6:07 AM
Ipang
+ 3
Well, I don't know either. But if that solves the case then it's good news! 👍
6th Feb 2021, 8:59 AM
Ipang
+ 2
Apparently the Description in the original post above was different with the most recent one you shared. I'm sorry my suggestion didn't work out, but I'm pretty sure someone more knowledgeable can assist you to get through 👍
6th Feb 2021, 6:26 AM
Ipang
+ 2
Ipang Indeed ! :).
6th Feb 2021, 8:59 AM
Kirill Chernyatin
Kirill Chernyatin - avatar
+ 1
Ipang Thank you for the suggestion , but the only thing that changed is that the ERROR message now says the same thing , but with ORDER BY instead pf WHERE.
6th Feb 2021, 6:01 AM
Kirill Chernyatin
Kirill Chernyatin - avatar
+ 1
@Ipang This is the description : The WHERE Statement "You are given the following table of cars with details :contentImage Write a query to output the brand and model years of the cars produced in 2020, sorted by the 'company' column. Use the WHERE statement!" I've used your suggestion of changing the ORDER BY , but it only changed the ERROR message to the first one i got. Thank you for this suggestion nonetheless ! :).
6th Feb 2021, 6:13 AM
Kirill Chernyatin
Kirill Chernyatin - avatar
+ 1
Ipang Oh , sorry. Thank you for your time & have a great day with fantastic luck , wherever & whenever you are ! :). 👍
6th Feb 2021, 6:28 AM
Kirill Chernyatin
Kirill Chernyatin - avatar
+ 1
Ipang Hey , Ipang ! After some Googling , i found this code from one @Såñtösh Here it is : select company ,year from cars where year=2020 order by company; I don't know how , but it worked.
6th Feb 2021, 7:37 AM
Kirill Chernyatin
Kirill Chernyatin - avatar
+ 1
try this and it will resolve the problem. thanks for understanding select company, year from cars where year=2020 order by company Asc;
12th Aug 2021, 8:18 PM
FREDERIC DANIMBE
0
To type the order by is after where condition
7th Feb 2021, 9:27 AM
Arunkumar G
Arunkumar G - avatar
0
FREDERIC DANIMBE Hi , Frederic ! I have solved this issue long ago , but thank you for your help nonetheless ! Have a great day & fantastic luck ! :).
12th Aug 2021, 8:19 PM
Kirill Chernyatin
Kirill Chernyatin - avatar
0
U should put 'order by' after 'where' condition
4th Dec 2021, 8:42 AM
Agung Permana Mukti
Agung Permana Mukti - avatar
0
select * from cars where engine ='electric' or year > 2018
12th Dec 2021, 11:40 AM
Malaika Imran
Malaika Imran - avatar