SQL SELECT and FROM: pick the columns you need
Every query starts with two words: SELECT says which columns, FROM says which table.
SQL skills · One table
What you will learn
A table is a grid: columns such as customer and amount, and one row per invoice, order or employee. SELECT FROM is how you ask for a piece of that grid.
In this guide you will pick columns, take every column with the star, and rename columns for a report.
The basics
Data in a database lives in tables. A table has columns (customer, amount, due date) and rows (one invoice, one order, one employee). A query asks the database for a piece of a table.
The two words every query starts with
SELECT customer, amount
FROM invoicesSELECT lists the columns you want, separated by commas. FROM names the table. The result keeps the column order you wrote.
Every column at once
SELECT *
FROM invoicesThe star means every column. It is handy for a first look at a table. In a report, list the columns you need instead.
New names for the result
SELECT customer AS client, amount AS total
FROM invoicesAS renames a column in your result only. The table keeps its own names. Use letters, digits and _ in the new name.
Common mistakes
A missing comma: SELECT customer amount shows one column called amount, filled with customer names. A comma right before FROM is a syntax error. The table name must match exactly: invoices, not invoice.
Worked examples
Example 1
Your AR manager wants the customer and amount of every invoice.
SELECT customer, amount
FROM invoicesTwo columns, every row. The column order in SELECT is the column order in the result.
Example 2
The same, but the report needs the header “total” instead of amount.
SELECT customer, amount AS total
FROM invoicesAS renames the column in the result only; the table keeps its own name.
Reading helps, typing makes it stick. Every task gives you a fresh table, and your query is checked by a real database.
Try a taskCommon mistakes
- Forgetting the comma: SELECT customer amount shows one column named amount that holds customer names.
- A comma right before FROM, as in SELECT customer, amount, FROM invoices, which is a syntax error.
- Using the wrong table name, such as invoice instead of invoices. Check the exact name in the table list.
Where you will use it at work
- Finance: pull invoice numbers and amounts for a reconciliation.
- HR: list employee names and departments for an org chart.
- Marketing: export campaign names and spend for a slide.
Questions people ask
Is SELECT * bad?
It is fine for a first look at a table. In reports and saved queries, list the columns you need: the result is smaller and does not change when someone adds a column.
Do keywords have to be in capitals?
No. select and SELECT both work. Capitals are a habit that makes queries easier to read.
Does the semicolon matter?
For one query it is usually optional. It separates queries when you run several at once.
Ready to write it yourself?
Practice SELECT and FROM