TGTGInsighttelegram intelligenceLIVE / telegram public index
← Data Analytics
Data Analytics avatar

TGINSIGHT POST

Post #2722

@sqlspecialist

Data Analytics

Views6,270Post view count
PostedApr 1804/18/2026, 06:17 PM
Post content

Post content

Now, Let’s move to the next topic of the SQL Roadmap: ORDER BY LIMIT 🧠 1. ORDER BY (Sorting Data) ORDER BY is used to sort your result. 👉Syntax SELECT column_name FROM table_name ORDER BY column_name; 🔹 Ascending Order (Default) SELECT * FROM employees ORDER BY salary ASC; ✔ Lowest salary → highest 🔹 Descending Order SELECT * FROM employees ORDER BY salary DESC; ✔ Highest salary → lowest 💡 2. Sorting Multiple Columns SELECT * FROM employees ORDER BY department ASC, salary DESC; 👉 First sorts by department 👉 Then salary within each department 🎯 3. LIMIT (Control Output Size) LIMIT is used to restrict the number of rows. 👉Syntax SELECT * FROM table_name LIMIT number; 👉Example SELECT * FROM employees LIMIT 5; ✔ Returns only the first 5 rows ⚡ 4. Using ORDER BY LIMIT 👉Top 5 highest salaries SELECT * FROM employees ORDER BY salary DESC LIMIT 5; 👉Lowest 3 salaries SELECT * FROM employees ORDER BY salary ASC LIMIT 3; 🎯 5. Practice Tasks 1. Show all employees sorted by salary (ascending) 2. Show all employees sorted by salary (descending) 3. Get top 3 highest paid employees 4. Get lowest 2 salary employees 5. Sort employees by department and salary ✅Practice Task Solution ✅ 1. Show all employees sorted by salary (ascending) SELECT * FROM employees ORDER BY salary ASC; ✅ 2. Show all employees sorted by salary (descending) SELECT * FROM employees ORDER BY salary DESC; ✅ 3. Get top 3 highest paid employees SELECT * FROM employees ORDER BY salary DESC LIMIT 3; ✅ 4. Get lowest 2 salary employees SELECT * FROM employees ORDER BY salary ASC LIMIT 2; ✅ 5. Sort employees by department and salary SELECT * FROM employees ORDER BY department ASC, salary DESC; 👉 First sorts by department 👉 Then highest salary inside each department ⚡ Mini Challenge 🔥 👉 Get the 2nd highest salary employee. ⚡ Mini Challenge Solution 🔥 ✔ Method 1 (Using LIMIT + OFFSET) SELECT * FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1; ✔ Method 2 (Alternative way) SELECT * FROM employees ORDER BY salary DESC LIMIT 1, 1; 🔥 Pro Tip: If you understand OFFSET → you can get Top N, 2nd highest, 3rd highest easily. ⚡ Double Tap ❤️ For More