Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

MySQL Order By

PHP

MySQL ORDER BY

ORDER BY clause MySQL में records को sort करने के लिए उपयोग किया जाता है। PHP में ORDER BY का उपयोग करके आप data को ascending (ASC) या descending (DESC) order में आसानी से प्राप्त कर सकते हैं। यह reports, lists, tables, search results, और pagination जैसी जगहों पर बहुत उपयोगी होता है।

  • ASC → ascending (A → Z, 1 → 100)
  • DESC → descending (Z → A, 100 → 1)

ORDER BY कब उपयोग करें?

  • Users list को alphabetically show करने के लिए
  • Latest records (e.g., newest posts, newest orders) दिखाने के लिए
  • Top scoring students या highest salary employees को sort करने के लिए
  • Reports में data को विशिष्ट format में display करने के लिए

Basic Syntax

SQL: ORDER BY
SELECT * FROM students ORDER BY name ASC;
-- नाम alphabetically sort होंगे (A → Z)

SELECT * FROM students ORDER BY class DESC;
-- class high to low

Important Notes

  • ORDER BY हमेशा SELECT के बाद लगता है
  • Multiple columns पर भी sort किया जा सकता है
  • Performance के लिए sorted column पर index बनाना अच्छा होता है
  • Pagination + ORDER BY एक साथ उपयोग करना best practice है

Multiple Column ORDER BY

SQL: Multiple Columns
SELECT * FROM students ORDER BY class ASC, name ASC;
-- पहले class के अनुसार sort, फिर name के अनुसार

MySQLi Procedural Example (ORDER BY)

यह example छात्रों को उनकी class descending order में sort करके fetch करता है:

उदाहरण: MySQLi Procedural (ORDER BY)
 <?php
  $conn = mysqli_connect("localhost", "root", "", "school");
  $sql = "SELECT * FROM students ORDER BY class DESC";
  $result = mysqli_query($conn, $sql);
  while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . " - Class: " . $row['class'] . "<br>";
  }
  mysqli_close($conn);
  ?>

MySQLi Procedural + LIMIT (Sorting + Pagination)

ORDER BY और LIMIT का combination pagination और large data load के लिए जरूरी है:

उदाहरण: ORDER BY + LIMIT
  <?php
  $conn = mysqli_connect("localhost", "root", "", "school");
  $limit = 5;
  $sql = "SELECT * FROM students ORDER BY id DESC LIMIT $limit";
  $result = mysqli_query($conn, $sql);
  while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . " - Class: " . $row['class'] . "<br>";
  }
  mysqli_close($conn);
  ?>

PDO Example (ORDER BY)

PDO में ORDER BY बहुत आसानी से query किया जाता है:

उदाहरण: PDO (ORDER BY)
 <?php
  $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");
  $stmt = $pdo->query("SELECT * FROM students ORDER BY name ASC");
  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  foreach ($rows as $row) {
    echo $row['name'] . " - " . $row['class'] . "<br>";
  }
  ?>

PDO Example with Dynamic Column + Safety

Sorting column को dynamic बनाना हो तो SQL injection से बचने के लिए allowed list का उपयोग करें:

उदाहरण: PDO सुरक्षित Dynamic ORDER BY
 <?php
  $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");

  $allowed = ['name', 'class', 'id'];
  $order = 'class';  // user input

  if (!in_array($order, $allowed)) {
    $order = 'id';
  }

  $stmt = $pdo->query("SELECT * FROM students ORDER BY $order ASC");
  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  foreach ($rows as $row) {
    echo $row['name'] . " - " . $row['class'] . "<br>";
  }
  ?>

Best Practices

  • Always use ORDER BY with LIMIT जब large datasets हों।
  • Indexed columns पर sort करें (performance बढ़ती है)।
  • User input को direct ORDER BY में न पास करें → SQL injection risk!
  • Stable sorting के लिए ORDER BY id ASC या unique column उपयोग करें।

Common Mistakes

गलती (Mistake) सही तरीका (Fix)
ORDER BY के साथ non-indexed column → slow query उस column पर INDEX बनाएं।
User input को direct ORDER BY में डालना Allowed list validation करें।
Sorting + Pagination बिना ORDER BY हमेशा ORDER BY साथ उपयोग करें ताकि consistent results मिलें।

Conclusion

ORDER BY MySQL का सबसे आम लेकिन powerful clause है जो data को logically sort करने में मदद करता है। PHP में इसे MySQLi और PDO दोनों के साथ आसानी से लागू किया जा सकता है। अच्छा performance और predictable results पाने के लिए ORDER BY के साथ LIMIT और proper indexing का उपयोग जरूर करें।

Article By: Brajlal Prasad
Created on: 05 Nov 2025  62  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store