codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🗄️

DBMS

23 chapters

1Intro & 3-Schema Architecture2ER Model & Diagrams3Generalization, Specialization & Aggregation4Relational Model & Codd's Rules5Relational Algebra6Tuple & Domain Relational Calculus7SQL: DDL, DML, DCL8Advanced SQL (Joins, Aggregates)9Views, Triggers & Stored Procedures10Functional Dependencies11Normalization (1NF, 2NF, 3NF)12BCNF & Lossless Decomposition13Transaction Concepts & ACID14Conflict & View Serializability15Concurrency Control & Locks162-Phase Locking (2PL)17Timestamp-Based Protocols18Indexing (Primary, Clustering)19B-Trees & B+ Trees20Hashing Techniques in DBMS21Database Recovery Techniques22NoSQL Databases Overview23Data Warehousing Concepts
SubjectsDBMS

Advanced SQL (Joins, Aggregates)

Updated 2026-04-25
2 min read

Advanced SQL (Joins, Aggregates)

While basic SELECT, INSERT, UPDATE, and DELETE commands allow for simple data management, the true power of relational databases lies in their ability to combine data from multiple tables and perform complex mathematical aggregations across millions of rows efficiently.

1. SQL Joins

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Consider two tables: Students (ID, Name, Dept_ID) and Departments (Dept_ID, Dept_Name).

INNER JOIN

Returns records that have matching values in both tables. If a student does not belong to any department, they will not appear in the result.

SELECT Students.Name, Departments.Dept_Name
FROM Students
INNER JOIN Departments ON Students.Dept_ID = Departments.Dept_ID;

LEFT (OUTER) JOIN

Returns all records from the left table (Students), and the matched records from the right table. If a student does not have a department, they will still appear in the result, but the Dept_Name will be NULL.

SELECT Students.Name, Departments.Dept_Name
FROM Students
LEFT JOIN Departments ON Students.Dept_ID = Departments.Dept_ID;

RIGHT (OUTER) JOIN

Returns all records from the right table (Departments), and the matched records from the left table.

FULL (OUTER) JOIN

Returns all records when there is a match in either left or right table. It is essentially a combination of a LEFT JOIN and a RIGHT JOIN.

2. Aggregate Functions

SQL provides built-in functions to perform calculations on a set of values and return a single value.

  • COUNT(): Returns the number of rows.
  • SUM(): Returns the total sum of a numeric column.
  • AVG(): Returns the average value of a numeric column.
  • MIN(): Returns the smallest value.
  • MAX(): Returns the largest value.
-- Find the highest salary in the company
SELECT MAX(Salary) FROM Employees;

3. GROUP BY and HAVING

The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of employees in each department". It is typically used with aggregate functions.

-- Count how many students belong to each department
SELECT Dept_ID, COUNT(ID) as Total_Students
FROM Students
GROUP BY Dept_ID;

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions. WHERE filters rows before aggregation, while HAVING filters groups after aggregation.

-- Find departments that have MORE than 50 students
SELECT Dept_ID, COUNT(ID) as Total_Students
FROM Students
GROUP BY Dept_ID
HAVING COUNT(ID) > 50;

4. Subqueries (Nested Queries)

A Subquery is a query nested inside another query.

-- Find the names of students who belong to the 'Computer Science' department
SELECT Name 
FROM Students 
WHERE Dept_ID IN (
    SELECT Dept_ID 
    FROM Departments 
    WHERE Dept_Name = 'Computer Science'
);


PreviousSQL: DDL, DML, DCLNextViews, Triggers & Stored Procedures

Recommended Gear

SQL: DDL, DML, DCLViews, Triggers & Stored Procedures