EasySQL challenge #93ORDER BY

Latest Reviews

Sort reviews by reviewed_at descending.

The challenge runs on the SQLabHub practice database (SQLite) and uses the reviews table. You can solve it in the browser, with nothing to install.

Solve This SQL Challenge

The task

Sort reviews by reviewed_at descending.

A correct answer returns 180 rows with 7 columns: id, product_id, user_id, rating, comment, reviewed_at, helpful_votes.

Tables used in this challenge

reviews 180 rows

id INTEGER (PK) · product_id INTEGER · user_id INTEGER · rating INTEGER · comment TEXT · reviewed_at TEXT · helpful_votes INTEGER

First 3 rows of the reviews table
idproduct_iduser_idratingcommentreviewed_athelpful_votes
123455Works perfectly. Very happy with this purchase.2022-09-0240
2101685Excellent build quality. Will buy again!2023-01-0596
340391Would give 6 stars if I could!2023-05-1517

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
idproduct_iduser_idratingcommentreviewed_athelpful_votes
99321024Decent but the packaging could be better.2024-12-1192
15081464Decent but the packaging could be better.2024-12-1090
2017133Impressive quality. Definitely buying more.2024-12-012
54421072Impressive quality. Definitely buying more.2024-11-2736
15258822Great product, exactly as described. Fast shipping!2024-11-2494

First 5 of 180 rows. This is the output of the model solution on the practice database.

What you practise

  • Returning every column of a table with SELECT *
  • Sorting the result with ORDER BY

Hint and approach

Try the challenge first. Open these only when you need them.

Show the hint

ORDER BY DESC

Show the step-by-step approach

How the model solution is built, in the order the database evaluates it:

  1. Read from the reviews table.
  2. Return every column with SELECT *.
  3. Sort the result by reviewed_at descending.

Model solution

Checking your session…

What to watch for

  • ORDER BY. Without ORDER BY the row order is not guaranteed, even if it looks stable. Rows that tie on the sort columns can come back in any order.
  • SELECT *. SELECT * is fine for exploring, but its output changes whenever a column is added to the table. Queries that feed a report should name their columns.
Solve This SQL Challenge

Keep learning