Exercise- Aggregate Function and ROUND in SQL
Master SQL aggregates and precise number formatting
Core SQL Aggregate Functions
MAX and MIN
Find the highest and lowest values in a dataset. Perfect for price analysis and data range discovery.
COUNT
Count rows or non-null values. Essential for data validation and understanding dataset completeness.
SUM and AVG
Calculate totals and averages. Critical for financial calculations and statistical analysis.
Aggregate functions take multiple values from a column and return a single result. This is the fundamental concept that drives all SQL aggregation operations.
Setting Up Your SQL Environment
Choose Server
Select your database server from the connection dropdown. Most work will be done in the company data database.
Select Database
Default to company data for most exercises. Occasionally switch to jeopardy database for specific examples.
Identify Data
Examine your tables and columns to understand the data structure before writing aggregate queries.
Single Column vs Multiple Aggregates
| Feature | Single Aggregate | Multiple Aggregates |
|---|---|---|
| Result | One value | Multiple values in one row |
| Usage | Basic analysis | Comprehensive overview |
| Example | MAX(price) | MAX(price), MIN(price), AVG(price) |
If you aggregate one column, you must aggregate all columns in your SELECT statement. You cannot mix aggregated and non-aggregated columns.
COUNT Behavior with NULL Values
COUNT vs COUNT(column) vs COUNT DISTINCT
State Count Comparison
Revenue Calculation Impact
| Feature | Price Only | Price × Quantity |
|---|---|---|
| Total Revenue | $39,000 | $99,000 |
| Business Impact | Underestimated | Accurate |
| Decision Making | Poor decisions | Informed decisions |
Average Analysis Results
Professional Column Naming
Use Aliases
Add meaningful names to calculated columns using AS keyword for clarity and professionalism.
Handle Spaces
Use double quotes for column names with spaces. Single quotes are reserved for string values.
Be Descriptive
Choose names that clearly explain what the calculation represents, especially for exported data.
Number Formatting Options
| Feature | ROUND Function | CAST to MONEY |
|---|---|---|
| Default Behavior | Whole numbers | 2 decimal places |
| Decimal Control | Specify places | Fixed at 2 |
| Display Format | Plain number | Dollar sign prefix |
| Use Case | General rounding | Financial display |
The ROUND function accepts two parameters: the number to round and optionally the number of decimal places. Without the second parameter, it rounds to whole numbers.
This lesson is a preview from our SQL Course Online (includes software) and SQL Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways