+ 3

The "17 Code Project" question left me confused about columns named with clause key words--Please help!

Bear with me for this question pls! The problem was: Write a query to output the apartments whose prices are greater than the average and are also not rented, sorted by price. The actual code that ran successfully was: SELECT * FROM Apartments WHERE price >= (SELECT AVG(price) FROM apartments) ORDER BY price The problem was a little difficult for me because it also asks to include, as part of the query, apartments whose status is "not rented". So, what I'd like to know is, how would I write that part of the query when the word "status" is recognized as a clause but in this problem "status" was also the name of a column of the apartments table?

3rd Feb 2022, 10:42 PM
Ana Collado
Ana Collado - avatar
9 Respostas
+ 2
As far as I can tell "status" is not a keyword in SQL. Apparently it is in MySQL. Here is a stack overflow thread about what to do in MySQL: https://stackoverflow.com/questions/6427352/a-column-called-status-in-mysql
3rd Feb 2022, 11:46 PM
Simon Sauter
Simon Sauter - avatar
+ 2
Thanks Simon Sauter I was asking because the word “status” turns purple as do the SELECT, WHERE, FROM, and ORDER BY clauses. I appreciate all your help!!
4th Feb 2022, 12:06 AM
Ana Collado
Ana Collado - avatar
+ 1
In theory that code should not be accepted as correct. The reason that it is accepted has to do with the way sololearn evaluates codes. All it does is compare the output of your code to the expected output. If it is identical your code is accepted as correct, if it is not your code is rejected. In this case the output is the same whether you add the condition that the apartment has to be "not rented" or not for the simple reason that there is no apartment which is rented and has an above average price.
3rd Feb 2022, 10:59 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Simon Sauter and thanks for the stackoverflow link!!!! 😩🙏
4th Feb 2022, 12:07 AM
Ana Collado
Ana Collado - avatar
+ 1
Strange, for me "status" shows up white. Both on the SL website and in the android app.
4th Feb 2022, 12:50 AM
Simon Sauter
Simon Sauter - avatar
+ 1
🥲 then why is it appearing in purple on SL on my iphone 😩
4th Feb 2022, 12:53 AM
Ana Collado
Ana Collado - avatar
+ 1
Maybe they use a different syntax highlighter in the iPhone app.
4th Feb 2022, 12:55 AM
Simon Sauter
Simon Sauter - avatar
+ 1
Sololearn uses PostgreSQL 13.0. You can check by running "SELECT VERSION()". According to the documentation "status" is not a keyword in PostgreSQL. https://www.postgresql.org/docs/current/sql-keywords-appendix.html
4th Feb 2022, 12:58 AM
Simon Sauter
Simon Sauter - avatar
0
Sololearn accepts this without error: WHERE     status = 'Not rented'
3rd Feb 2022, 11:48 PM
Simon Sauter
Simon Sauter - avatar