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 Compiler

Result:

product_nameprice
Redmi Note 13 Pro23999.00
boAt Airdopes 1411299.00
Samsung 43" Smart TV31990.00
Men's Cotton Kurta1899.00
Women's Silk Saree8499.00
Running Shoes2499.00
Prestige Pressure Cooker 5L2199.00
Steel Water Bottle 1L549.00
Non-stick Tawa899.00
Herbal Face Wash199.00
Kumkumadi Face Oil2995.00
The Psychology of Money299.00

(showing 12 of 15 rows)

2. Select all columns with *

SELECT *
FROM categories;
Try it in the SQL Compiler

Result:

category_idcategory_name
1Electronics
2Fashion
3Home & Kitchen
4Beauty
5Books
6Sports

(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 Compiler

Result:

product_namepricecost_priceprofit_per_unit
Redmi Note 13 Pro23999.0019500.004499.00
boAt Airdopes 1411299.00700.00599.00
Samsung 43" Smart TV31990.0026000.005990.00
Men's Cotton Kurta1899.00950.00949.00
Women's Silk Saree8499.005200.003299.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 Compiler

Result:

order_idproduct_idquantityunit_pricegross_amount
1001101123999.0023999.00
100110221299.002598.00
100210518499.008499.00
1003103131990.0031990.00
10031083549.001647.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 Compiler

Result:

gst_amountstore
4319.82ShopKart

(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 Compiler

Error: column "product_nme" does not exist

  • Trailing comma before FROM. SELECT product_name, price, FROM products is 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 Compiler

Result:

product_namepriceprice_with_gst
Redmi Note 13 Pro23999.0028318.8200
boAt Airdopes 1411299.001532.8200
Samsung 43" Smart TV31990.0037748.2000
Men's Cotton Kurta1899.002240.8200
Women's Silk Saree8499.0010028.8200

(5 rows)

Key takeaways

  • SELECT picks columns; FROM names 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 FROM before SELECT.

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.

Newsletter

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)