Learn/SQL/Querying Data

SELECT & FROM — Reading Data

SELECT is the SQL statement you will use most often. It reads rows from a table and returns a result set — a temporary table of answers.

Beginner~12 min read

Query Execution Flow

How SELECT works (logical view)

You name the source with FROM, then project columns with SELECT. The database engine returns matching rows — it does not change stored data.

Sample Data — users Table

users (reference)json
1| user_id | email | is_active |
2|---------|---------------------|-----------|
3| 1 | [email protected] | true |
4| 2 | [email protected] | false |
5| 3 | [email protected] | true |

SELECT Specific Columns

Read email and active flagjson
1SELECT email, is_active
2FROM users;
Expected outputjson
1| email | is_active |
2|---|---|
3| [email protected] | true |
4| [email protected] | false |
5| [email protected] | true |

SELECT * — All Columns

Every columnjson
1SELECT * FROM users;

When to use *

Fine for quick exploration in a SQL client. In apps and reports, list columns explicitly for clarity and performance.

Column Aliases (AS)

Rename output columnsjson
1SELECT
2 user_id AS id,
3 email AS contact_email
4FROM users;
Expected outputjson
1| id | contact_email |
2|---|---|

DISTINCT — Remove Duplicates

Unique values onlyjson
1SELECT DISTINCT is_active
2FROM users;
Expected outputjson
1| is_active |
2|---|
3| true |
4| false |

Common Mistakes

MistakeProblemFix
Missing FROMInvalid syntaxAlways specify a table: FROM users
SELECT * everywhereOver-fetching dataList needed columns
Ambiguous names after JOINError once joins are usedUse table aliases: u.email

Practice Prompts

  1. Write a query that returns only user_id and email from users.
  2. Return unique is_active values using DISTINCT.
  3. Alias email as address in the result.

Frequently Asked Questions

What is the most basic SQL query?
SELECT column FROM table; reads one or more columns from a named table. SELECT * FROM table; returns every column.
Should I use SELECT * in production?
Avoid SELECT * in application code when possible. Explicit columns reduce network transfer, make schemas clearer, and prevent breaks when columns are added.
What does DISTINCT do?
DISTINCT removes duplicate rows from the result set based on the selected column combination.
What is a column alias?
AS renames a column in the output (e.g. SELECT email AS user_email). Useful for readable reports and APIs.
Does SELECT modify data?
No. SELECT only reads data. INSERT, UPDATE, and DELETE modify data.