Writing Queries task #20


#1


https://www.codecademy.com/en/courses/learn-sql/projects/learn_sql_query_table

The task is "Return the first 10 movies sorted in reverse alphabetical order." I think Limit has to be the last thing in the query so I'm unsure how to do this. When I enter it the way I did below it just gives me the last 10 movies when they are sorted alphabetically but how do I get the first 10 movies and then sort them?



SELECT * FROM movies
ORDER BY name DESC
LIMIT 10;


#2

I'm not sure whether it's a case of them actually wanting it limited before the sort, or just poor wording.

Either way, if you want the former, you can use a nested query:

SELECT * FROM (
  SELECT * FROM foo
  /* do stuff */
)
ORDER BY bar DESC;

#3

Ok interesting. I'm guessing it was poor wording because they didn't teach nested queries yet and I had no idea that you can do that but that's great to know now. Thank you!


#4

SELECT * FROM (
SELECT * FROM movies
ORDER BY id ASC
LIMIT 10)
AS t1
ORDER BY name DESC;


#5

Hello Nedwards. Please can you tell me more about nested queries? Actually i'm trying to understand meaning of each component and if you can help me with this i will be very grateful.Or you can recommend me some useful source of information (in your opinion). Thanks.
SELECT * FROM (SELECT * FROM movies ORDER BY id ASC LIMIT 10) AS t1 ORDER BY name DESC; - This method is working. Do you offer the same way? In this way only part 'AS t1' is unclear for me.


#6

The answers given above are from more experienced users. Nesting has not even been touched upon so clearly that is not what's required at this stage. Below is the Query statement that worked for me:

SELECT * FROM movies WHERE id <= 10 ORDER BY name DESC;