Try these yourself first.
Q1. Display all employees sorted by salary from highest to lowest.
Q2. Find the top 5 highest-priced products.
Q3. Display customers alphabetically by name.
Q4. Find the 10 customers with the highest spending.
Q5. Display employees by department alphabetically and salary from highest to lowest within each department.
Q6. Find the 3 cheapest products.
Q7. Display unique customer cities alphabetically.
Q8. Return the second page of 10 customers ordered by customer_id.
Q9. Find the 5 most profitable products.
Q10. Explain why ORDER BY revenue DESC LIMIT 3 cannot directly find the top 3 products in each category.
✅ Answers
Answer 1
SELECT employee_name, salary FROM employees ORDER BY salary DESC;
Answer 2
SELECT product_name, price FROM products ORDER BY price DESC LIMIT 5;
Answer 3
SELECT customer_name FROM customers ORDER BY customer_name ASC;
Answer 4
SELECT customer_name, total_spend FROM customers ORDER BY total_spend DESC LIMIT 10;
Answer 5
SELECT employee_name, department, salary FROM employees ORDER BY department ASC, salary DESC;
Answer 6
SELECT product_name, price FROM products ORDER BY price ASC LIMIT 3;
Answer 7
SELECT DISTINCT city FROM customers ORDER BY city ASC;
Answer 8
SELECT * FROM customers ORDER BY customer_id LIMIT 10 OFFSET 10;
Answer 9
SELECT product_name, profit FROM products ORDER BY profit DESC LIMIT 5;
Answer 10
Because LIMIT 3 applies to the entire result, not separately to each category. To get the top 3 within every category, you need a window function such as ROW_NUMBER() or DENSE_RANK().
🔥 Mini Challenge
You have products:
1 Laptop Electronics 90000
2 Phone Electronics 70000
3 Monitor Electronics 50000
4 Chair Furniture 80000
5 Desk Furniture 60000
Business Requirement:
Find the 3 products generating the highest revenue overall.
Steps:
1. Retrieve products, 2. Sort revenue highest → lowest, 3. Keep 3 rows
The solution is:
SELECT product_name, category, revenue FROM products ORDER BY revenue DESC LIMIT 3;
Double Tap ❤️ For Part-5
