All 40 lessons
Lesson 3 of 40 · SQL Basics
SQL SELECT Statement
By Skillancy Editorial Team
Every example runs on the ShopKart dataset, already loaded in the free SQL Compiler. Click Try it in the SQL Compiler under a query to open it there, then press Run.
The SELECT statement reads data from a table. You list the columns you want after SELECT and the table after FROM. Every analysis you'll ever write in SQL starts here.
Syntax
SELECT column1, column2, ...
FROM table_name;Examples
1. Select specific columns
Show each product's name and price:
SELECT product_name, price
FROM products;Try it in the SQL CompilerResult:
| product_name | price |
|---|---|
| Redmi Note 13 Pro | 23999.00 |
| boAt Airdopes 141 | 1299.00 |
| Samsung 43" Smart TV | 31990.00 |
| Men's Cotton Kurta | 1899.00 |
| Women's Silk Saree | 8499.00 |
| Running Shoes | 2499.00 |
| Prestige Pressure Cooker 5L | 2199.00 |
| Steel Water Bottle 1L | 549.00 |
| Non-stick Tawa | 899.00 |
| Herbal Face Wash | 199.00 |
| Kumkumadi Face Oil | 2995.00 |
| The Psychology of Money | 299.00 |
(showing 12 of 15 rows)
2. Select all columns with *
SELECT *
FROM categories;Try it in the SQL CompilerResult:
| category_id | category_name |
|---|---|
| 1 | Electronics |
| 2 | Fashion |
| 3 | Home & Kitchen |
| 4 | Beauty |
| 5 | Books |
| 6 | Sports |
(6 rows)
* is great for a first look at a table. Combine it with LIMIT on big tables so you don't pull millions of rows.
3. Calculated columns
You can do arithmetic inside SELECT. Here is the profit per unit on each product:
SELECT product_name,
price,
cost_price,
price - cost_price AS profit_per_unit
FROM products
LIMIT 5;Try it in the SQL CompilerResult:
| product_name | price | cost_price | profit_per_unit |
|---|---|---|---|
| Redmi Note 13 Pro | 23999.00 | 19500.00 | 4499.00 |
| boAt Airdopes 141 | 1299.00 | 700.00 | 599.00 |
| Samsung 43" Smart TV | 31990.00 | 26000.00 | 5990.00 |
| Men's Cotton Kurta | 1899.00 | 950.00 | 949.00 |
| Women's Silk Saree | 8499.00 | 5200.00 | 3299.00 |
(5 rows)
The new column only exists in the output. The table itself is not changed. AS profit_per_unit gives the column a readable name (you'll learn more about aliases in the next lesson).
4. Line revenue for each order item
SELECT order_id,
product_id,
quantity,
unit_price,
quantity * unit_price AS gross_amount
FROM order_items
LIMIT 5;Try it in the SQL CompilerResult:
| order_id | product_id | quantity | unit_price | gross_amount |
|---|---|---|---|---|
| 1001 | 101 | 1 | 23999.00 | 23999.00 |
| 1001 | 102 | 2 | 1299.00 | 2598.00 |
| 1002 | 105 | 1 | 8499.00 | 8499.00 |
| 1003 | 103 | 1 | 31990.00 | 31990.00 |
| 1003 | 108 | 3 | 549.00 | 1647.00 |
(5 rows)
5. Constants and expressions without a table
PostgreSQL lets you run SELECT without FROM, which works like a calculator:
SELECT 23999 * 0.18 AS gst_amount, 'ShopKart' AS store;Try it in the SQL CompilerResult:
| gst_amount | store |
|---|---|
| 4319.82 | ShopKart |
(1 row)
How SQL runs a SELECT
Even though you write SELECT first, the database reads FROM first: it finds the table, then picks the columns. This logical order becomes important later:
1. FROM → which table(s)
2. WHERE → which rows
3. GROUP BY → how to group
4. HAVING → which groups
5. SELECT → which columns and calculations
6. ORDER BY → sort
7. LIMIT → how many rows
Common mistakes
Misspelled column names. The error tells you exactly which one:
SELECT product_nme FROM products;Try it in the SQL CompilerError: column "product_nme" does not exist
- Trailing comma before FROM.
SELECT product_name, price, FROM productsis a syntax error. - Using
SELECT *in reports. List columns explicitly: it's clearer and avoids surprises when the table changes.
Practice question
Show each product's name, its price, and its price including 18% GST as a column called price_with_gst.
Show solution
SELECT product_name,
price,
price * 1.18 AS price_with_gst
FROM products
LIMIT 5;Try it in the SQL CompilerResult:
| product_name | price | price_with_gst |
|---|---|---|
| Redmi Note 13 Pro | 23999.00 | 28318.8200 |
| boAt Airdopes 141 | 1299.00 | 1532.8200 |
| Samsung 43" Smart TV | 31990.00 | 37748.2000 |
| Men's Cotton Kurta | 1899.00 | 2240.8200 |
| Women's Silk Saree | 8499.00 | 10028.8200 |
(5 rows)
Key takeaways
SELECTpicks columns;FROMnames the table.*returns all columns; use it for exploration, not reports.- You can calculate new columns with arithmetic and name them with
AS. - The database processes
FROMbeforeSELECT.
Frequently asked questions
What does SELECT * mean in SQL?
SELECT * returns every column of the table. It is handy for exploring a table, but in reports and production code list the columns you need, so the query is faster and does not break when columns are added.
Can SELECT work without FROM?
In PostgreSQL, MySQL and SQL Server, yes. SELECT 2 + 2; returns 4. Oracle needs FROM dual.
Does the order of columns in SELECT matter?
It only changes the order of columns in the output. It does not change which rows are returned.
Keep learning SQL
One email a week with SQL and analytics guides, new practice questions and cohort dates. No spam, unsubscribe anytime.
Prefer a one-page summary? Download the free SQL cheat sheet (PDF)