Over a million developers have joined DZone.

SQL Database Hacks Using AS and ORDER BY

Here are a few hacks you can throw into your SQL to help sort and organize what you want to pull from your data while saving time.

Learn NoSQL for free with hands-on sample code, example queries, tutorials, and more.  Brought to you in partnership with Couchbase.

Image title

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!

The Getting Started with NoSQL Guide will get you hands-on with NoSQL in minutes with no coding needed. Brought to you in partnership with Couchbase.

Topics:
saas ,saas architecture ,database ,sql

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}