Mastering SQL Basics: A Practical Guide to SELECT, WHERE, and JOINs
Whether you are building a backend API, analyzing datasets, or prepping for interviews, SQL (Structured Query Language) is one of the most durable skills in tech.
Relational databases organize data into structured tables consisting of rows and columns. In this guide, we'll walk through the foundational commands with practical examples and practice challenges:
SELECT— Retrieving the data you need.WHERE— Filtering for specific conditions.JOIN— Connecting related datasets.
The Sample Dataset
We'll use these two simple company tables for all examples and exercises:
employees Table
| employee_id | first_name | department_id | salary |
|---|---|---|---|
| 101 | Alex | 1 | 75000 |
| 102 | Priya | 2 | 88000 |
| 103 | Marcus | 1 | 62000 |
| 104 | Sara | NULL | 54000 |
departments Table
| department_id | department_name |
|---|---|
| 1 | Engineering |
| 2 | Marketing |
| 3 | Finance |
1. Retrieving Data: The SELECT Statement
The SELECT statement tells the database engine which columns you want to see, while FROM specifies the source table.
Fetching Specific Columns
SELECT first_name, salary
FROM employees;
Best Practice: Always request only the columns your feature needs rather than SELECT * to minimize memory usage and network transfer overhead.
2. Filtering Rows: The WHERE Clause
The WHERE clause filters records based on boolean conditions.
Chaining Criteria with AND
SELECT first_name, department_id, salary
FROM employees
WHERE department_id = 1 AND salary >= 70000;
Gotcha: For missing records, always check with IS NULL or IS NOT NULL, never = NULL.
3. Combining Tables: JOIN
Relational databases split data across tables to minimize duplication. To query records side-by-side, we join them using primary and foreign keys.
SELECT
e.first_name,
e.salary,
d.department_name
FROM employees AS e
INNER JOIN departments AS d
ON e.department_id = d.department_id;
Hands-On Practice Exercises
Test your understanding using the employees and departments tables above. Write your query first, then expand the solutions to check your work.
Unassigned Department Check
Write a query that selects the first_name and salary of all employees who do not have an assigned department.
Show Solution ▼
SELECT first_name, salary
FROM employees
WHERE department_id IS NULL;
Expected output: 1 row returned (Sara | 54000).
Engineering Salary Filter
Retrieve the first_name of all employees in Department 1 whose salary is less than 70,000.
Show Solution ▼
SELECT first_name
FROM employees
WHERE department_id = 1 AND salary < 70000;
Expected output: 1 row returned (Marcus).
Complete Employee Roster
Write a query that displays every employee's first_name along with their corresponding department_name, ensuring that employees without a department are still included in the final results.
Show Solution ▼
SELECT
e.first_name,
d.department_name
FROM employees AS e
LEFT JOIN departments AS d
ON e.department_id = d.department_id;
Expected output: 4 rows returned (Alex, Priya, Marcus, and Sara with NULL department).
0 Comments