SQL SELECT statement: get exactly the columns you need
Every report you pull from a database starts with SELECT. You tell the database which columns you want and which table they live in, and it hands back the answer as a new table.
In short: SELECT name, city FROM customers; returns the name and city of every row in the customers table. SELECT * returns every column. Columns go after SELECT, separated by commas; the table goes after FROM.
What you should know first: what tables, rows and columns are (SQL basics).
Try it now
All exercises use a small database from an online shop that sells office supplies. The table is shown above the editor. Write your query, press Run to see the result straight away, then Check to compare it with the task. You can tap table and column names instead of typing them.
Task Sales needs a list of all customers and where they are. From the customers table, get the columns name and city, in that order.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
Anatomy of a SELECT statement
SELECT name, price
FROM products;
| Part | What it does | In the example |
|---|---|---|
SELECT | Lists the columns you want in the result, in the order you want them, separated by commas. | name, price |
FROM | Names the table the rows come from. | products |
; | Ends the statement. Optional for a single query, required when you run several in a row. | at the end |
The result has one row for every product and exactly two columns. Here are the first three:
| name | price |
|---|---|
| Copy paper, 500 sheets | 4.99 |
| Blue ballpoint pens, 10-pack | 3.49 |
| Lever arch file, gray | 2.79 |
Think of it as hiding columns in a spreadsheet, except the original stays untouched. SELECT only reads. Run it a hundred times and the data is exactly the same afterwards. Changing data takes different statements (INSERT, UPDATE, DELETE), which come in level 4 of the course.
SELECT *: every column at once
The star is shorthand for "all columns". SELECT * FROM products; returns all five columns of the products table in the order they were created. That is handy when you open a table for the first time and want to see what is in it.
For anything you save or share, such as a report, a query behind a dashboard or code in an application, list the columns by name. Your result keeps the same shape if someone adds a column to the table next month, and the database does not ship data nobody needs.
If an exercise asks for specific columns and you answer with SELECT *, the check tells you which columns it is looking for.
Another exercise: three columns
This time the warehouse needs a stock list. Watch the column order: the task spells it out.
Task For the warehouse: show the product number, name and stock level of every product. The columns are product_id, name and stock.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
Upper case, lower case, line breaks
SQL keywords are not case sensitive, so select name from customers works just as well. The common habit is to write keywords in capitals and table and column names in lower case, so you can tell commands from names at a glance.
The database ignores line breaks and extra spaces. Once queries get longer, put each clause on its own line: SELECT first, FROM below it, and later WHERE and ORDER BY underneath. Colleagues who read your query later will thank you.
Common mistakes
A comma after the last column
SELECT name, city, FROM customers fails because after a comma the database expects another column and finds FROM instead. The exercise then says "There is one comma too many before FROM." Commas go between columns, never after the last one.
A misspelled column name
Names have to match exactly. SELECT name, cty FROM customers gets the message "There is no column "cty". Did you mean city?" The safest fix is to tap the names above the editor instead of typing them.
Column names in single quotes
SELECT 'name', city FROM customers runs without an error, but the first column says "name" in every row. Single quotes turn anything into fixed text. The check answers "Column and row count match, but not the values. Check that you fetch the right columns", and the comparison below shows "name" where the customer name should be. Leave column names unquoted.
Columns in the wrong order
SELECT city, name returns the right data, but the task asked for name first. The check notices and says "The columns are right, but not their order." In a report, column order is part of the answer.
Forgetting FROM
SELECT name, city on its own does not say where the data lives, so the database cannot find the columns. The exercise reports "FROM is missing, with the table the columns come from". Add FROM customers and it works.
SELECT in MySQL, PostgreSQL, SQL Server and SQLite
A plain SELECT columns FROM table looks the same in every major database. The differences appear when a name contains a space or clashes with a keyword. Then you have to quote the name, and each system has its own habit:
| Database | Name with a space | Upper and lower case in names |
|---|---|---|
| MySQL | `Sales Region` (backticks); double quotes only with the ANSI_QUOTES mode | Column names: never case sensitive. Table names: depends on the operating system (not case sensitive on Windows, case sensitive on most Unix systems) |
| PostgreSQL | "Sales Region" | Unquoted names are folded to lower case; quoted names are case sensitive |
| SQL Server | [Sales Region] or "Sales Region" (double quotes need QUOTED_IDENTIFIER ON, the usual setting) | Depends on the collation of the database |
| SQLite (these exercises) | "Sales Region"; also accepts square brackets and backticks for compatibility | In our exercises, Customers and customers both work |
The easy way out is to name tables and columns without spaces in the first place, as the practice database does: customer_since rather than "Customer Since".
FAQ
Is SELECT * bad practice?
Not in itself. For a quick look at a table it is perfect. In saved queries, reports and application code, name the columns, so the result stays stable and only the data you need is transferred.
Can I select the same column twice?
Yes. SELECT name, name FROM customers shows the name twice. It becomes useful with calculations, for example price and price including tax side by side. You will do that in the lesson calculations in SQL.
Why does the same value appear more than once?
SELECT returns every row, even if two rows look identical in the columns you picked. SELECT city FROM customers lists Boston twice because two customers are based there. To show each value only once, use DISTINCT.
In what order do the rows come back?
Without ORDER BY, no order is guaranteed. Often you get the rows in the order they were stored, but you cannot rely on it. Sorting is covered in the lesson ORDER BY.
What is the difference between SELECT and a SELECT statement?
SELECT is the keyword. The SELECT statement is the whole query built around it: SELECT with its columns, FROM with the table, and later optional clauses such as WHERE, GROUP BY and ORDER BY.