SQL SELECT and WHERE Explained for Beginners
Learn the SQL SELECT and WHERE clauses step by step, with copy-paste examples. Write your first working query today, no experience needed.
Learn the SQL SELECT and WHERE clauses step by step, with copy-paste examples. Write your first working query today, no experience needed.
If you searched for "SQL SELECT and WHERE explained for beginners", here is the short answer: SELECT tells the database which columns of data you want to see, and WHERE tells it which rows to include by filtering out the ones you don't need. Together, SELECT and WHERE are the two most important building blocks of SQL, the language used to talk to databases. By the end of this guide, you will be able to write real queries yourself, even if you have never touched a database before.
SQL stands for Structured Query Language. It is how you ask a database questions, like "show me all customers from Canada" or "find every order placed last week". A database stores information in tables, which look a lot like spreadsheets: rows and columns. SQL is the language you use to read, add, change, and delete that information.
If you already connected a PHP application to a MySQL database, SQL is the language you'll use inside that connection to actually fetch data. This guide focuses purely on reading data with SELECT and WHERE, which works the same way in MySQL, PostgreSQL, SQLite, and almost every other SQL database.
To keep things concrete, imagine a table named customers that stores information about people who signed up for a service. Every example below uses this table.
| id | name | country | signup_year |
|---|---|---|---|
| 1 | Amara Okafor | Nigeria | 2024 |
| 2 | Liam Chen | Canada | 2023 |
| 3 | Sofia Rossi | Italy | 2024 |
| 4 | Noah Silva | Brazil | 2022 |
SELECT is how you choose which columns to see. The most basic query selects every column from a table using the * symbol, which means "all columns".
SELECT *
FROM customers;Line by line: SELECT * means "give me every column". FROM customers means "look in the customers table". The semicolon (;) at the end tells the database this statement is finished. This query returns all four rows and all four columns from the table above.
Usually you don't need every column. You can list only the ones you want, separated by commas. This is faster and easier to read.
SELECT name, country
FROM customers;This query returns just two columns, name and country, for every row in the table. The order you list the columns is the order they appear in the results.
SELECT decides which columns you see. WHERE decides which rows you see. You add a WHERE clause after FROM to filter the results using a condition.
SELECT name, country
FROM customers
WHERE country = 'Canada';Line by line: SELECT name, country picks the two columns to show. FROM customers picks the table. WHERE country = 'Canada' keeps only the rows where the country column exactly equals the text Canada. Text values in SQL go inside single quotes. This query returns just Liam Chen, since he is the only customer from Canada.
WHERE works with numbers too, and you can use comparison operators like greater than (>) or less than (<).
SELECT name, signup_year
FROM customers
WHERE signup_year > 2023;This keeps only customers who signed up after 2023, meaning signup_year is 2024. Notice that numbers, unlike text, don't need quotes around them.
You can check more than one condition at once. AND keeps a row only if all conditions are true. OR keeps a row if at least one condition is true.
SELECT name, country, signup_year
FROM customers
WHERE country = 'Nigeria' AND signup_year = 2024;This returns only rows where both conditions are true at the same time: the country must be Nigeria, and the signup_year must be 2024. Amara Okafor matches both, so she is the only row returned.
Here are the operators you will use most often when filtering with WHERE.
| Operator | Meaning | Example |
|---|---|---|
| = | equals | country = 'Italy' |
| != or <> | not equal to | country != 'Italy' |
| >, < | greater than, less than | signup_year > 2022 |
| >=, <= | greater than or equal, less than or equal | signup_year >= 2023 |
| LIKE | matches a text pattern | name LIKE 'S%' |
| IN | matches any value in a list | country IN ('Canada', 'Italy') |
| BETWEEN | matches a range, inclusive | signup_year BETWEEN 2023 AND 2024 |
The % symbol in LIKE means "any characters can go here". So name LIKE 'S%' matches any name starting with the letter S, such as Sofia.
SELECT and WHERE control what you see. ORDER BY controls the order results appear in. Add it at the end of your query.
SELECT name, signup_year
FROM customers
WHERE signup_year >= 2023
ORDER BY signup_year DESC;DESC means descending, so the newest signups appear first. Use ASC for ascending order (oldest or smallest first), which is also the default if you leave DESC or ASC out.
You don't need to install anything to practice. Most database tools, including phpMyAdmin, pgAdmin, or free online SQL sandboxes, let you type a query and run it instantly. Try these three exercises using the customers table above.
SELECT chooses which columns of a table you want to see in the results. WHERE chooses which rows to include, based on a condition. You almost always use them together: SELECT picks the columns, WHERE filters the rows.
No. SQL reads close to plain English and is often the first language people learn before any programming language. If you can follow a recipe with steps like "pick these ingredients, but only if fresh", you can learn SQL SELECT and WHERE in under an hour.
The core commands, including SELECT, WHERE, AND, OR, and ORDER BY, work the same across almost all SQL databases like MySQL, PostgreSQL, and SQLite. Small differences appear in advanced features, but everything in this guide works everywhere.
The most common reasons are a typo in the value, missing quotes around text, or mismatched capitalization if your database is case-sensitive. Double check the exact spelling and quoting of the value you are filtering on.
Next, learn JOIN, which lets you combine data from two related tables, and GROUP BY, which lets you summarize data, like counting how many customers came from each country. Both build directly on the SELECT and WHERE skills from this guide.
Already connected your app to a database and want to put these queries to use? See how to connect PHP to a MySQL database step by step.
Read the PHP database guideNeed a developer to build the database-backed app around your queries?
Talk to our teamOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
What is an index in SQL? A plain-English guide with copy-paste examples showing how indexes speed up queries and when you actually need one.
Learn JavaScript DOM manipulation for beginners: select elements, change text and styles, and handle clicks with simple, copy-paste code examples.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.