Skip to the content.
24 August 2026

Most modern applications store their data in relational databases. To retrieve or manipulate this data, developers use Structured Query Language (SQL).

Whether you are building a web app, analyzing business metrics, or managing system logs, knowing how to write clean SQL queries is a fundamental skill.

Retrieving and filtering data

The foundation of any SQL query is the SELECT statement. It tells the database which columns you want to retrieve, while the FROM clause specifies the target table.

SELECT first_name, last_name 
FROM employees;

To limit the rows returned, use a WHERE clause. This allows you to filter data based on specific conditions, such as finding employees in a particular department.

SELECT first_name, last_name, salary 
FROM employees 
WHERE department = 'Engineering' AND salary > 80000;

Using filters ensures that your application only fetches the necessary data from the server, reducing memory usage and network overhead.

Relational databases are structured to avoid duplicating data. Instead of storing all details in one massive sheet, information is split across multiple tables linked by identifiers (foreign keys).

To pull data from separate tables into a single result set, you use a JOIN clause.

SELECT employees.first_name, departments.name AS department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;

An INNER JOIN only returns rows where there is a match in both tables, whereas a LEFT JOIN returns all records from the left table even if no matching records exist in the right table.

Aggregating and grouping data

Sometimes you do not want individual records. Instead, you need summary metrics, like the average salary in a department or the total number of orders shipped.

For these tasks, SQL provides aggregate functions like COUNT, SUM, AVG, MIN, and MAX. You combine these functions with a GROUP BY clause to split the calculations across categories.

SELECT department, COUNT(*) AS employee_count, AVG(salary) AS average_salary
FROM employees
GROUP BY department;

By grouping data, you can build dashboards and generate analytical reports directly inside the database engine.

Practice database queries

The best way to understand SQL is by running queries against a real database. You do not need to install local software or configure server instances. Use our web-based SQL playground to create tables, insert mock data, and test queries instantly. For a condensed syntax reference, see our SQL Commands Cheatsheet.