Skip to content

SELECT and FROM

How SELECT and FROM shape a query and its result set.

Builds on Basic SQL Commands

Stage 1intro6 min read

This lesson focuses on SELECT queries, but not every SQL statement starts with SELECT and FROM. The familiar FROM-before-SELECT sequence is a useful logical-processing model, not a promise about the engine's physical execution.

SELECT and FROM (Deep Dive)


1. Introduction

SELECT and FROM are the most fundamental SQL commands.

Every SQL query starts with them.

They are the foundation of reading data from a database.

Before learning filtering, sorting, grouping, or joins, you must clearly understand how SELECT and FROM work.

These two commands answer the first two questions of every SQL query:

  1. What data do I want to see?
  2. Where should SQL read it from?

2. What is SELECT?

SELECT tells SQL which columns you want to retrieve.

Without SELECT, SQL does not know what data you want to view.

It defines the output of your query.

You use SELECT to:

  • fetch one column
  • fetch multiple columns
  • fetch all columns
  • calculate values
  • create derived output

3. What is FROM?

FROM tells SQL which table to read the data from.

Without FROM, SQL does not know where the data lives.

It tells SQL the source of the data.

You use FROM to:

  • choose the source table
  • tell SQL where to look
  • define the dataset being queried

4. Basic Syntax

SELECT column_name
FROM table_name;

This is the most basic SQL query structure.


5. SELECT All Columns

Query

SELECT *
FROM countries;

Explanation

The * (asterisk) means:

Select all columns

This tells SQL:

  • go to the countries table
  • return every column
  • return every row

This is useful when:

  • exploring a new table
  • checking table structure
  • debugging quickly

Output

countrypopulationarearegion
India14000000003287000Asia
China12000000004287000Asia
Brazil2140000008516000South America
Australia257000007692000Oceania
USA3310000009834000North America

Why SELECT * Should Be Used Carefully

SELECT * is useful for exploration, but not ideal in real applications.

Why?

Because if a table has 50 columns and your app only needs 2, SQL still fetches all 50.

This causes:

  • unnecessary memory usage
  • more network transfer
  • slower queries
  • harder-to-read results

Best practice:

Use SELECT * for learning and quick checks.

Use explicit column names in real projects.


6. SELECT Specific Columns

Query

SELECT country, population
FROM countries;

Explanation

This tells SQL:

  • go to countries
  • return only country
  • return only population

Now the result is smaller, cleaner, and more focused.

This is the habit every developer should build:

Only select what you need.


Output

countrypopulation
India1400000000
China1200000000
Brazil214000000
Australia25700000
USA331000000

7. Deep Dive — How SELECT and FROM Actually Work

Even though we write:

SELECT ...
FROM ...

SQL does not execute it in that written order.

This is one of the most important concepts beginners miss.


8. SQL Execution Order

We read SQL left to right.

But SQL processes it differently.

Actual internal execution order:

  1. FROM
  2. SELECT

Why?

Because SQL must first find the table before it can select columns from it.

This means SQL thinks like this:

  1. Go to the table
  2. Open the data source
  3. Find requested columns
  4. Return result

Mental Model

Imagine a filing cabinet.

  • FROM = open the correct drawer
  • SELECT = pull out only the documents you need

SQL cannot select anything until it first knows where to look.

That is why FROM logically happens before SELECT.


9. Real-World Example — products Table

Let us use a more practical example.

Imagine you manage an online electronics store.

You have a products table:

product_idproduct_namecategorypricestock_quantity
101Gaming MouseAccessories50100
102Mechanical KeyboardAccessories12050
103Curved MonitorElectronics30020
104Ergonomic ChairFurniture25010
105USB-C CableCables15200

10. Case 1 — SELECT * (Full Inventory)

Query

SELECT *
FROM products;

Explanation

Return every row and every column from the inventory table.

This is useful for:

  • understanding table structure
  • checking sample data
  • exploring a dataset quickly

Important Note

This is good for learning and debugging.

Avoid it in production unless you truly need all columns.


11. Case 2 — Controlling Column Order

When selecting specific columns, SQL returns them in the exact order you write them.

The original table order does not matter.


Query

SELECT price, product_name
FROM products;

Explanation

Even though product_name appears before price in the table, SQL returns:

  1. price
  2. product_name

Because SQL follows the order written in SELECT.


Output

priceproduct_name
50Gaming Mouse
120Mechanical Keyboard
300Curved Monitor
250Ergonomic Chair
15USB-C Cable

12. Case 3 — Performing Calculations in SELECT

A very important concept:

SELECT does not only fetch stored values.

It can also calculate new values on the fly.

This is one of SQL’s most powerful features.


Query

SELECT product_name, price * stock_quantity AS total_value
FROM products;

Explanation

For each row, SQL calculates:

price * stock_quantity

This creates a new computed column.

Then AS total_value gives that column a readable name.


Output

product_nametotal_value
Gaming Mouse5000
Mechanical Keyboard6000
Curved Monitor6000
Ergonomic Chair2500
USB-C Cable3000

Why This Matters

SQL can calculate values while reading data.

This is heavily used in:

  • reports
  • dashboards
  • analytics
  • finance
  • inventory systems

13. Case 4 — Adding Static Labels

You can also create fixed/static columns in the result.

This means SQL prints the same value for every row.


Query

SELECT product_name, 'In Warehouse' AS status
FROM products;

Explanation

For every row:

  • fetch product_name
  • add a constant label 'In Warehouse'

This is useful for:

  • reports
  • exports
  • status labels
  • temporary flags

Output

product_namestatus
Gaming MouseIn Warehouse
Mechanical KeyboardIn Warehouse
Curved MonitorIn Warehouse
Ergonomic ChairIn Warehouse
USB-C CableIn Warehouse

14. Common Mistakes to Avoid

1. Missing Comma

Incorrect:

SELECT name age FROM users;

Correct:

SELECT name, age FROM users;

Columns must be separated by commas.


2. Extra Comma Before FROM

Incorrect:

SELECT name, age, FROM users;

Correct:

SELECT name, age FROM users;

Do not place a comma before FROM.


3. Selecting Too Much Data

Avoid selecting unnecessary columns.

Only fetch what is needed.

This improves:

  • readability
  • performance
  • memory usage

4. Ignoring Column Order

SQL returns columns in the order written in SELECT, not in table order.

Always control output intentionally.


15. Summary

SELECT

Defines what data to return.

FROM

Defines where SQL should read from.

SQL Execution Order

SQL logically processes:

  1. FROM
  2. SELECT

Key Takeaways

  • SELECT * returns everything
  • explicit columns are better
  • column order is controlled by SELECT
  • SELECT can calculate values
  • SELECT can add fixed labels
  • FROM always defines the source table

Final Mental Model

When SQL sees:

SELECT column_name
FROM table_name;

It thinks:

  1. Go to the table
  2. Open the data
  3. Pick the requested columns
  4. Return the result

In this topic

Done with this topic?

How well do you know it?

PreviousBasic SQL Commands