TGViewer
Data Analytics Data Analytics @sqlspecialist · 111K subscribers
Post #2722 7.92K
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
  • ❤ 26
More from @sqlspecialist
  1. Oct 9, 2026“Here, the data is sorted by the second column in descending order and the first five rows…
  2. Oct 9, 2026📊 Data Analyst Interview Series — Part 6 Guys, let's continue our Data Analyst Interview…
  3. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  4. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  5. Oct 7, 2026📊 Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vid…
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions such…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →