Skip to main content
SQL Fundamentals
CHAPTER 07 Beginner

SELECT Queries and Retrieving Data

Updated: May 16, 2026
15 min read

# CHAPTER 7

SELECT Queries and Retrieving Data

1. Introduction

Putting data into a database is only half the battle. The true power of a database lies in how quickly and accurately you can get that data back out. In the CRUD lifecycle, this is the Read operation. In SQL, data retrieval is entirely controlled by the legendary SELECT statement. It is the most frequently written command in software engineering. In this chapter, we will learn how to interrogate our tables and extract the exact columns of information our applications need.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use the SELECT * command to fetch an entire table.
  • Retrieve specific, targeted columns to optimize network bandwidth.
  • Eliminate duplicate results using the DISTINCT keyword.
  • Rename output columns on the fly using Aliases (AS).

3. The SELECT * Command (Fetching Everything)

If you want to view every single row and every single column inside a table, you use the asterisk * (which acts as a wildcard meaning "All").
sql
12
-- Retrieve absolutely everything from the customers table
SELECT * FROM customers;

*Result: The database will return a massive grid containing every row and column in the table.*

4. Selecting Specific Columns

The Danger of SELECT *: If your customers table has 50 columns (including sensitive data like passwords and huge text bios) and your application only needs to display a list of Usernames, running SELECT * will download megabytes of useless, dangerous data over the network, crashing your server.

You must explicitly ask for the specific columns you need by separating them with commas.

sql
12
-- Optimized! Only fetch the first_name and email columns!
SELECT first_name, email FROM customers;

5. Removing Duplicates (DISTINCT)

Suppose you have a table of 10,000 customers, and you want a list of all the countries your customers live in. If you run SELECT country FROM customers;, you will get 10,000 rows, returning "USA" 5,000 times! To eliminate the duplicates and only see unique values, use the DISTINCT keyword.
sql
12
-- Returns a clean list of unique countries (e.g., USA, UK, Canada)
SELECT DISTINCT country FROM customers;

6. Renaming Columns in the Output (Aliases / AS)

Sometimes the column names in the database are ugly or hard for humans to read (e.g., usrfrstnm). You can dynamically rename the column *in the output result* using the AS keyword. This creates an Alias. *(Note: This does NOT rename the actual column in the database; it only renames the label on the output grid!)*
sql
12345
-- Rename the output columns to be highly readable
SELECT 
    first_name AS 'First Name', 
    emp_sal AS 'Annual Salary' 
FROM employees;

7. Mini Project: The E-Commerce Dashboard

Let's build the SQL queries required to power the admin dashboard of a store.
sql
1234567891011
-- 1. The Home Page wants to display a simple list of all product names and their prices
SELECT product_name, price FROM products;

-- 2. The Marketing team wants a list of all unique cities we have shipped to
SELECT DISTINCT shipping_city FROM orders;

-- 3. The Accountant wants a clean report of inventory counts
SELECT 
    product_name AS 'Item', 
    stock_quantity AS 'Remaining Inventory' 
FROM products;

8. Common Mistakes

  • Forgetting the FROM Keyword: SELECT name, email users; will throw a syntax error. You must explicitly tell SQL *where* the data is coming from: SELECT name, email FROM users;.
  • Using SELECT * in Production Code: Using the asterisk is great when you are manually exploring a database in phpMyAdmin. However, hardcoding SELECT * into a PHP or Node.js backend application is a severe architectural anti-pattern. Always list your columns explicitly in production code.

9. Best Practices

  • Formatting Large Queries: As your queries get longer, do not write them all on one massive horizontal line. Break them into vertical lines for readability.
*Good:*
sql
12345678910111213
  SELECT 
      id, 
      name 
  FROM 
      users;
  ```

### 10. Exercises
1. What does the asterisk `*` symbol represent in a `SELECT` statement?
2. What SQL keyword is used to eliminate duplicate rows from the output grid?

### 11. SQL Challenges
Write a query to retrieve only the `title` and `author` columns from the `books` table, but use Aliases to rename the output columns to `Book Name` and `Written By`.

sql SELECT title AS 'Book Name', author AS 'Written By' FROM books; ``

12. MCQ Quiz with Answers

Question 1

Why is it an architectural best practice to explicitly name the specific columns you want (e.g., SELECT name, age FROM users) rather than using SELECT * in production application code?

Question 2

What is the primary function of the DISTINCT keyword in a SELECT statement?

13. Interview Questions

  • Q: Explain the purpose of a Column Alias (AS) in a SELECT query. Does utilizing an Alias permanently alter the structural schema of the underlying database table?
  • Q: Describe a scenario where a Data Analyst would rely heavily on the DISTINCT keyword. What data problem does it solve?

14. FAQs

Q: Can I do math inside a
SELECT statement? A: Yes! You can actually use SQL as a calculator. If you run SELECT price * 2 AS double_price FROM products;, SQL will mathematically double the price on the fly and return the new calculated column!

15. Summary

The
SELECT statement is your primary lens into the database. By explicitly targeting specific columns to optimize bandwidth, utilizing DISTINCT to extract unique insights, and wielding AS to format the output for human readability, you have mastered the foundational mechanics of data retrieval.

16. Next Chapter Recommendation

Right now, our
SELECT` queries are returning every single row in the table. But what if we only want to see products under $50? Or users who live in New York? In Chapter 8: Filtering Data with WHERE Clause, we will learn how to surgically slice our data using exact conditions.

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: ·