What You Will Learn
- What SQL is and what a query is
- The simplest possible SQL query:
SELECT ... FROM ... - How to select one column, several columns, or all columns
- How to read a query like a sentence
Why This Topic Matters
SQL is the language of every relational database — PostgreSQL, MySQL, SQLite, SQL Server, Oracle, BigQuery, Snowflake. Once you can write SELECT, you can read data from any of them. This lesson is the door. Behind it is the rest of database analytics.
What Is SQL?
SQL (Structured Query Language) is a language for talking to databases. You write a short command — a query — and the database returns data. SQL has been around since the 1970s and is the second most-used programming language in the world (after JavaScript, by some measures). It is not optional for an analyst.
The Sample Table
Imagine a table called orders in your database:
| order_id | customer | city | amount |
|---|---|---|---|
| 001 | Anita | Mumbai | 500 |
| 002 | Ravi | Chennai | 1200 |
| 003 | Mira | Kolkata | 750 |
| 004 | Sam | Mumbai | 900 |
Your First Query — One Column
SELECT customer
FROM orders;
Result:
| customer |
|---|
| Anita |
| Ravi |
| Mira |
| Sam |
Line by line
SELECT customer— "Give me the customer column".FROM orders— "From the table called orders".;— Ends the query. Always include it.
Read the query aloud: "Select customer from orders." That sentence is the meaning.
Select Multiple Columns
SELECT customer, city, amount
FROM orders;
Comma-separate the column names. The result has those three columns in the order you wrote them. order_id is omitted because you did not ask for it.
Select All Columns — The Star (*)
SELECT *
FROM orders;
The * means "all columns". The result is the entire table. This is useful when exploring a table for the first time, but avoid SELECT * in production queries — it returns columns you may not need, slowing the query and confusing readers.
SELECT with Expressions
You can do math in the SELECT clause:
SELECT customer, amount * 2
FROM orders;
Returns each customer with their amount doubled. Useful for "what-if" calculations.
SELECT with Aliases (Renaming Columns)
SELECT customer AS name, amount AS amount_inr
FROM orders;
The result columns are now called name and amount_inr instead of customer and amount. Aliases make output cleaner, especially for computed columns:
SELECT customer, amount * 0.10 AS tax_inr
FROM orders;
DISTINCT — Unique Values Only
SELECT DISTINCT city
FROM orders;
Returns each city only once: Mumbai, Chennai, Kolkata. (Without DISTINCT, Mumbai would appear twice because two orders came from there.)
How to Actually Run a Query
You need a SQL engine. Easiest options for learning:
- SQLite Online (sqliteonline.com) — paste schema, write SQL, run in the browser. Free.
- DB Fiddle (db-fiddle.com) — similar, supports PostgreSQL and MySQL.
- Install SQLite locally — a single executable, no server, perfect for practice.
- Python + sqlite3 — built into Python, no install needed. See lesson 25.
Every example in this phase runs on all of these without changes, unless noted.
Reading a Query Like a Sentence
SQL reads top-to-bottom like an English sentence:
| SQL clause | English |
|---|---|
| SELECT ... | "Give me these columns..." |
| FROM ... | "...from this table..." |
| WHERE ... | "...but only rows where this is true..." |
| GROUP BY ... | "...grouped by these columns..." |
| HAVING ... | "...keeping only groups where this is true..." |
| ORDER BY ... | "...sorted by these columns..." |
| LIMIT ... | "...and only the top N rows." |
The remaining lessons in this phase each add one clause to your toolkit.
SQL Is Case-Insensitive (But Be Consistent)
select customer from orders; -- works
SELECT CUSTOMER FROM ORDERS; -- also works
Select Customer From Orders; -- also works
Convention: write SQL keywords in UPPERCASE and table/column names in lowercase. This makes queries easier to read because the keywords stand out.
Common Mistakes
- Forgetting the semicolon. Some tools require it; others are forgiving. Always include it.
- Misspelling column or table names. SQL is strict —
custerinstead ofcustomergives an error. Check the schema. - Using
SELECT *in shared queries. It returns every column, including ones you do not need. Slows queries and confuses readers. - Forgetting commas between columns.
SELECT customer cityis parsed asSELECT customer AS city— a confusing alias. Always comma-separate. - Quoting column names incorrectly. Double quotes (
"customer") in standard SQL refer to a column name; single quotes ('customer') refer to a text value. They are different.
Practical Exercise (10 minutes)
- Go to sqliteonline.com.
- Create the sample
orderstable with this SQL:CREATE TABLE orders ( order_id TEXT, customer TEXT, city TEXT, amount INTEGER ); INSERT INTO orders VALUES ('001','Anita','Mumbai',500), ('002','Ravi','Chennai',1200), ('003','Mira','Kolkata',750), ('004','Sam','Mumbai',900); - Run
SELECT * FROM orders;— confirm you see all 4 rows. - Run
SELECT customer, amount FROM orders;— confirm you see 2 columns. - Run
SELECT DISTINCT city FROM orders;— confirm 3 unique cities.
Mini Challenge
Add a 5th order to the table with a city you have not used yet. Then write a query that returns only the customer names and their amounts, sorted by amount largest first. (Hint: you will need ORDER BY — see lesson 19. If you cannot wait, try SELECT customer, amount FROM orders ORDER BY amount DESC;.)
Key Takeaways
SELECT <columns> FROM <table>;is the simplest query.- Comma-separate columns. Use
*for all columns (sparingly). ASrenames columns in the output.DISTINCTreturns unique values.- SQL keywords are uppercase by convention; case does not matter to the engine.
Previously learned: Phase 3 (lessons 12–16) covered spreadsheet analytics. Many SQL concepts mirror Excel: columns are columns, rows are rows, filtering is filtering.
Today: You wrote your first SQL query.
Next: Lesson 18 adds the
WHERE clause — the SQL version of Excel's filter.
FAQ
Do I need to install a database to learn SQL?
No. Browser-based tools like sqliteonline.com or db-fiddle.com let you write and run SQL with zero installation. For this course, those are enough. Once you start working with company data, you will connect to whatever database the company uses.
Is SQL the same in every database?
Mostly yes. The core (SELECT, WHERE, GROUP BY, JOIN) works identically across PostgreSQL, MySQL, SQLite, SQL Server, and Oracle. Advanced features (window functions, date functions, full-text search) vary by dialect. This course uses standard SQL that works everywhere.
Comments
Comments
Post a Comment