Skip to main content

SELECT: Retrieving Columns

SELECT is the entry point of every read query in SQL. It tells the database which columns you want to see in the result.

The sample dataset (customers, products, orders) is defined in the Introduction to SQL lesson.

Syntax

-- Select specific columns
SELECT column1, column2, column3
FROM table_name;

-- Select all columns
SELECT *
FROM table_name;

Examples

Example 1: Select all columns

SELECT *
FROM customers;

Result:

customer_idnameemailcitycountry
1Alice Johnsonalice@example.comNew YorkUSA
2Bob Smithbob@example.comLondonUK
3Carol Whitecarol@example.comSydneyAustralia
4Dave Browndave@example.comTorontoCanada
5Eve Daviseve@example.comNew YorkUSA

Example 2: Select specific columns

Naming only the columns you need is the standard practice in real-world queries.

SELECT
order_id,
order_date,
total_amount
FROM orders;

Result:

order_idorder_datetotal_amount
12024-01-05999.00
22024-01-1059.98
32024-01-15349.00
42024-02-01796.00
52024-02-1049.90
.........

Common Mistake

Avoid SELECT * in production

SELECT * is useful for quick exploration but is risky in production code. If someone adds or renames a column, your query silently returns different data. Always list the columns you actually need.

Practice

Write a query that returns the name, category, and price of every product.

Show answer
SELECT
name,
category,
price
FROM products;

Expected result:

namecategoryprice
LaptopElectronics999.00
MouseElectronics29.99
DeskFurniture349.00
ChairFurniture199.00
NotebookStationery4.99
Donate to this project