← Back to Lessons
BEGINNER ⏱ 25 minutes

Introduction to SELECT Statements

The SELECT statement is the most important SQL command you'll learn. Think of it as asking your database a question: "Show me this information." Every data analyst, developer, and database professional uses SELECT statements daily—this is your foundation!


📚 What You'll Learn

1. What Does SELECT Do?

The SELECT statement retrieves data from database tables. It's like pointing at specific information in a filing cabinet and saying "I want to see this."

💡 Real-World Example: Imagine you're a teacher with a student roster. Instead of pulling out the entire filing cabinet, you use SELECT to say: "Show me just the names and email addresses of all students in Math 101." That's what SELECT does—it fetches exactly what you need.

SELECT doesn't change or delete data—it only reads it. Think of it as "looking" at your data without touching it. This makes SELECT a safe command to practice with!

2. Basic Query Structure

Every SELECT statement follows this pattern:

SELECT column_name(s)
FROM table_name;

Let's break this down:

⚠️ Important: SQL is not case-sensitive, but we write keywords in UPPERCASE by convention. This makes queries easier to read. You could write "select" or "SeLeCt" and it would work, but "SELECT" is the professional standard.

3. Selecting All Columns with SELECT *

The asterisk (*) is a wildcard that means "all columns." It's a quick way to see everything in a table:

SELECT *
FROM students;

This query says: "Show me every column from the students table."

What You'll See:

student_id name email major gpa
1 Alice Johnson alice@university.edu Computer Science 3.8
2 Bob Smith bob@university.edu Mathematics 3.6
3 Carol Davis carol@university.edu Biology 3.9

When to Use SELECT *:

When NOT to Use SELECT *:

💡 Pro Tip: SELECT * is great for learning and quick checks, but experienced developers specify exact columns in their queries. It's more efficient and makes your code clearer.

4. Selecting Specific Columns

Most of the time, you only need specific information. List the column names you want, separated by commas:

SELECT name, email
FROM students;

This query says: "Show me only the name and email columns from the students table."

Result:

name email
Alice Johnson alice@university.edu
Bob Smith bob@university.edu
Carol Davis carol@university.edu

Notice how we only see the two columns we requested? This is more efficient than SELECT * because the database doesn't waste time retrieving columns we don't need.

Selecting Multiple Columns - Order Matters!

The columns appear in the order you list them:

SELECT name, major, gpa
FROM students;

Shows: name, then major, then gpa

SELECT gpa, name, major
FROM students;

Shows: gpa, then name, then major

5. Using Column Aliases (Renaming Columns)

Sometimes column names in the database aren't user-friendly. Aliases let you rename columns in your output using the AS keyword:

SELECT 
    name AS student_name,
    email AS contact_email,
    gpa AS grade_point_average
FROM students;

Before (without aliases):

name email gpa
Alice Johnson alice@university.edu 3.8

After (with aliases):

student_name contact_email grade_point_average
Alice Johnson alice@university.edu 3.8

The data is the same, but the column headers are now more descriptive!

Shortcut: AS is Optional!

You can omit the AS keyword—both work the same:

SELECT name AS student_name
FROM students;

✅ With AS (more readable)

SELECT name student_name
FROM students;

✅ Without AS (works too!)

💡 Best Practice: Include the AS keyword. It makes your queries easier to read, especially for beginners or when working in teams.

6. Common Beginner Mistakes (and How to Avoid Them)

Mistake #1: Forgetting the Semicolon

-- ❌ WRONG (missing semicolon)
SELECT name FROM students

-- ✅ CORRECT
SELECT name FROM students;

Why it matters: Without the semicolon, the database doesn't know your query is finished. Some tools will still execute it, but it's bad practice.

Mistake #2: Typos in Column Names

-- ❌ WRONG (misspelled column name)
SELECT nam FROM students;

-- ✅ CORRECT
SELECT name FROM students;

Error message: "Unknown column 'nam' in 'field list'"
Fix: Check spelling carefully. SQL won't autocorrect for you!

Mistake #3: Typos in Table Names

-- ❌ WRONG (wrong table name)
SELECT name FROM student;  -- Singular, but table is "students"

-- ✅ CORRECT
SELECT name FROM students;

Error message: "Table 'student' doesn't exist"
Fix: Table names must match exactly (including singular vs. plural).

Mistake #4: Forgetting Commas Between Columns

-- ❌ WRONG (missing comma)
SELECT name email FROM students;

-- ✅ CORRECT
SELECT name, email FROM students;

What happens: The database might think "email" is an alias for "name"—confusing!
Fix: Always separate column names with commas.

Mistake #5: Extra Comma After Last Column

-- ❌ WRONG (trailing comma)
SELECT name, email, 
FROM students;

-- ✅ CORRECT
SELECT name, email
FROM students;

Error message: "You have an error in your SQL syntax"
Fix: No comma after the last column name.

7. 🎯 Hands-On Practice Exercises

Time to apply what you've learned! Try these exercises on your own. Don't peek at the answers until you've tried!

Exercise 1: Basic SELECT

Task: Write a query to retrieve all columns from the students table.

Show Answer
SELECT *
FROM students;

Exercise 2: Select Specific Columns

Task: Write a query to retrieve only the name and major columns from the students table.

Show Answer
SELECT name, major
FROM students;

Exercise 3: Using Aliases

Task: Write a query to retrieve the name column but rename it to "Student Name" and the email column renamed to "Contact Email".

Show Answer
SELECT 
    name AS "Student Name",
    email AS "Contact Email"
FROM students;

Note: When your alias has spaces, you must use quotes around it!

Exercise 4: Multiple Columns in Custom Order

Task: Write a query to display gpa first, then name, then major from the students table.

Show Answer
SELECT gpa, name, major
FROM students;

Challenge Exercise: Spot the Mistakes

Task: Find and fix all the errors in this query:

SELECT nam email
FROM student
Show Answer
SELECT name, email
FROM students;

Errors found:
1. "nam" should be "name" (typo)
2. Missing comma between column names
3. "student" should be "students" (wrong table name)
4. Missing semicolon at the end

📝 Key Takeaways

🚀 What's Next?

Now that you can retrieve data with SELECT, you're ready to learn how to filter that data! The WHERE clause lets you specify conditions, like "Show me only students with a GPA above 3.5" or "Find students majoring in Computer Science."

Up next: WHERE Clause - Filtering Your Data

← Back to Lessons
Practice Now →