Insights into Company
← SQL learning path

Queries and Aggregation

Learn SQL from fundamentals to advanced interview and production-level database concepts through practical examples, questions and quizzes.

Complete SQL Learning & Interview Path

Learn SQL by working with realistic databases instead of memorising isolated commands. Every concept can be connected with practical tables such as students, employees, departments, countries, states, customers, products and orders.

SQL Fundamentals SELECT WHERE JOINs GROUP BY Subqueries Indexes Transactions Window Functions Interview Questions
Start Here

SQL through a real database

SQL is used to store, retrieve, modify and analyse structured data. Instead of learning commands independently, imagine that we are building a small company database.

Student table

A student database can contain identity, name, course and marks.

CREATE TABLE students ( id INT PRIMARY KEY, name VARCHAR(100), course VARCHAR(100), marks INT );

Employee table

Employee information can be connected with departments, salaries and managers.

CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(100), department_id INT, salary DECIMAL(12,2) );

Country / State table

Geographic data is useful for understanding relationships and JOINs.

CREATE TABLE states ( id INT PRIMARY KEY, name VARCHAR(100), country_id INT );

Customer / Orders

E-commerce data provides practical examples for aggregation, JOINs and transactions.

CREATE TABLE orders ( id INT PRIMARY KEY, customer_id INT, total DECIMAL(12,2), order_date DATE );
id name course marks
1 Rahul Java 85
2 Priya SQL 92
3 Amit React 76
SQL Learning Block 1

Questions 1 – 3

1
Question 21 of 3

Find maximum salary in each department.

Solution

SELECT department_id, MAX(salary) AS max_salary FROM employees GROUP BY department_id;
Think before looking at the solution
  • What would the SQL query do?
  • Which rows will match?
  • Which columns will appear?
  • What happens when NULL values are involved?
  • Can the query be improved using an index?
Question 22 of 3

Find departments having more than 5 employees.

Solution

SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id HAVING COUNT(*) > 5;
  • WHERE filters rows; HAVING filters groups.
Think before looking at the solution
  • What would the SQL query do?
  • Which rows will match?
  • Which columns will appear?
  • What happens when NULL values are involved?
  • Can the query be improved using an index?
Question 23 of 3

Find departments whose average salary is greater than 60,000.

Solution

SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id HAVING AVG(salary) > 60000;
Think before looking at the solution
  • What would the SQL query do?
  • Which rows will match?
  • Which columns will appear?
  • What happens when NULL values are involved?
  • Can the query be improved using an index?
Knowledge Check

Quiz — Final Learning Block

Complete the remaining questions before finishing this SQL learning path.

1. Find maximum salary in each department.
Show explanation
SELECT department_id, MAX(salary) AS max_salary FROM employees GROUP BY department_id;
2. Find departments having more than 5 employees.
Show explanation
SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id HAVING COUNT(*) > 5;
  • WHERE filters rows; HAVING filters groups.
3. Find departments whose average salary is greater than 60,000.
Show explanation
SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id HAVING AVG(salary) > 60000;

Showing questions 11–13 of 13