DEV Community

Cover image for ๐Ÿš€ SQL Querying Using Aliases: With and Without the `AS` Keyword
Arkadipta kundu
Arkadipta kundu

Posted on

๐Ÿš€ SQL Querying Using Aliases: With and Without the `AS` Keyword

If you've worked with SQL before, you've likely come across the concept of aliases. They're a great way to make your SQL queries more readable and concise, especially when you're dealing with complex tables or need a more meaningful name for your columns.

In this post, I'm going to walk you through how to use aliases in SQL, both with and without the AS keyword. By the end, you'll have a clearer understanding of how they work and why they can make your life a lot easier when writing SQL queries!

๐Ÿง What are Aliases in SQL?

Aliases are essentially temporary names you can assign to tables or columns in your queries. They don't change the actual names in the database; they just help make things more readable in your result set.

For example, if you have a table called customers and a column named first_name, you can use an alias to rename first_name to something simpler, like fname, in the results.

๐Ÿ’ก Using Aliases with the AS Keyword

The typical way to create an alias in SQL is with the AS keyword. You place AS between the original name (of the column or table) and the alias name you want to use.

Column Aliases

Hereโ€™s how you can use a column alias:

SELECT first_name AS fname, last_name AS lname
FROM customers;
Enter fullscreen mode Exit fullscreen mode

In this query:

  • The column first_name is given the alias fname.
  • The column last_name is given the alias lname.

This helps when you want your result set to be more readable, especially if you're working with long or complex column names.

Table Aliases

Now, let's look at table aliases. Table aliases are super handy when you're working with multiple tables (like in a JOIN) or when you want to shorten a long table name.

SELECT c.first_name, c.last_name, o.order_id
FROM customers AS c
JOIN orders AS o
ON c.customer_id = o.customer_id;
Enter fullscreen mode Exit fullscreen mode

Here:

  • Weโ€™ve given the table customers the alias c.
  • The table orders is given the alias o.

Now, instead of typing out the full table names every time, you can use c and o for a cleaner and more concise query. This is especially useful when your query involves several tables and columns.

๐Ÿ˜Ž Using Aliases Without the AS Keyword

Hereโ€™s a little secret: You donโ€™t actually need the AS keyword to use aliases! In most SQL databases, you can simply put a space between the original name and the alias name, and SQL will treat it as an alias.

Letโ€™s rewrite the previous examples without using AS:

Column Aliases Without AS

SELECT first_name fname, last_name lname
FROM customers;
Enter fullscreen mode Exit fullscreen mode

Yep, that's it! Just a space between the column name and its alias. SQL knows what you're doing here.

Table Aliases Without AS

SELECT c.first_name, c.last_name, o.order_id
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id;
Enter fullscreen mode Exit fullscreen mode

Again, no need for ASโ€”just a simple space works.

๐Ÿ”„ When to Use (or Skip) the AS Keyword?

So, when should you use AS, and when can you skip it? It really comes down to personal preference or team coding standards. Both styles work just fine, but here are a few things to keep in mind:

Use AS When:

  • You want to make your query more explicit. For new learners, using AS makes it clearer that you're creating an alias.
  • You're working in a team that prefers to follow the convention of using AS.

Skip AS When:

  • You prefer cleaner and shorter code. Dropping the AS keyword can save a bit of typing and make your query look slightly neater.
  • You're confident that others reading the code will still understand what you're doing (most experienced SQL users will get it).

๐Ÿ‘ฉโ€๐Ÿ’ป Practice Time!

Hereโ€™s a simple practice query. See if you can spot where the aliases are being used, both with and without AS:

SELECT c.first_name fname, c.last_name lname, o.order_date
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id;
Enter fullscreen mode Exit fullscreen mode

Notice how weโ€™ve mixed and matched here? AS for column aliases, but no AS for table aliases. This is just to show you that you can mix styles as long as your SQL engine supports it.

๐Ÿš€ Conclusion

Using aliases in SQL is a simple but powerful technique to make your queries more readable. Whether you choose to use the AS keyword or just leave a space between the name and the alias, itโ€™s entirely up to you.

Next time you write a query, try experimenting with both styles and see which one feels better for you. The goal is to make your SQL code easy to read and understandโ€”for you and anyone else who might come across it!

Happy querying! ๐Ÿ˜„

Top comments (0)