LEARN BASICS OF SQL

Mastering SQL Basics: A Practical Guide to SELECT, WHERE, and JOINs
Tutorial • 5 min read

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:

  1. SELECT — Retrieving the data you need.
  2. WHERE — Filtering for specific conditions.
  3. 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

SQL Query ANSI-SQL
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

SQL Query Filtering
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.

SQL Query INNER JOIN
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.

Exercise 1 Topic: WHERE & NULL

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 ▼
solution.sql
SELECT first_name, salary
FROM employees
WHERE department_id IS NULL;

Expected output: 1 row returned (Sara | 54000).

Exercise 2 Topic: Filtering Range

Engineering Salary Filter

Retrieve the first_name of all employees in Department 1 whose salary is less than 70,000.

Show Solution ▼
solution.sql
SELECT first_name
FROM employees
WHERE department_id = 1 AND salary < 70000;

Expected output: 1 row returned (Marcus).

Exercise 3 Topic: LEFT JOIN

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 ▼
solution.sql
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).


What’s Next?

Once you are comfortable with basic selection, filtering, and joining, the next step is summarizing data. In the next post, we will explore:

  • Aggregation functions: COUNT(), SUM(), and AVG()
  • Grouping records with GROUP BY
  • Filtering aggregated results with HAVING

Happy querying!

Post a Comment

0 Comments