Module 2 : SQL Column Selection & Aliases (AS): Step-by-Step

Module 2

Selecting Specific Columns & Using Column Aliases (AS)

A step-by-step, hands-on SQL practice lesson

SELECT first_name AS "Customer Name" FROM customers;

In Module 1, you learned how SELECT * pulls every column from a table. That's a great starting point, but real-world queries are rarely that broad. In this lesson, you'll learn how to pick exactly the columns you need, and give them clean, readable names using the AS keyword — a skill you'll use in almost every query you ever write.

Introduction

What You'll Learn — and Why It Matters

🎯 Objective

By the end of this lesson, you will be able to:

  • Select one or more specific columns from a table instead of every column.
  • Rename a column's output label using AS, without changing the actual table structure.
  • Create calculated columns and give them meaningful alias names.
  • Recognize where aliases can — and can't — be reused inside the same query.

⚠️ What to Avoid

Common mistakes
  • Using SELECT * out of habit, even when you only need two or three columns.
  • Forgetting to quote an alias that contains a space, like AS Full Name instead of AS "Full Name".
  • Assuming an alias can be used in a WHERE clause in the same query — it can't, and we'll explain exactly why below.

💡 Important to Know

Before you start

An alias only changes what the column is called in your results — it never renames the real column in the table. Run the query again without the alias, and the table itself is exactly as it was. Aliases are also temporary: they only exist for the length of that one query.

Requirements

What You'll Need Before Starting

Operating SystemWindows, macOS, or Linux — this lesson works identically on all of them.
Database ServerMySQL installed and running. See How to Install MySQL if you haven't set it up yet.
Optional but recommendedXAMPP with phpMyAdmin, for a visual way to run queries. See How to Install XAMPP.
Prior knowledgeBasic familiarity with SELECT * FROM table; from Module 1.

Practice

Step-by-Step Practice

Follow along in order — each step builds on the one before it.

1 Open MySQL (CLI or phpMyAdmin)

Open a terminal and connect with the MySQL command-line client, or open phpMyAdmin in your browser at http://localhost/phpmyadmin/.

mysql -u root -p

The basic syntax pattern you'll use throughout this lesson looks like this:

SELECT column1, column2 FROM table_name;

2 Create a Database and Verify It

Create a fresh practice database and confirm it exists before moving on.

CREATE DATABASE module2_practice;
SHOW DATABASES;
USE module2_practice;

Now create a table and verify it was created:

CREATE TABLE customers (
  id INT PRIMARY KEY,
  first_name VARCHAR(50),
  last_name VARCHAR(50),
  email VARCHAR(100),
  country VARCHAR(50)
);
SHOW TABLES;
DESCRIBE customers;

3 Insert Sample Data

Add a few rows so you have something real to query against.

INSERT INTO customers (id, first_name, last_name, email, country) VALUES
(1, 'Sokha', 'Chan', 'sokha@email.com', 'Cambodia'),
(2, 'Maria', 'Lopez', 'maria@email.com', 'Spain'),
(3, 'Wei', 'Zhang', 'wei@email.com', 'China');

4 Select All Columns First (Baseline)

Start with SELECT * so you can see the full table before narrowing it down.

SELECT * FROM customers;

5 Select Specific Columns

Now pick only the columns you actually need.

SELECT first_name, last_name, email FROM customers;

6 Rename a Column with a Simple Alias

Use AS to give a column a friendlier name in the output.

SELECT first_name AS given_name, last_name AS surname FROM customers;

7 Alias with Spaces (Quoted)

If you want a label with a space in it, wrap the alias in quotes.

SELECT first_name AS "First Name", last_name AS "Last Name" FROM customers;

8 Alias on a Calculated Column

Aliases are especially useful when the column doesn't already exist — like a combined full name.

SELECT CONCAT(first_name, ' ', last_name) AS full_name, country
FROM customers;

9 Sort by an Alias

Unlike WHERE, the ORDER BY clause can reference an alias directly — because it runs after column names are already assigned.

SELECT first_name AS given_name, country
FROM customers
ORDER BY given_name;

Troubleshooting

Common Problems and Solutions

Problem 1: "Unknown column 'alias_name' in 'where clause'"

You tried to filter using an alias, like WHERE given_name = 'Sokha', right after aliasing first_name AS given_name.

SolutionMySQL evaluates WHERE before it assigns column aliases, so the alias doesn't exist yet at that point. Filter using the real column name instead: WHERE first_name = 'Sokha'.

Problem 2: Syntax error near "AS"

Your query throws a syntax error right at the alias keyword.

