How to Write SQL Queries: A Complete Beginner's Guide
15 min read · Updated August 13, 2026
This guide assumes you've never written a line of SQL in your life. We'll go slowly, one small idea at a time, and by the end you'll be able to write real queries yourself — not just copy them. Keep the SQL Playground open in another tab so you can type every example as we go. That's the fastest way to make it stick.
1. What is a table, really?
Before touching any SQL, get one picture firmly in your head: a database table looks exactly like a spreadsheet. If you've ever used Excel or Google Sheets, you already understand 90% of what a table is.
- Each column is a category of information — like a spreadsheet column header (
first_name,salary). - Each row is one single record — like one line in the spreadsheet (one employee, one order, one customer).
- A table is the whole grid — all the rows and columns together, usually with a name like
Employees.
Here's the actual Employees table you'll be practicing on — it's already loaded in the Playground:
| emp_id | first_name | last_name | dept_id | salary |
|---|---|---|---|---|
| 1 | Ananya | Rao | 10 | 142000 |
| 2 | Marcus | Bennett | 10 | 118000 |
| 4 | Tomas | Nowak | 20 | 131000 |
(This is a small preview — the real table has 12 rows and a few more columns like email and hire_date.)
2. What is SQL, and why does it exist?
SQL (say it "S-Q-L" or "sequel") stands for Structured Query Language. It's simply the language you use to ask questions about the data in a table, or to change that data. Instead of scrolling through a spreadsheet with your eyes looking for rows that match, you write one sentence-like statement, and the database hands you exactly the rows you asked for — even if the table has ten million rows.
Every SQL sentence you write is called a query, or a statement. The five you'll use constantly are:
| Keyword | What it does |
|---|---|
| SELECT | Read / retrieve data |
| INSERT | Add a new row |
| UPDATE | Change existing rows |
| DELETE | Remove rows |
We'll spend most of this guide on SELECT, because it's what you'll use 90% of the time — and everything else builds on the same ideas.
3. Your very first query
Open the Playground, clear the editor, and type exactly this:
SELECT * FROM Employees;
Press Ctrl + Enter (or click Run Query). Let's break down what you just wrote, word by word:
SELECT— "I want to read some data."*— the asterisk means "every column." Think of it as a wildcard meaning "all of them."FROM Employees— "...and get it from the Employees table.";— the semicolon marks the end of the statement. Always end your queries with one.
Read out loud, this query says: "Select everything, from the Employees table." That's it — you just wrote and ran your first SQL query.
4. Picking only the columns you need
* is convenient, but
in practice you rarely want every column. Instead, name the ones you actually want, separated by commas:
SELECT first_name, last_name, salary
FROM Employees;
Try it. You'll notice the result table now only has three columns instead of nine — you asked for exactly those, and that's exactly what came back. This is the core pattern of every SQL query you'll ever write: SELECT (what you want) FROM (where it lives).
5. Filtering rows with WHERE
So far every query returns all rows. To narrow it down to rows matching a condition, add a WHERE clause after FROM:
SELECT first_name, salary
FROM Employees
WHERE salary > 100000;
Read it as: "Give me first_name and salary, from Employees, but only where salary is greater than 100000." The database checks the condition against every row, one at a time, and only keeps the ones where it's true. A few more examples to try:
WHERE dept_id = 10 -- exactly equal to 10
WHERE salary != 100000 -- not equal to
WHERE active = 1 -- only currently active employees
6. Combining conditions
Use AND when every condition must be true, and OR when just one needs to be:
SELECT first_name, dept_id, salary
FROM Employees
WHERE dept_id = 10 AND salary > 100000;
For the full picture on filtering — including matching text, checking ranges, and lists of values — see our dedicated WHERE clause guide once you're comfortable with the basics here.
7. Sorting your results
By default, rows come back in no particular order. Add ORDER BY to sort them:
SELECT first_name, salary
FROM Employees
ORDER BY salary DESC;
DESC means highest-to-lowest;
drop it (or use ASC) for lowest-to-highest, which is the default.
8. Limiting how many rows you see
Combine ORDER BY with LIMIT to answer questions like "who are the 3 highest-paid employees?":
SELECT first_name, salary
FROM Employees
ORDER BY salary DESC
LIMIT 3;
Notice the order these clauses appear in — this order is fixed and always the same: SELECT → FROM → WHERE → ORDER BY → LIMIT. Writing them in a different order is a syntax error.
9. Summarizing data
Instead of looking at individual rows, you'll often want a summary — a total, an average, a count. These are called aggregate functions:
SELECT COUNT(*) AS total_employees, AVG(salary) AS avg_salary
FROM Employees;
This collapses the whole table into a single summary row. To get a summary per group — like "average salary per department" — add GROUP BY:
SELECT dept_id, COUNT(*) AS headcount, AVG(salary) AS avg_salary
FROM Employees
GROUP BY dept_id;
This is a big enough topic to deserve its own guide — see GROUP BY vs HAVING when you're ready to go deeper.
10. Combining two tables
Real data is usually spread across several tables. For example, Employees stores a dept_id number,
but the actual department name lives in a separate Departments table. A JOIN links them together by that shared column:
SELECT e.first_name, d.dept_name
FROM Employees e
JOIN Departments d ON d.dept_id = e.dept_id;
Here, e and d are
short nicknames (aliases) for the two tables, so we can write e.first_name instead of the
longer Employees.first_name. JOINs are one of the most important
SQL skills — read the full JOINs guide next.
11. Changing data safely
So far we've only read data. These three statements change it — use them carefully, always with WHERE:
-- Add a new row
INSERT INTO Employees (emp_id, first_name, last_name, dept_id, salary, active)
VALUES (13, 'Nadia', 'Petrova', 10, 88000, 1);
-- Change existing rows
UPDATE Employees
SET salary = 95000
WHERE emp_id = 13;
-- Remove rows
DELETE FROM Employees
WHERE emp_id = 13;
The single most important habit to build: before running an UPDATE or DELETE, run the same WHERE condition as a SELECT first, and check the rows it returns. If you forget WHERE entirely, UPDATE and DELETE apply to every row in the table — there's no undo. (In the Playground, this is completely safe to experiment with — just click "Reset" in the sidebar to restore the sample data afterward.)
12. Mistakes every beginner makes
Using double quotes for text
Text values need single quotes: WHERE dept_name = 'Sales', not double quotes.
Writing = NULL instead of IS NULL
NULL means "unknown," so it can never equal anything, even itself. Use IS NULL or IS NOT NULL instead.
Forgetting the clause order
It's always SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. Writing WHERE after ORDER BY, for example, is a syntax error.
Running UPDATE or DELETE without WHERE
Covered above, but worth repeating: no WHERE means every row is affected.
13. Cheat sheet & what to learn next
SELECT column1, column2 -- what you want
FROM table_name -- where it lives
WHERE condition -- which rows
GROUP BY column -- optional: collapse into groups
HAVING group_condition -- optional: filter those groups
ORDER BY column ASC|DESC -- optional: sort it
LIMIT n; -- optional: cap the row count
You now understand every piece of a real SQL query. From here, go deeper in this order:
- The SQL SELECT Statement: A Beginner's Guide — more on aliases and DISTINCT
- The SQL WHERE Clause — every filtering operator, including LIKE and BETWEEN
- SQL JOINs Explained — combining tables properly
- GROUP BY vs HAVING — summarizing data correctly
- SQL Subqueries Explained — queries inside queries
- 25 SQL Interview Questions — test what you've learned
Now go practice
Reading is step one. Open the Playground and retype every example in this guide yourself, from Step 3 onward — that repetition is what actually makes it stick.
Open the Playground