The ORDER BY clause in PostgreSQL is used to sort the result set of a query by one or more columns. By default, it sorts in ascending order.
To sort in ascending order, specify the column name followed by ASC. Since ascending order is the default, ASC can be omitted.
To sort in descending order, use the DESC keyword after the column name.
You can sort by multiple columns by listing them separated by commas. Each column can have its own sort direction.
PostgreSQL allows you to specify whether NULL values should appear first or last using NULLS FIRST or NULLS LAST.
Using ORDER BY can impact performance, especially on large datasets. Proper indexing can help mitigate this.
SELECT * FROM employees ORDER BY last_name ASC;
This query retrieves all records from the "employees" table and sorts them by "last_name" in ascending order.
Console Output:
John Doe, Jane Smith, Robert Brown
You can sort by multiple columns to create a more complex sorting logic. For instance, sort by department and within each department, sort by employee name.
Each column can have its own sorting direction. The first column could be ascending, while the second one could be descending.
SELECT * FROM employees ORDER BY department ASC, salary DESC;
This query sorts the "employees" table by "department" in ascending order and then by "salary" in descending order within each department.
Console Output:
HR: Alice (60000), Bob (55000); IT: Charlie (70000), Dave (65000)
NULL values can be sorted explicitly to appear first or last. This is useful when NULLs have a specific meaning in your dataset.
Use NULLS FIRST to make NULL values appear at the beginning of your sorted result set.
SELECT * FROM products ORDER BY price ASC NULLS FIRST;
This query sorts the "products" table by "price" in ascending order, placing NULL values first.
Console Output:
NULL, 10.99, 15.99, 20.00
Use NULLS LAST to ensure that NULL values appear at the end of your sorted result set.
SELECT * FROM products ORDER BY price DESC NULLS LAST;
This query sorts the "products" table by "price" in descending order, placing NULL values last.
Console Output:
20.00, 15.99, 10.99, NULL
You can use expressions in the ORDER BY clause to sort data based on calculated values.
SELECT *, (price * quantity) AS total_value FROM products ORDER BY total_value DESC;
This query calculates the "total_value" for each product and sorts the results in descending order based on this calculated value.
Console Output:
$200.00, $150.00, $100.00
Aliases can be used in the ORDER BY clause to refer to computed columns.
SELECT name, (price * quantity) AS total_value FROM products ORDER BY total_value DESC;
This query uses the alias "total_value" for a calculated column and sorts the results by this alias in descending order.
Console Output:
Product A: $200.00, Product B: $150.00
The ORDER BY clause can be combined with the LIMIT clause to return a specified number of sorted results.
SELECT * FROM employees ORDER BY hire_date DESC LIMIT 5;
This query retrieves the most recently hired five employees from the "employees" table.
Console Output:
Employee A, Employee B, Employee C, Employee D, Employee E
ORDER BY can be applied to subqueries to sort the results of the subquery before they are used by the outer query.
SELECT * FROM (SELECT * FROM employees ORDER BY salary DESC) AS sorted_employees LIMIT 3;
This query uses a subquery to sort employees by salary in descending order and then selects the top three results.
Console Output:
High Salary Employee 1, High Salary Employee 2, High Salary Employee 3
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies