Skip to main content
PostgreSQL
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 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 PostgreSQL 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. 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
12
SELECT first_name AS "First Name", salary AS "Annual Income" 
FROM employees;

9. Common Mistakes

  • Case Sensitivity in Data: In PostgreSQL, text comparisons inside a WHERE clause 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 using ILIKE).
  • 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. SQL Challenges

Write a query to find all employees who earn less than $50,000, and rename the output column from salary to Needs
Raise.
sql
123
SELECT first_name, salary AS "Needs_Raise" 
FROM employees 
WHERE salary < 50000;

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 a SELECT firstname, lastname query.
  • Q: Explain a scenario where the SELECT DISTINCT command 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 using AND and OR (e.g., WHERE salary > 50000 AND department = 'IT'). We will dive deeply into complex operators in Chapter 9.

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

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 the ORDER BY, LIMIT, and ILIKE 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: ·