Skip to main content
MySQL Basics
CHAPTER 07 Beginner

SELECT Queries and Filtering Data

Updated: May 16, 2026
5 min read

# CHAPTER 7

SELECT Queries and Filtering Data

1. Introduction

Of all the SQL commands, INSERT, UPDATE, and DELETE make up about 10% of a developer's daily usage. The other 90% is spent retrieving data. When you log into Facebook, the backend executes a massive query to fetch your profile, your friends, and your newsfeed. The command used to "read" or "fetch" data from a database is the SELECT statement. In this chapter, we will master the art of querying databases to find exactly the needle we need in the data haystack.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Retrieve all rows and columns using SELECT *.
  • Retrieve specific columns to optimize performance.
  • Use the WHERE clause to filter rows based on conditions.
  • Utilize basic comparison operators (=, >, <).
  • Remove duplicate results using SELECT DISTINCT.

3. The Basics: SELECT *

If you want to view the entire table—every single row and every single column—you use the asterisk * (which acts as a wildcard meaning "All").
sql
12
-- "Select ALL columns FROM the employees table"
SELECT * FROM employees;

*Output: A massive grid displaying every piece of data in the table.*

4. Selecting Specific Columns

Using SELECT * on a table with 100 columns and a million rows is incredibly slow and wastes massive amounts of server RAM. You should only ask the database for the exact data you need.
sql
12
-- We only want to see names and emails. Ignore the salary and ID.
SELECT first_name, email FROM employees;

*Output: A clean, 2-column table. This executes thousands of times faster than SELECT * on a large database.*

5. Filtering Data with the WHERE Clause

Getting all the data is rarely useful. Usually, we want to find a specific user or a specific group. We filter the rows returned using the WHERE clause.
sql
1234567
-- Find the single employee with ID number 4
SELECT * FROM employees 
WHERE emp_id = 4;

-- Find everyone who works in the IT department
SELECT first_name, email FROM employees 
WHERE department = &#039;IT';

6. Comparison Operators

The WHERE clause is extremely powerful when combined with mathematical comparison operators.
  • = (Equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)
  • != or <> (Not equal to)
sql
1234567
-- Find everyone making a six-figure salary
SELECT first_name, salary FROM employees 
WHERE salary >= 100000;

-- Find everyone who does NOT work in the Exec department
SELECT first_name, department FROM employees 
WHERE department != &#039;Exec';

7. Handling Duplicates: SELECT DISTINCT

Imagine a table of 10,000 customer orders. You want a list of all the different cities your customers live in. If you run SELECT city FROM orders;, you will get 10,000 rows, with "New York" repeating 4,000 times. The DISTINCT keyword forces MySQL to only return unique values.
sql
12345678
-- This returns every department name exactly ONE time, no matter how many employees are in it.
SELECT DISTINCT department FROM employees;

-- Output:
-- IT
-- Sales
-- HR
-- Exec

8. Mini Project: The Analyst's Dashboard

You are an HR Analyst. Your boss asks for a report containing the email addresses of every employee who earns less than $60,000 so they can be reviewed for a raise.
sql
12345678
-- The perfect query for the boss:
SELECT first_name, email, salary 
FROM employees 
WHERE salary < 60000;

-- Output:
-- Mike | mike@company.com | 45000.00
-- John | john@company.com | 55000.00

9. Common Mistakes

  • Case Sensitivity in Data: In standard MySQL configurations, comparing text in a WHERE clause (e.g., WHERE department = 'sales') is *case-insensitive*. It will match 'Sales', 'SALES', and 'sales'. However, table names and column names ARE case-sensitive on Linux servers! Always use exact lowercase casing for columns.
  • Using = with NULL: If an employee's hire date is blank (NULL), you CANNOT find them by typing WHERE hiredate = NULL. Standard math operators do not work on "nothingness". (We will learn how to find NULLs in Chapter 9).

10. Best Practices

  • Never use SELECT * in Production Code: If your PHP or Python application only needs the user's email, explicitly write SELECT email. If you use SELECT *, and a database admin adds a 5-Megabyte profilepicture column to the table later, your app will suddenly crash because it's downloading 5MB of data for every user unnecessarily!

11. Exercises

  1. 1. Write a query to select ONLY the firstname and department columns for employees who make exactly $85,000.
  1. 2. What does the DISTINCT keyword do in a SELECT query?

12. MCQ Quiz with Answers

Question 1

Why is it considered a bad practice to use SELECT * in production application code?

Question 2

Which operator would you use in a WHERE clause to find all products that cost less than $50?

13. Interview Questions

  • Q: Describe the architectural difference in how MySQL processes a SELECT * query versus a SELECT firstname, last_name query.
  • Q: Explain a scenario where the SELECT DISTINCT command is absolutely necessary to generate an accurate business report.

14. FAQs

Q: Can I filter data based on multiple conditions at the same time? A: Absolutely! You can chain conditions together using AND and OR (e.g., WHERE salary > 50000 AND department = 'IT'). We will dive deeply into complex operators in Chapter 9.

15. Summary

The SELECT statement is your primary tool for investigating databases. By explicitly requesting only the columns you need and utilizing the WHERE clause with strict mathematical operators, you transform a massive, unreadable database into highly targeted, instantaneous business intelligence.

16. Next Chapter Recommendation

We know how to filter data, but right now, the results come back in a random, chaotic order. What if we want the top 10 highest-paid employees? Or an alphabetical list? In Chapter 8: Sorting, Limiting, and Searching Data, we will master the ORDER BY, LIMIT, and LIKE operators.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·