SQL Database Hacks Using AS and ORDER BY

SQL Database Hacks Using AS and ORDER BY

Morpheus Data’s CTO, Brian Wheeler, contributes with this SQL HowTo, that walks users through some advanced SQL hacks guaranteed to save time if they use SQL regularly. Morpheus allows users to provision apps in a single click, and provides ease of use for developers with APIs and a CLI. Backups are automatic, and users can have redundancy as needed to avoid potentially long waits for disaster recovery to take place.

SQL Database Hacks Using AS and ORDER BY. 

By Brian Wheeler, CTO | Morpheus Data

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.

How to use AS

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:

Table: orders

+—-+———————————- +———————————–+

| id | number_of_rocks_ordered | number_of_socks_ordered |

+—-+———————————–+———————————–+

| 1 | 2 | 1 |

| 2 | 3 | 3 |

+—-+———————————–+———————————–+

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:

SELECT id, number_of_rocks_ordered AS rocks FROM orders;

Now, the column name can later be referred to as simply ‘rocks’ later.

The results will look like the following:

+—-+——–+

| id | rocks |

+—-+——–+

| 1 | 2 |

| 2 | 3 |

+—-+——-+

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.

How to use ORDER BY

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:

SELECT

id,

number_of_rocks_ordered

FROM

orders

ORDER BY

number_of_rocks_ordered DESC;

Since you wanted the number of rocks ordered from most to least, DESC was used. This produces the following set of results:

+—-+———————————–+

| id | number_of_rocks_ordered |

+—-+———————————–+

| 2 | 3 |

| 1 | 2 |

+—-+———————————–+

Combining AS and ORDER BY

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 ‘rocks’. Here is the query to do this:

SELECT

id,

number_of_rocks_ordered AS rocks

FROM

orders

ORDER BY

rocks DESC;

Notice that the shorter ‘rocks’ 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:

+—-+——–+

| id | rocks |

+—-+——–+

| 2 | 3 |

| 1 | 2 |

+—-+——–+

Now the result set is both ordered and uses a shorter name for the number of rocks ordered!

Databases and monitoring with Morpheus

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.

morpheus-interface (1)

Source: Morpheus.


About the author

Jared McKinney
Content Marketing Manager