SolutionCheck for a missing comma between column names, or an alias that matches a reserved SQL word (like order or group) without quotes. Wrap reserved-word aliases in backticks: AS \`order\`.

Problem 3: Alias doesn't show up correctly in phpMyAdmin

The result grid still shows the original column name instead of your alias.

SolutionDouble-check your spelling of AS and that there's no typo in the alias itself. Also confirm you re-ran the query after editing it — phpMyAdmin sometimes shows cached results from the previous run.

Problem 4: Alias with a space "breaks" the query

Writing AS Full Name without quotes causes an error, because MySQL reads Name as a second, separate column.

SolutionAlways wrap multi-word aliases in double quotes or backticks: AS "Full Name" or AS \`Full Name\`.

Conclusion

What You Should Take Away

By practicing these nine steps, you should now be comfortable selecting exactly the columns you need instead of pulling everything with SELECT *, and renaming those columns with AS so your results are easier to read and share. This habit alone will make every future query you write cleaner and more professional.

In Module 3, we'll build directly on this by learning how to filter rows with WHERE — combining column selection, aliases, and conditions into complete, real-world queries.

Homework

Homework & Quiz

📝 Homework Task

Using the customers table from this lesson, write your own query that:

  • Selects email and country only.
  • Renames email to "Contact Email".
  • Sorts the results by country.

Try writing it yourself before checking — there's no single "correct" answer as long as the logic matches these three requirements.

🧪 Quiz: Test Your Understanding (10 Questions)

Answer all 10, then click Submit to see your score instantly.

1. Which keyword renames a column or table in the result set?

2. What does SELECT name FROM users; return?

3. Which is the correct syntax for an alias containing a space?

4. True or False: an alias changes the actual column name stored in the table.

5. Can you use a column alias inside a WHERE clause in the same SELECT statement?

6. Which symbol quotes column or table names containing special characters or reserved words in MySQL?

7. What does SELECT price * 0.9 AS discounted_price do?

8. Which clause CAN reference a column alias, unlike WHERE?

9. What happens if you misspell an alias in your ORDER BY clause?

10. Best practice: should you use SELECT * in production applications?

0 / 10

FAQ

Frequently Asked Questions

1. What is the difference between SELECT * and selecting specific columns?

SELECT * returns every column in the table. Naming specific columns returns only what you ask for — faster, clearer, and safer for production applications.

2. Is AS required, or can I skip it?

In MySQL, AS is optional — SELECT name full_name FROM users; works the same as SELECT name AS full_name FROM users;. Most developers keep AS anyway, because it makes the query easier to read.

3. Can I alias a table name too, not just a column?

Yes. SELECT * FROM customers AS c; lets you refer to the table as c for the rest of the query — very useful once you start writing JOINs.

4. Does aliasing slow down my query?

No. Aliasing is just a display label applied after MySQL has already retrieved the data — it has no meaningful effect on performance.

5. Can two columns share the same alias?

MySQL won't stop you from writing it, but your results will show two columns with an identical header, which is confusing to read. Always use a unique alias for each column.

6. Why does my alias disappear when I re-run the query?

Aliases only exist for the lifetime of that single query — they're never saved anywhere. You'll need to include AS again every time you write that query.

7. Can I use numbers in an alias name?

Yes, as long as the alias doesn't start with a number and doesn't contain spaces or special characters — or if it does, wrap it in quotes or backticks.

8. What's the difference between single quotes, double quotes, and backticks for aliases?

Single quotes are for string values. Double quotes and backticks are used to quote identifiers like column names or aliases — backticks are MySQL's own standard way to do this.

9. Can I use a calculated alias in a GROUP BY clause?

Yes, in MySQL you generally can reference an alias in GROUP BY, the same way you can in ORDER BY — both run after column names are resolved.

10. Does aliasing work the same way in PostgreSQL or SQL Server?

The core idea is the same everywhere, but quoting rules differ slightly — PostgreSQL uses double quotes for identifiers, and SQL Server commonly uses square brackets, like [Full Name].

11. Why did my query fail when I aliased a column as "order"?

"Order" is a reserved SQL keyword (used in ORDER BY). Wrap it in backticks — AS \`order\` — so MySQL treats it as a plain name instead of part of the syntax.

12. Is it bad practice to alias every single column, even when the name is already clear?

Not bad, but usually unnecessary. Reserve aliases for columns that genuinely benefit from a clearer name — calculated columns, abbreviations, or anything a report reader wouldn't immediately understand.

Post a Comment

Previous Post Next Post