SQL / Guided lesson

Master SQL Queries and Data Filtering

Learn to interact with databases using SQL. Practice retrieving, filtering, sorting, grouping, and analyzing data through real-world database examples.

Browse all SQL topics

Worked example

SQL stands for Structured Query Language. SELECT retrieves data from tables, WHERE filters rows based on conditions, ORDER BY sorts results, and GROUP BY combines records for aggregate calculations.

WITH employees (id, name, department, salary) AS (
    VALUES
        (1, 'Rahul', 'IT', 60000),
        (2, 'Priya', 'HR', 45000),
        (3, 'Arjun', 'IT', 75000),
        (4, 'Sneha', 'Finance', 55000)
)
SELECT name, department, salary
FROM employees
WHERE salary > 50000
ORDER BY salary DESC;

Expected output

name  | department | salary
Arjun | IT         | 75000
Rahul | IT         | 60000
Sneha | Finance    | 55000

Check your understanding

What is the difference between WHERE and HAVING in SQL?

Show answer

WHERE filters individual rows before grouping and aggregation. HAVING filters grouped results after GROUP BY. HAVING is commonly used with aggregate functions such as COUNT(), SUM(), and AVG().

Try it yourself

Write a SQL query to calculate the average salary of employees in each department.

Show solution
WITH employees (id, name, department, salary) AS (
    VALUES
        (1, 'Rahul', 'IT', 60000),
        (2, 'Priya', 'HR', 45000),
        (3, 'Arjun', 'IT', 75000),
        (4, 'Sneha', 'Finance', 55000)
)
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department
ORDER BY department;

-- Finance: 55000
-- HR: 45000
-- IT: 67500

SQL / Full curriculum

All SQL Topics

Choose a topic to study the explanation, code, output, questions, and practice exercises.

Lesson 1 / 56

SQL · Lesson 1 of 56

Introduction to SQL

1. Understand the idea

SQL is a language for asking a database questions and changing saved data. A database keeps information organised so a program can find it quickly.

Think of a real-life example

A school register stores student names and marks. SQL lets you ask “Show all students in Class 10.”

2. Read the example

Read the code slowly. Before checking the output, try to guess what will happen.

SELECT 2 + 3 AS total;

What happens in this code?

The SELECT statement asks the database for data. The result is a table containing only the rows and columns requested.

Expected output

total
5

3. A mistake to avoid

SQL is not exactly the same in every database. Some commands differ between SQLite, MySQL, and PostgreSQL.

New words explained simply
database
An organised place to save and find information.
select
Choose what data you want to see.

4. Check your understanding

What does declarative mean in SQL?

Specify which results you want instead of how to loop through rows.

5. Try it yourself

Try the task on your own first. It is okay to get it wrong—that is part of learning. Then open the solution to compare.

Select the product of 4 and 5.

Back to all subjects