{"id":1091,"date":"2016-06-15T13:22:44","date_gmt":"2016-06-15T17:22:44","guid":{"rendered":"https:\/\/solutionsreview.com\/cloud-platforms\/?p=1091"},"modified":"2019-08-27T16:45:53","modified_gmt":"2019-08-27T20:45:53","slug":"sql-hacks-morpheus-data","status":"publish","type":"post","link":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/","title":{"rendered":"SQL Database Hacks Using AS and ORDER BY"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1095\" src=\"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY.jpg\" alt=\"SQL Database Hacks Using AS and ORDER BY\" width=\"800\" height=\"350\" srcset=\"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY.jpg 800w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY-300x131.jpg 300w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY-768x336.jpg 768w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY-600x263.jpg 600w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY-180x79.jpg 180w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY-400x175.jpg 400w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p><a href=\"https:\/\/www.morpheusdata.com\" target=\"_blank\" rel=\"noopener noreferrer\">Morpheus Data&#8217;s<\/a> CTO, <a href=\"https:\/\/twitter.com\/bd_wheeler\" target=\"_blank\" rel=\"noopener noreferrer\">Brian Wheeler<\/a>, contributes with this SQL HowTo,\u00a0that\u00a0walks users through some advanced SQL hacks guaranteed\u00a0to save time if they use SQL regularly.\u00a0<a href=\"https:\/\/www.morpheusdata.com\">Morpheus<\/a>\u00a0allows users\u00a0to provision apps in a single click, and provides ease of use for developers with APIs and a CLI. Backups are automatic, and users\u00a0can have redundancy as needed to avoid potentially long waits for disaster recovery to take place.<\/p>\n<h3><b>SQL Database Hacks Using AS and ORDER BY.<\/b><b>\u00a0<\/b><\/h3>\n<p>By Brian Wheeler, CTO | Morpheus Data<\/p>\n<p>When retrieving data from an SQL database, it is often handy to be able to reference a column name with a shorter or more meaningful name when you make use of the results. Also, many times it is very helpful if the results are ordered in ascending or descending order by the content of a particular column. These things can be done in an SQL query using AS and ORDER BY.<\/p>\n<p><b>How to use AS<\/b><\/p>\n<p>In SQL, the AS keyword can be used to make referencing a column easier. For example, you may be querying a table with one or more long column names, like the following table:<\/p>\n<p>Table: orders<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p>| id | number_of_rocks_ordered | number_of_socks_ordered |<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p>| 1 | 2 | 1 |<\/p>\n<p>| 2 | 3 | 3 |<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p>To make the column names easier to deal with in clauses, subqueries, or on the programming side, you can use AS in your query to provide a shorter alias to use later, as in the following example:<\/p>\n<p>SELECT id, number_of_rocks_ordered AS rocks FROM orders;<\/p>\n<p>Now, the column name can later be referred to as simply \u2018rocks\u2019 later.<\/p>\n<p>The results will look like the following:<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8211;+<\/p>\n<p>| id | rocks |<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8211;+<\/p>\n<p>| 1 | 2 |<\/p>\n<p>| 2 | 3 |<\/p>\n<p>+&#8212;-+&#8212;&#8212;-+<\/p>\n<p>This can be especially helpful when providing an API for programmers, as the shorter alias can be returned from the API route to make for a little less typing on the programming side.<\/p>\n<p><b>How to use ORDER <\/b><b>BY<\/b><\/p>\n<p>The ORDER BY command can be used to order the results of a query by a particular column. So, using the table from above, you could decide to order the results by the number or rocks ordered, from most to least. To do this, add ORDER BY, followed by the column name or alias, followed by ASC (for ascending) or DESC (for descending), as in the following example:<\/p>\n<p>SELECT<\/p>\n<p>id,<\/p>\n<p>number_of_rocks_ordered<\/p>\n<p>FROM<\/p>\n<p>orders<\/p>\n<p>ORDER BY<\/p>\n<p>number_of_rocks_ordered DESC;<\/p>\n<p>Since you wanted the number of rocks ordered from most to least, DESC was used. This produces the following set of results:<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p>| id | number_of_rocks_ordered |<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p>| 2 | 3 |<\/p>\n<p>| 1 | 2 |<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p><b>Combining AS and ORDER BY<\/b><\/p>\n<p>Since you can use an alias created by the AS keyword in a later clause, you can combine AS and ORDER BY for this example to provide a result set that is both ordered by most rocks ordered to least, but also has the shorter column name of \u2018rocks\u2019. Here is the query to do this:<\/p>\n<p>SELECT<\/p>\n<p>id,<\/p>\n<p>number_of_rocks_ordered AS rocks<\/p>\n<p>FROM<\/p>\n<p>orders<\/p>\n<p>ORDER BY<\/p>\n<p>rocks DESC;<\/p>\n<p>Notice that the shorter \u2018rocks\u2019 alias is used in the ORDER BY clause, making that a little shorter. The result set from using both AS and ORDER BY looks like this:<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8211;+<\/p>\n<p>| id | rocks |<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8211;+<\/p>\n<p>| 2 | 3 |<\/p>\n<p>| 1 | 2 |<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8211;+<\/p>\n<p>Now the result set is both ordered and uses a shorter name for the number of rocks ordered!<\/p>\n<p><b>Databases and monitoring with Morpheus<\/b><\/p>\n<p>Morpheus is a SaaS solution which allows you to provision databases, including SQL and NoSQL databases, as well as apps and servers with a user-friendly interface. In addition to this, Morpheus can monitor your apps, servers, and databases. With Morpheus, data logging is automatic as you provision servers and apps. Using the available tools, you can monitor the various parts of your system to keep track of uptime, response time, and to be alerted if an issue does arise.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1096\" src=\"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/morpheus-interface-1.png\" alt=\"morpheus-interface (1)\" width=\"640\" height=\"444\" srcset=\"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/morpheus-interface-1.png 640w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/morpheus-interface-1-300x208.png 300w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/morpheus-interface-1-389x270.png 389w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/morpheus-interface-1-117x81.png 117w, https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/morpheus-interface-1-259x180.png 259w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><i>Source: Morpheus.<\/i><\/p>\n<hr \/>\n<p style=\"text-align: left\">About the author<\/p>\n<div>\n<div><img loading=\"lazy\" decoding=\"async\" id=\"6B39673E-9DAF-46FA-9FBF-32C899024982\" src=\"https:\/\/outlook.office.com\/owa\/service.svc\/s\/GetFileAttachment?id=AAMkADY1NzE1MjZjLTRlZjAtNGY5YS05YTQ0LTZmMTU0MWEwNjg5ZgBGAAAAAACXi%2BthE3L0SI9wqM1XqBe%2BBwBC4iFB0LorSI%2BZGbbWUB08AAAAAAEMAABC4iFB0LorSI%2BZGbbWUB08AAA6l2SlAAABEgAQAIJ9D86sYy1AqSE%2F8ONLDdk%3D&amp;X-OWA-CANARY=3BoAymRRu06-aB-AiBGcWsBR5xUNltMYDpWsueKZyLlQFS5ioWjx035seDWGi8otoc57VzRhhr4.\" width=\"205\" height=\"210\" \/><\/div>\n<\/div>\n<div><\/div>\n<div>Jared McKinney<\/div>\n<div>\n<div>Content Marketing Manager<\/div>\n<div><a href=\"https:\/\/www.morpheusdata.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Morpheus Data<\/a><\/div>\n<div><\/div>\n<\/div>\n<hr \/>\n<br \/>Widget not in any sidebars<br \/>\n","protected":false},"excerpt":{"rendered":"<p>Morpheus Data&#8217;s CTO, Brian Wheeler, contributes with this SQL HowTo,\u00a0that\u00a0walks users through some advanced SQL hacks guaranteed\u00a0to save time if they use SQL regularly.\u00a0Morpheus\u00a0allows users\u00a0to provision apps in a single click, and provides ease of use for developers with APIs and a CLI. Backups are automatic, and users\u00a0can have redundancy as needed to avoid potentially [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":1095,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[353,352,308,350,354,349],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQL Database Hacks Using AS and ORDER BY - Cloud Platforms &amp; Infrastructure Services | Solutions Review<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/\",\"url\":\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/\",\"name\":\"SQL Database Hacks Using AS and ORDER BY - Cloud Platforms &amp; Infrastructure Services | Solutions Review\",\"isPartOf\":{\"@id\":\"https:\/\/solutionsreview.com\/cloud-platforms\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY.jpg\",\"datePublished\":\"2016-06-15T17:22:44+00:00\",\"dateModified\":\"2019-08-27T20:45:53+00:00\",\"author\":{\"@id\":\"\"},\"breadcrumb\":{\"@id\":\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#primaryimage\",\"url\":\"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY.jpg\",\"contentUrl\":\"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY.jpg\",\"width\":800,\"height\":350},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/solutionsreview.com\/cloud-platforms\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Database Hacks Using AS and ORDER BY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/solutionsreview.com\/cloud-platforms\/#website\",\"url\":\"https:\/\/solutionsreview.com\/cloud-platforms\/\",\"name\":\"Cloud Platforms &amp; Infrastructure Services | Solutions Review\",\"description\":\"Evaluating Hyperscale Cloud Providers, Hybrid IaaS, PaaS &amp; Cloud Management Tools.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/solutionsreview.com\/cloud-platforms\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"\",\"url\":\"https:\/\/solutionsreview.com\/cloud-platforms\/author\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Database Hacks Using AS and ORDER BY - Cloud Platforms &amp; Infrastructure Services | Solutions Review","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/","twitter_misc":{"Written by":"","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/","url":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/","name":"SQL Database Hacks Using AS and ORDER BY - Cloud Platforms &amp; Infrastructure Services | Solutions Review","isPartOf":{"@id":"https:\/\/solutionsreview.com\/cloud-platforms\/#website"},"primaryImageOfPage":{"@id":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#primaryimage"},"image":{"@id":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#primaryimage"},"thumbnailUrl":"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY.jpg","datePublished":"2016-06-15T17:22:44+00:00","dateModified":"2019-08-27T20:45:53+00:00","author":{"@id":""},"breadcrumb":{"@id":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#primaryimage","url":"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY.jpg","contentUrl":"https:\/\/solutionsreview.com\/cloud-platforms\/files\/2016\/06\/SQL-Database-Hacks-Using-AS-and-ORDER-BY.jpg","width":800,"height":350},{"@type":"BreadcrumbList","@id":"https:\/\/solutionsreview.com\/cloud-platforms\/sql-hacks-morpheus-data\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/solutionsreview.com\/cloud-platforms\/"},{"@type":"ListItem","position":2,"name":"SQL Database Hacks Using AS and ORDER BY"}]},{"@type":"WebSite","@id":"https:\/\/solutionsreview.com\/cloud-platforms\/#website","url":"https:\/\/solutionsreview.com\/cloud-platforms\/","name":"Cloud Platforms &amp; Infrastructure Services | Solutions Review","description":"Evaluating Hyperscale Cloud Providers, Hybrid IaaS, PaaS &amp; Cloud Management Tools.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/solutionsreview.com\/cloud-platforms\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"","url":"https:\/\/solutionsreview.com\/cloud-platforms\/author\/"}]}},"_links":{"self":[{"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/posts\/1091"}],"collection":[{"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/comments?post=1091"}],"version-history":[{"count":0,"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/posts\/1091\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/media\/1095"}],"wp:attachment":[{"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/media?parent=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/categories?post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solutionsreview.com\/cloud-platforms\/wp-json\/wp\/v2\/tags?post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}