{"id":20098,"date":"2020-12-21T12:47:44","date_gmt":"2020-12-21T12:47:44","guid":{"rendered":"http:\/\/www.devopsschool.com\/blog\/?p=20098"},"modified":"2022-04-28T09:15:09","modified_gmt":"2022-04-28T09:15:09","slug":"basic-sql-command-in-a-database","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/basic-sql-command-in-a-database\/","title":{"rendered":"Basic SQL Command in a database"},"content":{"rendered":"\n<p><strong>The SQL ORDER BY Keyword<\/strong><\/p>\n\n\n\n<p>The ORDER BY keyword is used to sort the result-set in ascending or descending order.<\/p>\n\n\n\n<p>The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.<\/p>\n\n\n\n<p><strong>ORDER BY Syntax<\/strong><\/p>\n\n\n\n<p>SELECT&nbsp;<em>column1<\/em>,<em>&nbsp;column2, &#8230;<\/em><br>FROM&nbsp;<em>table_name<\/em><br>ORDER&nbsp;BY&nbsp;<em>column1, column2, &#8230;&nbsp;<\/em>ASC|DESC;<\/p>\n\n\n\n<p><strong>Demo Database<\/strong><\/p>\n\n\n\n<p>Below is a selection from the &#8220;Customers&#8221; table in the Northwind sample database:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/12\/databae.png\" alt=\"\" class=\"wp-image-20102\"\/><\/figure>\n\n\n\n<p>The following SQL statement selects all customers from the &#8220;Customers&#8221; table, sorted by the &#8220;Country&#8221; column:<\/p>\n\n\n\n<p>SELECT * FROM Customers<br>ORDER BY CustomerID;<\/p>\n\n\n\n<p><strong>This Result <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/12\/Oder-by-cutomerID-1024x600.png\" alt=\"\" class=\"wp-image-20107\"\/><\/figure>\n\n\n\n<p><strong>ORDER BY Several Columns Example<\/strong><\/p>\n\n\n\n<p>The following SQL statement selects all customers from the &#8220;Customers&#8221; table, sorted by the &#8220;CustomerID&#8221; and the &#8220;PostalCode&#8221; column. This means that it orders by CustomerID, but if some rows have the same CustomerID, it orders them by PostalCode:<\/p>\n\n\n\n<p>SELECT * FROM Customers<br>ORDER BY CustomerID, PostalCode;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/12\/two-table-1024x571.png\" alt=\"\" class=\"wp-image-20106\"\/><\/figure>\n\n\n\n<p><strong>ORDER BY DESC Example<\/strong><\/p>\n\n\n\n<p>The following SQL statement selects all customers from the &#8220;Customers&#8221; table, sorted DESCENDING by the &#8220;Country&#8221; column:<\/p>\n\n\n\n<p>SELECT * FROM Customers<br>ORDER BY CustomerID DESC;<\/p>\n\n\n\n<p>This is result<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/12\/desc-1024x593.jpg\" alt=\"\" class=\"wp-image-20111\"\/><\/figure>\n\n\n\n<p><strong>ORDER BY ASC Example<\/strong><\/p>\n\n\n\n<p>The following SQL statement selects all customers from the &#8220;Customers&#8221; table, sorted abbreviation by the &#8220;Country&#8221; column:<\/p>\n\n\n\n<p>SELECT * FROM Customers<br>ORDER BY CustomerID ASC;<\/p>\n\n\n\n<p>This is result<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/12\/2569-1024x577.jpg\" alt=\"\" class=\"wp-image-20113\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The SQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5201],"tags":[6537,6536,6535],"class_list":["post-20098","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-ascdesc","tag-column2","tag-order-by-column1"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/20098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=20098"}],"version-history":[{"count":0,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/20098\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=20098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=20098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=20098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}