SQLTeam.com | Weblogs | Forums

How to make CASE in SQL?

Hi all,

I just started SQL for the first time ever yesterday and I am doing an online course with some app data.

I would like to sort this data descending and create a new column called "price rating" through the CASE code, as seen below:

However, when I enter this code, nothing happens and I am wondering what I did wrong? I saw the online course do this earlier, but it does not work for me for some reason.

Can anybody spot my mistake? Thanks!

My full code:

SELECT *

FROM fake_apps

ORDER BY price DESC;

SELECT fake_apps

CASE

WHEN price = 3.99 THEN 'fair price'

WHEN price = 14.99 THEN 'expensive'

ELSE 'cheap'

END AS 'price rating';

SELECT *

FROM fake_apps

ORDER BY price DESC;

SELECT CASE

WHEN price = 3.99 THEN 'fair price'

WHEN price = 14.99 THEN 'expensive'

ELSE 'cheap'

END AS 'price rating' from fake_apps;

This worked! Thank you so much!

Any idea what went wrong in my setup?

Ah nevermind it displayed what I wanted, but it does not create the column 'PRICE RATING' :frowning:

works for me

this is what I get (no new column named price rating)

am I doing something wrong?

Try this

SELECT *, CASE

WHEN price = 3.99 THEN 'fair price'

WHEN price = 14.99 THEN 'expensive'

ELSE 'cheap'

END AS 'price rating'

FROM fake_apps

ORDER BY price DESC;

1 Like

It worked!

Thank you sir!

Any idea why it did not work for me before?

you were having 2 different queries which didn't match your output.
Now, it is one query.

1 Like