Chapter 1 of ?
Database 12 min read •
Sanjay
Written by Sanjay

SQL Tutorial for Beginners — Chapter 5: SQL SELECT Statement

Chapter 5: SQL SELECT Statement

The SELECT statement is the foundational command of relational databases. Learn how to query tables, choose specific columns, use wildcards, calculate arithmetic expressions on the fly, and inspect output datasets without altering underlying data.

Read-Only Query

Safe Data Inspection
SELECT reads and projects records from storage without modifying, locking, or mutating disk records.

Column Selection

Targeted Projection
Specify the exact attributes needed in any custom sequence to reduce network overhead and memory consumption.

Wildcard Asterisk

Rapid Exploration
Use SELECT * for quick ad-hoc table inspection, and list columns explicitly in production systems.

Inline Math

Computed Columns
Calculate discounts, tax amounts, unit conversions, and tableless math expressions in real-time.

Curriculum
Module 2: Querying Data (Ch 5 of 82)
Estimated Time
12 mins read & practice
Prerequisites
Chapter 4: SQL Syntax
Execution Engine
SQLite WASM (In-Browser)

5.1 What Does the SELECT Statement Do?

The SELECT statement is the primary query tool in SQL. It falls into the DQL (Data Query Language) category. Its single responsibility is to read stored records from one or more database tables and project them as a two-dimensional result dataset.

Guarantee: SELECT Never Mutates Your Data

You can safely run SELECT queries as many times as you like. It will never overwrite, alter, insert, or delete any record stored in your database.

Standard SELECT Syntax
SELECT column1, column2, ... FROM table_name;
SELECT * FROM table_name;

For example, to retrieve all records and columns from the Customers table:

SELECT * FROM Customers;

5.2 Visual: Which Columns Does SELECT Pick?

Step through the visual lifecycle below to observe how the SQL query engine filters and projects columns from the Customers table:

Which Columns Does SELECT Pick? Interactive 5-step diagram showing how SELECT * keeps all columns, SELECT CustomerName, City filters specific columns, and changing order swaps output columns. SQL QUERY: SELECT * FROM Customers; STEP 1 OF 5 Table: Customers (5 Columns on disk) CustomerID CustomerName ContactName City Country 1 Aarav Sharma Aarav Mumbai India 2 Aditi Rao Aditi Bengaluru India 3 Vikram Malhotra Vikram Delhi India 4 Priya Nair Priya Kochi India ... 4 additional records stored in table ...
Step 1: The full Customers table stored on disk with all 5 columns and 8 records.
Figure 5.1: Interactive Column Selection Pipeline (Pattern B) — step through how SELECT picks, filters, and reorders table columns.

5.3 Selecting Specific Columns

In production software and professional data analysis, you should specify only the exact columns you intend to use. Separate each column name with a comma:

Query Customers (Name & City)
SELECT CustomerName, City
FROM Customers;
Try It Yourself »
CustomerNameCity
Aarav SharmaMumbai
Priya NairKochi
Liam CarterToronto
Sofia RossiMilan
... 4 more rows (8 total)
Query Products (Name & Price)
SELECT ProductName, Price
FROM Products;
Try It Yourself »
ProductNamePrice
Wireless Mouse₹799
Mechanical Keyboard₹3,499
USB-C Hub₹1,999
27-inch Monitor₹15,999
... 4 more rows (8 total)

Notice: The column order in your output table matches the exact sequence specified in your SELECT clause. Requesting SELECT City, CustomerName yields City first!


5.4 Selecting All Columns with the Wildcard (*)

The asterisk (*) is the universal SQL wildcard symbol meaning "all columns in their physical table schema order".

SELECT * FROM Customers;
Try It Yourself »
When SELECT * is Great
  • Quick schema exploration and inspecting new database tables.
  • Debugging query filters in local development sandboxes.
  • One-off ad-hoc data analysis scripts and interactive notebooks.
Why Avoid in Production Code
  • Transfers unneeded columns across network connections, slowing APIs.
  • Cannot leverage index-only scans (covering indexes) efficiently.
  • Adding or dropping columns later can break client code positional bindings.
Attribute SELECT * (Wildcard) SELECT col1, col2 (Explicit Columns)
Network Transfer Transfers every column in table (high bandwidth) Transfers only requested attributes (lightweight & fast)
Schema Change Resilience Fragile; column additions alter positional mapping Robust; application code only receives expected fields
Database Index Utilization Rarely uses index-only covering scans Can utilize high-speed covering indexes directly
Best Use Case Interactive exploratory data analysis & debugging Production APIs, ETL pipelines & reporting dashboards

5.5 Expressions and Calculations in SELECT

SQL can compute arithmetic calculations and string expressions directly within the SELECT clause. You can perform calculations using numeric columns, constants, and math operators (+, -, *, /):

-- Calculate a 10% promotional discount on all products
SELECT ProductName, Price, Price * 0.9
FROM Products;
Try It Yourself »

Preview: Column Aliases with AS (Chapter 16)

Notice that the computed column displays the header Price * 0.9. In Chapter 16, you will learn how to assign a friendly label using the AS keyword, such as SELECT Price * 0.9 AS DiscountedPrice.

