Fundamentals of Data Analysis Using aliases for columns

26. Using aliases for columns

In this lesson, we’ll learn how to give columns alias names. It’s very useful when the result of one query is the input for another query. Wait, what?! You can query … another query?! Yes and more! We’ll learn a couple of tricks before combining queries and aliases is the first one.

Let’s look at the created_at column in the users table:

SELECT
  id,
  signup_date,
  created_at
FROM users

We’ve learned that the created_at column contains the precise timestamp when a user record was created. If we use the users table in a more complex query naming could get confusing (because the created_at column could also exist in other tables). To give it a better name we can use alias operator AS:

SELECT
  id,
  signup_date,
  created_at AS signup_timestamp
FROM users

If you run this query ☝ you’ll see that column name in the result changed from created_at to signup_timestamp.

Another good example is columns produced by aggregate functions:

SELECT
  COUNT(*)
FROM users

This query ☝ will produce the result like:

count
102245

It’s a 1x1 table with a column name count (assigned by default). If later we want to reuse it, it’s better to refer to it as users_count or even better total_users_count:

SELECT
  COUNT(*) AS total_users_count
FROM users

1 step closer to write even more complex queries! Now go and practice these ☝ 🚀

Anatoli Makarevich, author of SQL Habit About SQL Habit

Hi, it’s Anatoli, the author of SQL Habit. 👋

SQL Habit is a course (or, as some of the students say, “business simulator”). It’s based on a story of a fictional startup called Bindle. You’ll play a role of their Data Analyst 📊 and solve real-life challenges from Business, Marketing, and Product Management.

SQL Habit course is made of bite-sized lessons (you’re looking at one atm) and exercises. They always have a real-life setting and detailed explanations. You can immediately apply everything you’ve learned at work. 🚀

“well worth the money”

Fluent in SQL in a month

Master Data Analysis with SQL with real life examples from Product Management, Marketing, Finance and more.
-- Type your query here, for example this one -- lists all records from users table: SELECT * FROM users
Loading chart... ⏳