SQL Queries Explained: A Beginner’s Guide to Reading and Changing Your Data
The first time someone showed me a database, I felt a little intimidated. Rows and columns of information, sitting there silently, and apparently I was supposed to “talk” to it somehow. It turns out that conversation has a name: SQL. And once I understood that SQL queries are just plain instructions you give to a database, the fear melted away. If you run a website, manage an online store, or just want to understand what’s happening behind the scenes of your app, learning a few SQL queries is one of the most empowering skills you can pick up. Let me walk you through it the way I wish someone had walked me through it.
Key Takeaways
• A SQL query is a command you send to a database to read or change its data.
• Four verbs cover almost everything: SELECT (read), INSERT (add), UPDATE (change), DELETE (remove).
• SELECT does the heavy lifting, paired with `WHERE` (filter), `ORDER BY` (sort), and `LIMIT` (cap results).
• Always add a `WHERE` clause to UPDATE and DELETE, or you risk changing every row.
• You can run queries safely through phpMyAdmin, the command line, or directly from your application.
What exactly is a SQL query?
A SQL query is a single command, written in a language called SQL (Structured Query Language), that tells a database what you want it to do. Think of it as a polite, very literal request. You’re either *asking to see* some data, or *asking to change* some data. That’s the whole job.
SQL is the standard language that databases like MySQL and MariaDB understand. Whether your data lives in a list of customers, a catalog of products, or a log of orders, you use the same handful of query patterns to interact with all of it. The reason this matters so much is that your website’s content, user accounts, and settings almost certainly live in a database. Knowing how to query it means you can answer questions, fix problems, and pull reports without waiting on anyone else.
Every query follows a readable, almost English-like structure. Here’s the simplest one imaginable:
“`sql SELECT * FROM customers; “`
That says: “Show me everything from the customers table.” The semicolon at the end marks the end of the statement. Run it, and you might see:
“`text +—-+—————-+———————+
| id | name | email | +—-+—————-+———————+
| 1 | Ana Silva | [email protected] |
| 2 | Marco Reyes | [email protected] |
| 3 | Priya Nair | [email protected] | +—-+—————-+———————+ “`
That’s it. You asked a question, the database answered. Now let’s build on that foundation. For the bigger picture of how these databases are set up and managed, see the complete guide to MySQL and MariaDB databases for website owners.
What are the core types of SQL queries?
Nearly every basic SQL query falls into one of four categories. People sometimes call these the CRUD operations: Create, Read, Update, Delete. Here’s how each maps to a SQL verb.
| Query type | What it does | Plain-English purpose |
|---|---|---|
| `SELECT` | Reads data | “Show me information” |
| `INSERT` | Adds data | “Add a new record” |
| `UPDATE` | Changes data | “Edit an existing record” |
| `DELETE` | Removes data | “Delete a record” |
Let’s look at each one with real examples.
How do I read data with SELECT?
SELECT is the query you’ll use most, by far. Reading data is far more common than changing it, so this is where your time pays off. The basic shape is: choose your columns, name your table, then optionally filter, sort, and limit.
“`sql SELECT name, email FROM customers; “`
This returns just the two columns you asked for, instead of everything:
“`text +—————-+———————+
| name | email | +—————-+———————+
| Ana Silva | [email protected] |
| Marco Reyes | [email protected] |
| Priya Nair | [email protected] | +—————-+———————+ “`
Now let’s make it useful. Suppose you want only customers from a particular city. You add a `WHERE` clause to filter:
“`sql SELECT name, email FROM customers WHERE city = ‘Lisbon’; “`
The `WHERE` clause is your filter. Only rows that match the condition come back. You can combine conditions with `AND` and `OR`:
“`sql SELECT name, email FROM customers WHERE city = ‘Lisbon’ AND signup_year = 2026; “`
There are a few more filtering tools worth knowing. `LIKE` matches patterns (the `%` is a wildcard for “anything”), `IN` matches a list of values, and `BETWEEN` matches a range:
“`sql SELECT name FROM customers WHERE email LIKE ‘%@gmail.com’; SELECT name FROM customers WHERE city IN (‘Lisbon’, ‘Porto’, ‘Braga’); SELECT name FROM orders WHERE total BETWEEN 50 AND 200; “`
To control the order of your results, add `ORDER BY`. To cap how many rows come back, add `LIMIT`:
“`sql SELECT name, total FROM orders ORDER BY total DESC LIMIT 5; “`
That query says: “Show me the five biggest orders, highest first.” (`DESC` means descending; `ASC` is ascending and is the default.) The result:
“`text +—————-+——–+
| name | total | +—————-+——–+
| Priya Nair | 480.00 |
| Marco Reyes | 312.50 |
| Ana Silva | 199.99 |
| Joao Costa | 150.00 |
| Lena Fischer | 142.00 | +—————-+——–+ “`
How do I add data with INSERT?
When you need to add a new row, INSERT is your verb. You name the table, list the columns, and provide the matching values:
“`sql INSERT INTO customers (name, email, city) VALUES (‘Diego Alves’, ‘[email protected]’, ‘Madrid’); “`
Run it, and the database confirms:
“`text Query OK, 1 row affected (0.01 sec) “`
A new customer now exists. The column list and the value list have to line up in the same order, which is the only thing people usually trip over here.
How do I change data with UPDATE?
UPDATE changes existing rows. This is where I want you to slow down and pay attention, because it’s the first query that can cause real damage. The shape is: name the table, set the new values, and filter with WHERE so you only change the rows you mean to.
“`sql UPDATE customers SET city = ‘Barcelona’ WHERE id = 4; “`
This updates only the customer whose `id` is 4. Now here is the cautionary tale every database person learns once: if you forget the `WHERE` clause, the database happily updates *every single row*.
“`sql — DANGER: this changes the city of EVERY customer UPDATE customers SET city = ‘Barcelona’; “`
I tell every person I mentor the same thing: write your `WHERE` clause first, before you even write the `SET`. It’s a small habit that has saved me from more than one bad afternoon.
How do I remove data with DELETE?
DELETE removes rows, and it carries the exact same warning as UPDATE. Always include a `WHERE` clause.
“`sql DELETE FROM customers WHERE id = 4; “`
That removes one specific customer. Leave off the `WHERE`, and you’ve emptied the entire table. There’s no undo button on a live query, which is why we’ll talk about safe habits in a moment.
Here is the most reassuring thing I can tell a nervous beginner: nearly all the SQL most people ever need is just those four verbs, SELECT, INSERT, UPDATE, and DELETE, the CRUD operations. And of those four, SELECT does the vast majority of the real work, because reading data is far more common than changing it. So the highest-leverage SQL skill isn’t memorizing exotic functions or clever tricks. It’s becoming genuinely fluent at SELECT with its small handful of modifiers: `WHERE` to filter, `ORDER BY` to sort, `LIMIT` to cap, `JOIN` to combine tables, and `GROUP BY` to summarize. Master SELECT, and you can answer almost any question your data holds. The writing verbs, INSERT, UPDATE, and DELETE, are simpler and used far less often. Don’t be intimidated by how enormous SQL appears from the outside. You will live in one small, friendly corner of it about 90 percent of the time.
How do I summarize data with aggregate queries?
Once you’re comfortable reading rows, you’ll want to *summarize* them. That’s what aggregate functions do. Instead of returning individual rows, they crunch many rows into a single answer. The four you’ll reach for constantly are `COUNT`, `SUM`, `AVG`, and pairing them with `GROUP BY`.
Want to know how many customers you have?
“`sql SELECT COUNT(*) FROM customers; “`
“`text +———-+
| COUNT(*) | +———-+
| 247 | +———-+ “`
Want the total revenue and the average order value?
“`sql SELECT SUM(total) AS revenue, AVG(total) AS avg_order FROM orders; “`
“`text +———-+———–+
| revenue | avg_order | +———-+———–+
| 18420.50 | 74.58 | +———-+———–+ “`
The `AS` keyword renames a column in the output so it’s easier to read. Now the real power move: `GROUP BY` lets you summarize *per category*. Say you want the number of orders and total spend for each customer:
“`sql SELECT customer_id, COUNT(*) AS order_count, SUM(total) AS spent FROM orders GROUP BY customer_id ORDER BY spent DESC LIMIT 3; “`
“`text +————-+————-+——–+
| customer_id | order_count | spent | +————-+————-+——–+
| 7 | 12 | 940.00 |
| 3 | 8 | 612.50 |
| 15 | 5 | 488.00 | +————-+————-+——–+ “`
In one query, you’ve found your top three customers by spend. This is the kind of question that used to require exporting spreadsheets and fiddling with formulas. With SQL, it’s five lines.
How do I combine tables with JOINs?
Real databases split information across multiple tables to avoid repeating data. Your `orders` table might store a `customer_id` instead of the full customer name, because the name already lives in the `customers` table. A JOIN stitches them back together when you need them in one result.
The most common JOIN is an `INNER JOIN`, which returns rows that have a match in both tables:
“`sql SELECT customers.name, orders.total FROM orders INNER JOIN customers ON orders.customer_id = customers.id ORDER BY orders.total DESC LIMIT 3; “`
“`text +————-+——–+
| name | total | +————-+——–+
| Priya Nair | 480.00 |
| Marco Reyes | 312.50 |
| Ana Silva | 199.99 | +————-+——–+ “`
Now the order shows the customer’s actual name instead of a numeric ID. A `LEFT JOIN` is the other one you’ll meet often. It returns *all* rows from the first table, even those without a match in the second, which is handy for finding, say, customers who have never placed an order. JOINs go deeper than this, but knowing INNER and LEFT covers most everyday needs, so don’t feel you have to master them all at once.
How do I keep my queries safe?
This is the part I care about most as a mentor, because a confident beginner with a `DELETE` statement and no safety net makes me nervous. A few simple habits will protect you.
Always write a WHERE clause for UPDATE and DELETE. I said it earlier, but it’s worth repeating because it’s the single most common cause of accidental data loss.
Test with SELECT first. Before you run an UPDATE or DELETE, run the *same WHERE clause* as a SELECT to see exactly which rows you’re about to affect:
“`sql — Step 1: check what you’re about to change SELECT * FROM customers WHERE city = ‘Lisbom’;
— Step 2: only if the rows look right, run the real change UPDATE customers SET city = ‘Lisbon’ WHERE city = ‘Lisbom’; “`
Notice the typo I left in there on purpose. Running the SELECT first would show you zero rows, telling you something is off *before* you change anything. That two-second check has saved me countless times.
Work on a copy or a backup when experimenting. If you’re learning or testing a tricky query, do it where a mistake costs you nothing.
Where do I actually run SQL queries?
You have three practical places to type and run your queries, and you can pick whichever feels most comfortable.
The friendliest option for most people is phpMyAdmin, a visual tool that lets you browse tables and run SQL in a text box, then see results in a tidy grid. It’s perfect when you’re starting out. If you’d like a walkthrough, see our guide on managing databases through that interface.
The second option is the command line (CLI). On a VPS or dedicated server, you log into MySQL or MariaDB and type queries directly. It’s fast and scriptable once you’re comfortable.
The third place is from within your application’s code, where your website runs queries automatically. You usually won’t write those by hand day-to-day, but understanding SQL helps you debug them when something breaks.
Build the right foundation for your database
DarazHost hosting includes MySQL and MariaDB databases with phpMyAdmin built in, so you can write and run SQL queries through a friendly visual interface, or use the CLI on VPS and dedicated plans when you want more control. Fast SSD storage means your queries return results quickly, and automatic backups mean experimenting is safe, because a mistake is never permanent. You get the database tools to learn and genuinely use SQL, backed by 24/7 support whenever you get stuck.
How do I practice and build confidence?
The honest answer is that you learn SQL by running queries, not by reading about them. Start with SELECT. Pull a list of records, then add a `WHERE` filter, then sort it with `ORDER BY`, then cap it with `LIMIT`. Once that feels natural, try a `COUNT` or a `GROUP BY` to answer a real question about your data, like “how many orders came in last month?” Save the writing queries (INSERT, UPDATE, DELETE) for when reading feels comfortable, and always test them with a SELECT first.
Within a week of daily practice, you’ll find yourself answering questions in seconds that used to require help. That shift, from feeling locked out of your own data to feeling at home in it, is the whole point. You don’t need to memorize everything. You need to be fluent in the handful of patterns you’ll use every day, and confident enough to look up the rest when a rare case comes along.
Frequently asked questions
Do I need to know programming to write SQL queries? No. SQL is a query language, not a programming language in the traditional sense. If you can write a sentence describing what you want (“show me customers from Lisbon”), you can learn to write it in SQL. It reads almost like structured English.
What’s the difference between SQL and MySQL? SQL is the language. MySQL (and MariaDB) is the database software that understands and runs that language. You write SQL queries; MySQL executes them. The same SQL skills transfer across most database systems.
Are SQL queries case sensitive? SQL keywords like `SELECT` and `WHERE` are not case sensitive, so `select` works the same as `SELECT`. Many people write keywords in uppercase by convention for readability. However, table and column names *can* be case sensitive depending on your server’s configuration, so it’s safest to match their exact spelling.
What happens if I run a DELETE without a WHERE clause? The database deletes every row in the table. There is no confirmation prompt and no built-in undo. This is exactly why you should always include a `WHERE` clause and test it with a SELECT first, and why hosting with automatic backups gives you a safety net.
Which SQL query should a beginner learn first? Start with `SELECT`. It’s the query you’ll use most often, it only reads data so it can’t damage anything, and mastering it with `WHERE`, `ORDER BY`, and `LIMIT` unlocks the ability to answer almost any question about your data.