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 topicsWorked 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 | 55000Check 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