SQL SELECT Statement Tutorial: Query Data Like a Pro
📖 Introduction: The Heart of SQL
If SQL were a toolbox, SELECT would be the hammer — it's the most frequently used and most important command you'll ever learn. Data retrieval is what databases are built for, and SELECT is how you ask your database questions and get answers.
In simple terms:
SELECTtells the database what data you want.FROMtells it where to find that data.
🛠️ Setting Up Practice Data
Before we query anything, we need a table with data. Run these commands to create a sample employees table:
1-- Create the table 2CREATE TABLE employees ( 3 emp_id INT PRIMARY KEY, 4 first_name VARCHAR(50), 5 last_name VARCHAR(50), 6 department VARCHAR(50), 7 salary DECIMAL(10,2), 8 hire_date DATE, 9 city VARCHAR(50) 10); 11 12-- Insert sample data 13INSERT INTO employees VALUES 14(1, 'Alice', 'Johnson', 'IT', 75000.00, '2020-03-15', 'New York'), 15(2, 'Bob', 'Smith', 'HR', 55000.00, '2019-07-22', 'Los Angeles'), 16(3, 'Carol', 'Williams', 'IT', 80000.00, '2021-01-10', 'New York'), 17(4, 'David', 'Brown', 'Finance', 90000.00, '2018-11-05', 'Chicago'), 18(5, 'Eve', 'Davis', 'HR', 55000.00, '2022-06-18', 'Los Angeles'), 19(6, 'Frank', 'Miller', 'IT', 75000.00, '2020-09-30', 'New York');
1️⃣ Basic Querying Syntax
Every SELECT query follows this pattern:
1SELECT column_name(s) 2FROM table_name;
Rules:
SELECTandFROMare keywords (not case-sensitive, but UPPERCASE is convention)- End every statement with a semicolon (
;) - The query reads like English: "Select [these columns] from [this table]"
2️⃣ SELECT * — Retrieve ALL Columns
Purpose: Get every single column from a table. The asterisk (*) is a wildcard meaning "everything."
Syntax:
1SELECT * FROM table_name;
Example:
1SELECT * FROM employees;
Output:
+--------+------------+-----------+-----------+----------+------------+-------------+
| emp_id | first_name | last_name | department| salary | hire_date | city |
+--------+------------+-----------+-----------+----------+------------+-------------+
| 1 | Alice | Johnson | IT | 75000.00 | 2020-03-15 | New York |
| 2 | Bob | Smith | HR | 55000.00 | 2019-07-22 | Los Angeles |
| 3 | Carol | Williams | IT | 80000.00 | 2021-01-10 | New York |
| 4 | David | Brown | Finance | 90000.00 | 2018-11-05 | Chicago |
| 5 | Eve | Davis | HR | 55000.00 | 2022-06-18 | Los Angeles |
| 6 | Frank | Miller | IT | 75000.00 | 2020-09-30 | New York |
+--------+------------+-----------+-----------+----------+------------+-------------+
When to use
*:
- Quick exploration of a table
- When you genuinely need every column
When NOT to use
*:
- Production queries (slows performance)
- When you only need specific columns
3️⃣ SELECT Specific Columns — Retrieve Only What You Need
Purpose: Choose exactly which columns to return, ignoring the rest.
Syntax:
1SELECT column1, column2 FROM table_name;
Example — Get only names and departments:
1SELECT first_name, last_name, department FROM employees;
Output:
+------------+-----------+-----------+
| first_name | last_name | department|
+------------+-----------+-----------+
| Alice | Johnson | IT |
| Bob | Smith | HR |
| Carol | Williams | IT |
| David | Brown | Finance |
| Eve | Davis | HR |
| Frank | Miller | IT |
+------------+-----------+-----------+
Example — Get only employee IDs and salaries:
1SELECT emp_id, salary FROM employees;
Output:
+--------+----------+
| emp_id | salary |
+--------+----------+
| 1 | 75000.00 |
| 2 | 55000.00 |
| 3 | 80000.00 |
| 4 | 90000.00 |
| 5 | 55000.00 |
| 6 | 75000.00 |
+--------+----------+
Best Practice: Always select only the columns you need. It's faster and cleaner.
4️⃣ Aliasing Columns — Rename Output Headers with AS
Purpose: Give a column a temporary, more readable name in your result set. This does not change the actual column name in the database.
Syntax:
1SELECT column_name AS alias_name FROM table_name;
Example — Rename salary to Annual Salary:
1SELECT first_name, salary AS annual_salary FROM employees;
Output:
+------------+---------------+
| first_name | annual_salary |
+------------+---------------+
| Alice | 75000.00 |
| Bob | 55000.00 |
| Carol | 80000.00 |
| David | 90000.00 |
| Eve | 55000.00 |
| Frank | 75000.00 |
+------------+---------------+
Example — Alias with spaces (use quotes):
1SELECT first_name, salary AS "Annual Salary (USD)" FROM employees;
Output:
+------------+---------------------+
| first_name | Annual Salary (USD) |
+------------+---------------------+
| Alice | 75000.00 |
| Bob | 55000.00 |
+------------+---------------------+
Example — Alias without AS keyword (shorthand):
1SELECT first_name fname, salary annual_salary FROM employees;
Note:
ASis optional in most databases, but using it improves readability.
5️⃣ Aliasing Tables — Shorten Table Names
Purpose: Give a table a short nickname, especially useful when joining multiple tables (covered in later modules).
Syntax:
1SELECT column FROM table_name AS alias;
Example:
1SELECT e.first_name, e.salary FROM employees AS e;
Output: Same as before, but e is now a shorthand for employees.
Why alias tables?
- Saves typing in long table names
- Essential when the same column exists in multiple tables
6️⃣ DISTINCT — Eliminate Duplicate Values
Purpose: Return only unique values from a column, removing duplicates.
Syntax:
1SELECT DISTINCT column_name FROM table_name;
Example — Without DISTINCT (all departments):
1SELECT department FROM employees;
Output:
+------------+
| department |
+------------+
| IT |
| HR |
| IT |
| Finance |
| HR |
| IT |
+------------+
(IT and HR appear multiple times)
Example — With DISTINCT (unique departments only):
1SELECT DISTINCT department FROM employees;
Output:
+------------+
| department |
+------------+
| IT |
| HR |
| Finance |
+------------+
Example — DISTINCT with multiple columns:
1SELECT DISTINCT department, city FROM employees;
Output:
+------------+-------------+
| department | city |
+------------+-------------+
| IT | New York |
| HR | Los Angeles |
| Finance | Chicago |
+------------+-------------+
This returns unique combinations of department and city.
Example — Count unique departments:
1SELECT COUNT(DISTINCT department) AS unique_departments FROM employees;
Output:
+--------------------+
| unique_departments |
+--------------------+
| 3 |
+--------------------+
📝 Complete Command Reference
| Command | What It Does | Example |
|---|---|---|
SELECT * FROM table; | Get all columns | SELECT * FROM employees; |
SELECT col1, col2 FROM table; | Get specific columns | SELECT first_name, salary FROM employees; |
SELECT col AS alias FROM table; | Rename column in output | SELECT salary AS pay FROM employees; |
SELECT * FROM table AS alias; | Rename table (shorthand) | SELECT * FROM employees AS e; |
SELECT DISTINCT col FROM table; | Remove duplicate values | SELECT DISTINCT city FROM employees; |
🚀 Hands-On Project: Employee Directory Query
Project Goal
Build queries for an HR dashboard that retrieves specific employee information.
Task 1: Get the Full Employee Roster
1SELECT * FROM employees;
Task 2: Create a Name Directory
1SELECT first_name, last_name FROM employees;
Task 3: Build a Salary Report
1SELECT 2 first_name, 3 last_name, 4 salary AS "Monthly Salary", 5 department 6FROM employees;
Task 4: Find All Unique Cities
1SELECT DISTINCT city FROM employees;
Task 5: Find All Unique Department-City Pairs
1SELECT DISTINCT department, city FROM employees;
Task 6: Count How Many Unique Departments Exist
1SELECT COUNT(DISTINCT department) AS total_departments FROM employees;
✅ Module 2 Summary
| Concept | Command Pattern | Remember |
|---|---|---|
| Select everything | SELECT * FROM table; | * = all columns |
| Select specific columns | SELECT col1, col2 FROM table; | Only fetch what you need |
| Column alias | SELECT col AS alias FROM table; | Renames output header only |
| Table alias | SELECT * FROM table AS t; | Shortens long table names |
| Remove duplicates | SELECT DISTINCT col FROM table; | Returns unique values only |
🎯 Practice Exercises
- Select only
first_nameandhire_datefromemployees. - Select all columns but alias
salaryascompensation. - List all unique salaries in the company.
- Find all unique combinations of
departmentandsalary. - Count how many unique cities the employees are from.
🎓 What's Next?
In Module 3, you'll learn how to filter your data using WHERE, sort results with ORDER BY, and limit output with LIMIT. You'll move from "show me everything" to "show me exactly what I want!" 🚀