8. Querying & Aggregate Functions
Querying & Aggregate Functions
Data sitting in tables has no value until you ask it questions. A table with 500,000 student records is useless if you cannot retrieve the ones you need in milliseconds. This module teaches you to query data precisely — filtering rows, sorting results, and performing calculations that turn raw records into useful information.
Open SSMS, connect to VoidX_Academy, and run every example alongside the reading.
Ensuring You Are in the Right Database
Before writing any query, confirm which database is active. Run:
<div class='code-block' data-lang='text'> USE VoidX_Academy;
You can also see the active database in the dropdown at the top-left of the SSMS toolbar. It should show VoidX_Academy.
The SELECT Statement — Reading Data
All data retrieval in SQL begins with SELECT. The basic structure has three parts: which columns you want, which table to look in, and optionally which rows to include.
To retrieve every column from every row in the Student table:
<div class='code-block' data-lang='text'> SELECT * FROM Student;
The asterisk means all columns. It is useful for exploration, but in production you should always name the specific columns you need. This reduces the data transferred and makes your code clearer:
<div class='code-block' data-lang='text'> SELECT FirstName, LastName, Email FROM Student;
Filtering Rows with WHERE
You rarely want every row. Use WHERE to return only rows matching a condition.
Find all students in Department 1:
<div class='code-block' data-lang='text'> SELECT FirstName, LastName FROM Student WHERE DeptID = 1;
Combine multiple conditions with AND. Find students in Department 1 who also have a GPA above 3.0:
<div class='code-block' data-lang='text'> SELECT FirstName, LastName, GPA FROM Student WHERE DeptID = 1 AND GPA > 3.0;
Use OR when any one of several conditions should match. Find students in either Department 1 or Department 2:
<div class='code-block' data-lang='text'> SELECT FirstName, LastName, DeptID FROM Student WHERE DeptID = 1 OR DeptID = 2;
When checking the same column against several values, IN is cleaner than multiple ORs:
<div class='code-block' data-lang='text'> SELECT FirstName, LastName, DeptID FROM Student WHERE DeptID IN (1, 2);
To search for text patterns, use LIKE. The percent sign % is a wildcard meaning any sequence of characters. Find all students whose last name starts with A:
<div class='code-block' data-lang='text'> SELECT FirstName, LastName FROM Student WHERE LastName LIKE 'A%';
Sorting Results with ORDER BY
Without ORDER BY, SQL Server returns rows in no guaranteed order. Use ORDER BY to control sorting. ASC means ascending (smallest to largest, A to Z). DESC means descending.
Sort students alphabetically by last name:
<div class='code-block' data-lang='text'> SELECT FirstName, LastName FROM Student ORDER BY LastName ASC;
Sort by GPA, highest to lowest:
<div class='code-block' data-lang='text'> SELECT FirstName, LastName, GPA FROM Student ORDER BY GPA DESC;