Every query that asks a database "give me some data" starts with SELECT. This lesson covers the shape of a basic SELECT query, and the examples that follow build on an imaginary employees table used throughout this course.
Learning Objectives
- Write a SELECT query that returns specific columns, or all of them.
- Rename a column in the result using AS.
- Understand why naming columns explicitly is usually better than SELECT *.
Sample Table: employees
| id | first_name | last_name | department | salary |
|---|---|---|---|---|
| 1 | Ada | Lovelace | Engineering | 95000 |
| 2 | Grace | Hopper | Engineering | 98000 |
| 3 | Margaret | Hamilton | Research | 87000 |
SELECT columns
What you want back
FROM table
Where to get it
Result Set
Rows returned to you
Selecting Specific Columns
SELECT lists the columns you want, in the order you want them. FROM names the table to pull them from. The query above returns every row, but only those two columns.
SELECT first_name, last_name FROM employees; -- Result: -- first_name | last_name -- Ada | Lovelace -- Grace | Hopper -- Margaret | Hamilton
Selecting Everything With *
The asterisk (*) means "every column." It's convenient while you're exploring a table you don't know yet, but naming columns explicitly is usually better in real code: it's clearer to read, and it keeps working even if someone adds a new column to the table later that your code didn't expect.
- + Fast to type while exploring an unfamiliar table
- + No need to update the query if you just want "whatever columns exist"
- − Reads more data than needed, slower on a wide table over a real network connection
- − Fragile: a column added later silently starts flowing through every place that used SELECT *
- − Makes it harder for a reader to know what a query actually depends on
SELECT * FROM employees;
Renaming a Column With AS
AS gives a column a different name in the result, without changing anything in the actual table. It's useful for making result columns more readable, or for naming a computed column, covered later in this course.
SELECT first_name AS `First Name`, salary AS annual_salary FROM employees;
SELECT first_name AS "First Name", salary AS annual_salary FROM employees;
SELECT first_name AS [First Name], salary AS annual_salary FROM employees;
Quoting a multi-word alias is the one real difference here: PostgreSQL and SQLite use double quotes ("First Name"), MySQL uses backticks (`First Name`), and SQL Server uses square brackets ([First Name]). annual_salary needs none of them, quoting is only required when the name has spaces or matches a reserved word.
Try It Yourself
Using the employees table above, write a query that returns every employee's last_name and salary, with salary renamed to yearly_pay in the result.
SELECT last_name, salary AS yearly_pay FROM employees;
Frequently Asked Questions
Does the order of columns in SELECT matter?
Yes, the result comes back in exactly the order you list them in, regardless of their order in the actual table.
Why avoid SELECT * in real applications?
Two main reasons: it's slower when a table has many columns you don't actually need, since the database still has to read and send all of them, and it's fragile, if someone adds a column later, every place using SELECT * silently starts returning more data than it expected.
Common Beginner Mistakes
Reaching for SELECT * out of habit in real application code
It's fine while exploring a table interactively, but in code that ships, it reads more data than needed and silently changes behavior if someone adds a column later. List the columns you actually use.
Assuming AS renames the column in the actual table
It only renames the column in this query's result set. The underlying table is completely untouched.
Best Practices
- Name the columns you actually need instead of SELECT * in any query that isn't purely exploratory.
- Use AS to give computed or awkwardly-named columns a clear, readable name in the result.
- Keep column aliases free of spaces and reserved words when you can, it avoids needing database-specific quoting at all.
Interview Questions
What's the execution order between SELECT and FROM, conceptually?
Even though SELECT is written first, the database logically processes FROM first (deciding which table and rows to work with), then applies SELECT to shape which columns come back.
Write a query that returns the last_name column renamed to Surname.
SELECT last_name AS Surname FROM employees; — no quoting needed here since Surname is a single word and not a reserved keyword.
Summary
This lesson covered naming specific columns versus using * for all of them, and renaming a result column with AS, including how alias quoting differs slightly by database when the alias has spaces.
What's Next?
The next lesson covers SELECT DISTINCT, removing duplicate rows from a result.
Ready to Practice SQL?
Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.
Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.