2๏ธโƒฃ ๐Ÿ”Ž MySQL SQL Fundamentals
Estimated reading: 4 minutes 40 views

๐Ÿงฑ MySQL Sorting & Limiting (ORDER BY, LIMIT, DISTINCT)


๐Ÿงฒ Introduction โ€“ Why Learn Sorting & Limiting in MySQL?

Fetching data with SQL is only part of the jobโ€”how you organize and reduce the results matters just as much. MySQL offers powerful keywords like ORDER BY, LIMIT, and DISTINCT to help you sort, paginate, and eliminate duplicates in query results.

๐ŸŽฏ In this tutorial, youโ€™ll learn:

  • How to sort query results using ORDER BY
  • How to limit the number of rows returned with LIMIT
  • How to remove duplicate rows using DISTINCT
  • Use cases for each operator with real-world examples

๐Ÿ“˜ What Are Sorting & Limiting Clauses in MySQL?

These SQL clauses control how data is displayed and how much of it is shown. Together, they make SQL queries more precise and efficient.

ClausePurpose
ORDER BYSorts results by one or more columns
LIMITRestricts the number of returned rows
DISTINCTRemoves duplicate rows

๐Ÿงช MySQL ORDER BY โ€“ Sort Query Results

๐Ÿ”น Syntax:

SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC|DESC];

๐Ÿ”น Example 1: Sort Customers by Name (A-Z)

SELECT name, city FROM customers
ORDER BY name ASC;

๐Ÿ”น Example 2: Sort by Date (Latest First)

SELECT name, signup_date FROM users
ORDER BY signup_date DESC;

๐Ÿ“˜ Use ASC for ascending (default), DESC for descending order.


๐ŸŽฏ ORDER BY with Multiple Columns

SELECT name, city, age FROM employees
ORDER BY city ASC, age DESC;

๐Ÿ“Œ Sorts first by city (A-Z), then by age (highest first) within each city.


๐Ÿšฆ MySQL LIMIT โ€“ Control the Result Size

๐Ÿ”น Syntax:

SELECT column1 FROM table_name
LIMIT number;

๐Ÿ”น Example 1: Get Top 5 Products

SELECT * FROM products
ORDER BY rating DESC
LIMIT 5;

๐Ÿ“Œ Returns the 5 highest-rated products.


๐Ÿ”น Pagination with LIMIT and OFFSET

SELECT * FROM users
ORDER BY id ASC
LIMIT 10 OFFSET 20;

๐Ÿ“Œ Returns 10 rows starting from the 21st record (offset starts from 0).


โœจ MySQL DISTINCT โ€“ Eliminate Duplicates

๐Ÿ”น Syntax:

SELECT DISTINCT column1 FROM table_name;

๐Ÿ”น Example 1: Unique Cities from Customers

SELECT DISTINCT city FROM customers;

๐Ÿ“Œ Returns each city only once, even if multiple customers are from the same city.


๐Ÿ”น Example 2: Multiple Columns with DISTINCT

SELECT DISTINCT country, city FROM customers;

๐Ÿ“Œ Returns unique pairs of country and city.

๐Ÿ“˜ DISTINCT applies to the entire row returnedโ€”not just individual columns.


โš–๏ธ ORDER BY vs LIMIT vs DISTINCT โ€“ Quick Comparison

KeywordUse CaseExample
ORDER BYSorts dataORDER BY name ASC
LIMITLimits result rowsLIMIT 10
DISTINCTRemoves duplicate recordsSELECT DISTINCT city FROM customers

๐Ÿ› ๏ธ Real-World Use Cases

ScenarioSQL Example
๐Ÿ“ˆ Top 10 highest-paid employeesSELECT * FROM employees ORDER BY salary DESC LIMIT 10;
๐Ÿ“ Find all unique citiesSELECT DISTINCT city FROM users;
๐Ÿ“… Get recent 5 signupsSELECT * FROM users ORDER BY signup_date DESC LIMIT 5;
๐Ÿงพ Paginate products pageSELECT * FROM products LIMIT 20 OFFSET 40;

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Sorting, limiting, and de-duplicating results are core techniques in data querying and reporting. Mastering ORDER BY, LIMIT, and DISTINCT helps you return only the data you actually needโ€”neatly arranged and optimized.

๐Ÿ” Key Takeaways

  • ORDER BY sorts results by columns in ascending or descending order
  • LIMIT reduces the number of rows returned and supports pagination
  • DISTINCT filters out duplicate rows for clean, unique results
  • You can combine all three for powerful query output control

โš™๏ธ Real-World Relevance

From paginated web interfaces to dashboards and reports, these clauses optimize user experience and system performance.


โ“ FAQ โ€“ MySQL ORDER BY, LIMIT, DISTINCT

โ“ Can I use ORDER BY and LIMIT together?

โœ… Yes. It’s common to sort data and then limit how many results you see:

SELECT * FROM users ORDER BY created_at DESC LIMIT 10;

โ“ How does DISTINCT work with multiple columns?

โœ… It removes duplicate row combinations, not just individual column values.

โ“ Can I sort by an alias or expression?

โœ… Yes:

SELECT name, salary * 12 AS yearly_salary
FROM employees
ORDER BY yearly_salary DESC;

โ“ Whatโ€™s the difference between LIMIT and OFFSET?

โœ… LIMIT controls the number of rows. OFFSET skips rows:

LIMIT 5 OFFSET 10  -- skips first 10, shows next 5

โ“ Is DISTINCT faster than GROUP BY?

โœ… It depends on the data and indexing. For simple use cases, both can be equally effective.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงฑ MySQL Sorting & Limiting (ORDER BY, LIMIT, DISTINCT)

Or Copy Link

CONTENTS
Scroll to Top