Instruction
Great! Sometimes you may want to look at sorted data. Thankfully, LIMIT works with ORDER BY:
SELECT * FROM orders ORDER BY total_sum DESC LIMIT 10;
The above code will return the 10 rows with the highest total_sum. By default, PostgreSQL considers NULL values to be larger than any non-NULL value – this will result in all orders with NULL total_sum to appear first in this query. To avoid this, you can write a WHERE condition to exclude NULL values:
SELECT * FROM orders WHERE total_sum IS NOT NULL ORDER BY total_sum LIMIT 10;
There are also other ways to deal with NULLs in such queries, you can read about them in this article.
Exercise
This time, show the top ten highest salaries from the employees table. Select the position column as well. Modify the answer from the previous exercise. Only show employees with known salary.
You can try running the template before solving the exercise in order to see the difference for yourself.
Stuck? Here's a hint!
Type:
SELECT salary, position FROM employees WHERE salary IS NOT NULL ORDER BY salary DESC LIMIT 10;



