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_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164

Anatomy of a SELECT statement

SELECT name, price
FROM products;
PartWhat it doesIn the example
SELECTLists the columns you want in the result, in the order you want them, separated by commas.name, price
FROMNames 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:

nameprice
Copy paper, 500 sheets4.99
Blue ballpoint pens, 10-pack3.49
Lever arch file, gray2.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_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

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:

DatabaseName with a spaceUpper and lower case in names
MySQL`Sales Region` (backticks); double quotes only with the ANSI_QUOTES modeColumn 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 compatibilityIn 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.

Learn this next: WHERE

So far you always get every row. WHERE keeps only the rows that meet a condition, such as all customers in Chicago or all products under 10.

Practice WHERE

Write to us

Choose language

Select your preferred language