All posts
SQLAugust 17, 20267 min read

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.

F
Fepiq Team
Fepiq

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.

What is SQL?

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.

A sample table to practice with

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.

idnamecountrysignup_year
1Amara OkaforNigeria2024
2Liam ChenCanada2023
3Sofia RossiItaly2024
4Noah SilvaBrazil2022

The SELECT statement

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".

sql
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.

sql
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.

Filtering rows with WHERE

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.

sql
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 (<).

sql
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.

Combining conditions with AND and OR

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.

sql
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.

Common WHERE operators

Here are the operators you will use most often when filtering with WHERE.

OperatorMeaningExample
=equalscountry = 'Italy'
!= or <>not equal tocountry != 'Italy'
>, <greater than, less thansignup_year > 2022
>=, <=greater than or equal, less than or equalsignup_year >= 2023
LIKEmatches a text patternname LIKE 'S%'
INmatches any value in a listcountry IN ('Canada', 'Italy')
BETWEENmatches a range, inclusivesignup_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.

Sorting results with ORDER BY

SELECT and WHERE control what you see. ORDER BY controls the order results appear in. Add it at the end of your query.

sql
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.

Try it yourself

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.

  1. Write a query that selects only the name column for every customer.
  2. Write a query that selects name and country, but only for customers who signed up in 2024.
  3. Write a query that selects everything, ordered by signup_year from newest to oldest.

Mistakes beginners often make

  • Forgetting quotes around text values, like writing WHERE country = Canada instead of WHERE country = 'Canada'.
  • Using a single = sign correctly is fine in SQL (unlike some programming languages that need ==), but forgetting it entirely causes an error.
  • Putting WHERE before FROM. The correct order is always SELECT, then FROM, then WHERE.
  • Forgetting the semicolon at the end of a statement when running multiple queries together.
  • Assuming SELECT * is always fine in real applications. It works for learning, but naming columns explicitly is faster and safer once you build real projects.

Frequently asked questions

What is the difference between SELECT and WHERE in SQL?+

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.

Do I need to know programming to learn SQL?+

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.

Is SQL the same in MySQL, PostgreSQL, and other databases?+

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.

Why does my WHERE clause return no results?+

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.

What should I learn after SELECT and WHERE?+

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 guide

Need a developer to build the database-backed app around your queries?

Talk to our team

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.