Lesson 14/100

Tutorials SQL Server Tutorial

GROUP BY — Complete Guide

GROUP BY — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.

On this page

SQL Server Tutorial · Lesson 14 of 100

GROUP BY

SQL basicsQueriesAdvanced

SQL basics · 1 — SELECT · ~6 min · SQL — SQL Queries & Clauses

What is this?

GROUP BY collapses rows that share the same key and lets you run aggregates (COUNT, SUM, AVG) per group.

Why should you care?

Managers ask “orders per city” or “revenue per day” — that is GROUP BY, not a row-by-row dump.

See it live — copy this example

Run in SQL Server Management Studio (SSMS) or Azure Data Studio.

USE DataVerse;
IF OBJECT_ID(N'dbo.Orders', N'U') IS NOT NULL DROP TABLE dbo.Orders;
CREATE TABLE dbo.Orders (
    OrderId INT IDENTITY(1,1) PRIMARY KEY,
    City NVARCHAR(50) NOT NULL,
    Amount DECIMAL(10,2) NOT NULL
);
INSERT INTO dbo.Orders (City, Amount) VALUES
 (N'Pune', 1200), (N'Pune', 800), (N'Mumbai', 2500), (N'Mumbai', 400);
SELECT City, COUNT(*) AS OrderCount, SUM(Amount) AS Revenue
FROM dbo.Orders
GROUP BY City
ORDER BY Revenue DESC;

What happened?

  • Rows group by City.
  • COUNT(*) is orders per city; SUM(Amount) is revenue.
  • Non-aggregated columns in SELECT must appear in GROUP BY.

Practice next

  1. Create and fill dbo.Orders.
  2. Run the grouped query.
  3. Add AVG(Amount) AS AvgOrder.
  4. GROUP BY LEFT(City, 1) for a fun rollup.
  5. Add HAVING SUM(Amount) > 1000.

Remember

GROUP BY builds one row per group key. Aggregates summarize each group. SELECT list rules are strict — group or aggregate.

City-wise sales board

Ops dashboard groups DataVerse Orders by City.

Outcome: Mumbai vs Pune revenue shows in one query.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Start with 1NF: Ensure that the table has no repeating groups or arrays, and each?
Short answer: record has a unique identifier. Real-world example (ShopNest) Product and Category are separate tables (normalized). The order line stores product id + price snapshot—not a giant duplicated product blob. Sa…
Mid PDF Detailed
Use Lookup Tables: For categories or repeated groups of data, use lookup tables?
Short answer: with primary keys and referential integrity rather than repeating the data. Real-world example (ShopNest) ShopNest’s SQL Server database stores customers, products, and orders. Good indexes and clear foreig…
Mid PDF Detailed
What are the differences between SQL and NoSQL databases?
Short answer: SQL Databases (Relational Databases): These are structured databases that use Structured Query Language (SQL) for defining and manipulating data. Explain a bit more They store data in tables with rows and c…
Mid PDF Detailed
Query Performance:?
Short answer: Use EXPLAIN or QUERY PLAN to analyze query execution times and identify slow queries. Track metrics like response time, execution time, and query throughput. Real-world example (ShopNest) ShopNest adds an i…
Junior PDF Detailed
Define Roles: Define different roles based on business requirements (e.g., admin,?
Short answer: Define Roles: Define different roles based on business requirements (e.g., admin,? is a common interview topic in SQL & Databases. Give a clear definition, then one concrete example. Say this in the int…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

SQL Server Tutorial
Course syllabus

SQL Server Tutorial

SQL — Foundations
SQL — SQL Queries & Clauses
SQL — Joins & Relationships
SQL — Indexing & Performance
SQL — Stored Procedures & Functions
SQL — Transactions & Concurrency
SQL — Advanced SQL Server
SQL — Security & High Availability
SQL — 2022 & Cloud
SQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details