You can also execute tableless calculations where no database table is queried:

SELECT 2 + 3;
Try It Yourself »
Dialect Compatibility Note: Tableless SELECT 2 + 3; runs natively across SQLite, MySQL, PostgreSQL, and Microsoft SQL Server. Older Oracle Database systems require specifying the dummy table FROM DUAL (e.g. SELECT 2 + 3 FROM DUAL;).

5.6 Upcoming Capabilities: You Will Learn These Soon

DISTINCT (Chapter 6)

Eliminate duplicate values: SELECT DISTINCT City FROM Customers;

WHERE (Chapter 7)

Filter rows matching conditions: WHERE Country = 'India'

ORDER BY (Chapter 8)

Sort resulting records: ORDER BY Price DESC

UPPER() (Chapter 11)

Transform text in queries: SELECT UPPER(CustomerName)


5.7 Interactive Practice: Try It Yourself

Execute real queries against our in-browser SQLite database (Customers, Products, Orders):

SQL Interactive Sandbox (SQLite WASM) Database Ready
Open in Playground »
Ctrl + Enter to execute
Result Output: Ready
Click "Run SQL" to execute the query.
Sample Database Tables: Customers (CustomerID, CustomerName, City, Country) • Products (ProductID, ProductName, Category, Price) • Orders (OrderID, CustomerID, ProductID, Quantity, OrderDate)

5.8 Common SELECT Mistakes & Real Error Messages

Below are 6 real SQLite errors beginners encounter with SELECT and how to correct them:

1. Misspelled Column Name
-- ❌ Error: no such column: CustomerNam
SELECT CustomerNam FROM Customers;
-- ✅ Fixed:
SELECT CustomerName FROM Customers;
2. Misspelled Table Name
-- ❌ Error: no such table: Costumers
SELECT * FROM Costumers;
-- ✅ Fixed:
SELECT * FROM Customers;
3. Forgetting Comma Between Columns

Omitted commas are silently interpreted as column aliases!

-- ❌ Mistake: Returns names under column header "City"
SELECT CustomerName City FROM Customers;
-- ✅ Fixed:
SELECT CustomerName, City FROM Customers;
4. Trailing Comma Before FROM
-- ❌ Error: near "FROM": syntax error
SELECT CustomerName, City, FROM Customers;
-- ✅ Fixed:
SELECT CustomerName, City FROM Customers;
5. Forgetting the FROM Clause
-- ❌ Error: no such column: CustomerName
SELECT CustomerName;
-- ✅ Fixed:
SELECT CustomerName FROM Customers;
6. Quoting Column Names

Single quotes produce a literal string repeated for every row!

-- ❌ Mistake: Prints the text 'CustomerName' 8 times
SELECT 'CustomerName' FROM Customers;
-- ✅ Fixed:
SELECT CustomerName FROM Customers;

5.9 Hands-On Practice Exercises

Solve each challenge using the sandbox above, or click "Insert into Sandbox":

Challenge 1: Display All Product Records

Write a query to retrieve every column and row from the Products catalog.

SELECT * FROM Products;
Open in Playground »
Challenge 2: Specific Product Catalog Columns

Retrieve only the ProductName and Category for all items in the Products table.

SELECT ProductName, Category FROM Products;
Open in Playground »
Challenge 3: Computed Order Quantities

Select OrderID, Quantity, and double the quantity (Quantity * 2) from the Orders table.

SELECT OrderID, Quantity, Quantity * 2 FROM Orders;
Open in Playground »

5.11 Key Takeaways

  • SELECT reads data from tables into a result set without altering or deleting any underlying records.
  • List specific column names separated by commas (SELECT CustomerName, City) to return only necessary data.
  • Output column order strictly reflects the order written in the SELECT clause.
  • The asterisk (*) returns every column; use it for exploring tables, but prefer named columns in production.
  • SQL can compute arithmetic calculations (Price * 0.9) and tableless expressions (SELECT 2 + 3;).

5.12 Frequently Asked Questions

What does SELECT do?
SELECT reads data from one or more database tables and returns it as a two-dimensional tabular result without modifying or deleting the underlying stored data.
What does SELECT * mean?
The asterisk (*) is the wildcard character in SQL that instructs the database engine to retrieve every column defined on the table in its original schema order.
Can I choose the order of columns in the result?
Yes! The order of columns in the query output precisely mirrors the comma-separated sequence you write in your SELECT statement.
Does SELECT change my data?
No. SELECT belongs to Data Query Language (DQL) and is strictly a read-only operation. It never alters, inserts, updates, or deletes stored rows.
Is SELECT * bad practice?
SELECT * is convenient for exploratory data analysis, but in production applications and analytical pipelines, naming specific columns is best practice because it reduces unnecessary network data transfer and protects code against schema changes.
Sanjay
Tutorial Author
Sanjay · Full-Stack Developer & AI Enthusiast
Pursuing MCA at Manipal University Jaipur. Founder of AICodeLab.
Done with this chapter?
Mark it complete to track your progress and unlock your certificate.
Next Up
—

Learner Reviews

Write a Review
Share your experience to help other learners.
Your Rating *
★ ★ ★ ★ ★