DevLearningTools

LEARN · SQL

SELECT Basics

SELECT is how you ask a database for data. Learn how to pick specific columns, select everything, and give columns a friendlier name with AS.

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.

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

idfirst_namelast_namedepartmentsalary
1AdaLovelaceEngineering95000
2GraceHopperEngineering98000
3MargaretHamiltonResearch87000

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. This exact syntax is identical across every SQL database.

MySQL / PostgreSQL / SQLite / SQL Server
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.

MySQL / PostgreSQL / SQLite / SQL Server
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 you'll meet in a later lesson on aggregate functions.

MySQL
SELECT first_name AS `First Name`, salary AS annual_salary
FROM employees;
PostgreSQL / SQLite
SELECT first_name AS "First Name", salary AS annual_salary
FROM employees;
SQL Server
SELECT first_name AS [First Name], salary AS annual_salary
FROM employees;
NOTE

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 SQL word.

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.

Do I need a semicolon at the end of a query?

Most database tools accept a single query without one, but it's required when running multiple queries together, and it's good habit to always include it.

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.

Forgetting FROM and only writing SELECT

SELECT needs a source table to pull from; without FROM naming one, the query is incomplete and the database will reject it with a syntax error.

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, run the query again without AS and the original column name is right there.

Quoting an alias the same way on every database

PostgreSQL and SQLite expect double quotes for a multi-word alias ("First Name"), MySQL expects backticks (`First Name`), and SQL Server expects square brackets ([First Name]). Copying a quoted alias between databases without adjusting the quote style is a common source of a confusing syntax error.

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.
  • Get in the habit of ending every query with a semicolon, even when the tool you're using doesn't strictly require it.

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. Writing order and evaluation order aren't the same thing in SQL.

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 SQL keyword.

Why can SELECT * hurt performance on a wide table?

The database has to read every column's data off disk and send it all over the connection, even the columns the caller never uses, which costs more I/O and network time than fetching only the needed columns.

Does an alias created with AS exist outside the query that defined it?

No, it's scoped to that single query's result set only. The table's real column name is unchanged, and the alias isn't something you can reference in a separate later query.

Summary

This lesson covered the shape of a basic SELECT query: 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 introduces WHERE, narrowing a query down to only the rows that match a condition instead of returning the whole table.