top of page

SQL Ordering and Limiting

Updated: May 1


SQL Ordering and Limiting


sql ordering and limiting:


SQL Ordering and Limiting

 

Write a query to select the 4 animals who sleep the least, displayed in ascending order by hours of sleep.

To order from smallest first, either remove the DESC flag or use the ASC (ascending) flag.

 

Answer: SELECT * FROM animals

ORDER BY hours_sleep ASC LIMIT 4

 

 

Least Lazy Carnivores

Now let's add in a WHERE clause.

 

Select the 3 carnivores who sleep the least. In other words, WHERE diet = 'carnivore'.

Note that SQL requires you to put the WHERE clause before the ORDER BY clause.

 

Answer: SELECT * FROM animals

WHERE diet = ‘carnivore’

ORDER BY hours_sleep ASC LIMIT 3

 

Largest Moons of Saturn

Note that SQL requires you to put the ORDER BY clause before the LIMIT clause.

What are the 3 largest radius moons of Saturn?

 

Answer:

SELECT * FROM moons

WHERE planet = ‘Saturn’

ORDER BY radius DESC LIMIT 3

 

Largest Countries of Africa

Select the 5 largest area countries of Africa.

 

Answer:

SELECT * FROM countries

WHERE continent = ‘Africa’

ORDER BY area DESC LIMIT 5


Ready to become a SQL wizard in record time? Dive into the world of databases with our comprehensive ebook!



Whether you're a complete beginner or looking to brush up on your skills, "Mastering SQL" is your ticket to proficiency.





18 views0 comments

Comments


bottom of page