Query Execution Flow
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_active2FROM 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
1SELECT2 user_id AS id,3 email AS contact_email4FROM users;Expected outputjson
1| id | contact_email |2|---|---|3| 1 | [email protected] |4| 2 | [email protected] |5| 3 | [email protected] |DISTINCT — Remove Duplicates
Unique values onlyjson
1SELECT DISTINCT is_active2FROM users;Expected outputjson
1| is_active |2|---|3| true |4| false |Common Mistakes
| Mistake | Problem | Fix |
|---|---|---|
| Missing FROM | Invalid syntax | Always specify a table: FROM users |
| SELECT * everywhere | Over-fetching data | List needed columns |
| Ambiguous names after JOIN | Error once joins are used | Use table aliases: u.email |
Practice Prompts
- Write a query that returns only
user_idandemailfromusers. - Return unique
is_activevalues using DISTINCT. - Alias
emailasaddressin the result.
Try These Tools
Continue Learning
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.