CHAPTER 07
Intermediate
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 an application, the backend executes a massive query to fetch your profile, your settings, and your dashboard data. 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
WHEREclause 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
*Output: A massive grid displaying every piece of data in the table.*
4. Selecting Specific Columns
UsingSELECT * 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
*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
6. Comparison Operators
TheWHERE 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
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 PostgreSQL to only return unique values.
sql
8. Column Aliases (AS Keyword)
Sometimes, the actual column name in the database (emp_id) is ugly, and you want to output it as something cleaner (Employee Number) for a report. You use the AS keyword.
sql
9. Common Mistakes
-
Case Sensitivity in Data: In PostgreSQL, text comparisons inside a
WHEREclause are strictly case-sensitive!WHERE department = 'sales'will NOT match a row that contains 'Sales'. (We will learn how to do case-insensitive searches in Chapter 8 usingILIKE).
-
Using
=with NULL: If an employee's hire date is blank (NULL), you CANNOT find them by typingWHERE 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'semail, explicitly writeSELECT email. If you useSELECT *, and a database admin adds a 5-Megabyteprofilepicturecolumn to the table later, your app will suddenly crash because it's downloading 5MB of data for every user unnecessarily!
11. Exercises
-
1.
Write a query to select ONLY the
firstnameanddepartmentcolumns for employees who make exactly $85,000.
-
2.
What does the
DISTINCTkeyword do in aSELECTquery?
12. SQL Challenges
Write a query to find all employees who earn less than $50,000, and rename the output column fromsalary to NeedsRaise.
sql
13. 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?
14. Interview Questions
-
Q: Describe the architectural difference in how PostgreSQL processes a
SELECT *query versus aSELECT firstname, lastnamequery.
-
Q: Explain a scenario where the
SELECT DISTINCTcommand is absolutely necessary to generate an accurate business report.
15. FAQs
Q: Can I filter data based on multiple conditions at the same time? A: Absolutely! You can chain conditions together usingAND and OR (e.g., WHERE salary > 50000 AND department = 'IT'). We will dive deeply into complex operators in Chapter 9.
16. Summary
TheSELECT 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.
17. 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, Searching, and Limiting Results, we will master theORDER BY, LIMIT, and ILIKE operators.