This repository contains a collection of SQL exercises that introduce essential SQL functions and query operations used for data analysis and database management. The exercises focus on performing calculations, manipulating dates, combining data from multiple tables, handling missing values, and merging query results.
These concepts form the foundation of writing efficient SQL queries for reporting, business intelligence, and data analytics.
After completing these exercises, you should be able to:
- Use aggregate functions to summarize data.
- Apply SQL operators to filter and compare data.
- Manipulate and format dates using date functions.
- Retrieve related data using different types of joins.
- Handle NULL values effectively.
- Combine the results of multiple queries using
UNIONandUNION ALL.
This exercise introduces aggregate functions used to summarize data and SQL operators used to filter and compare records.
COUNT()SUM()AVG()MIN()MAX()
- Arithmetic Operators (
+,-,*,/) - Comparison Operators (
=,>,<,>=,<=,<>) - Logical Operators (
AND,OR,NOT) BETWEENINLIKE
- Calculate totals and averages.
- Count records.
- Identify minimum and maximum values.
- Filter records using conditions.
- Combine multiple conditions in SQL queries.
This exercise focuses on working with date and time values in SQL.
- Current date and time
- Extracting date components
- Date arithmetic
- Formatting dates
- Calculating date differences
CURRENT_DATECURRENT_TIMESTAMPNOW()YEAR()MONTH()DAY()DATEDIFF()DATEADD()(SQL Server)
- Retrieve the current system date.
- Extract year, month, and day values.
- Calculate the difference between dates.
- Perform date calculations for reporting and analysis.
This exercise introduces SQL joins, which combine related data from multiple tables based on common fields.
Returns matching records from both tables.
Returns all records from the left table and matching records from the right table.
Returns all records from the right table and matching records from the left table.
Returns all matching and non-matching records from both tables.
- Combine data from multiple tables.
- Understand relationships between tables.
- Retrieve complete datasets using appropriate join types.
- Analyze relational data effectively.
This exercise focuses on handling missing or unknown values stored as NULL.
- Understanding NULL values
- Replacing NULL values
- Comparing NULL values
COALESCE()IFNULL()(MySQL)ISNULL()(SQL Server)NULLIF()
- Detect missing values.
- Replace NULL values with default values.
- Prevent NULL values from affecting calculations.
- Improve data quality and query reliability.
This exercise demonstrates how to combine the results of two or more SELECT statements.
- Combines query results.
- Removes duplicate rows.
- Requires the same number of columns with compatible data types.
- Combines query results.
- Includes duplicate rows.
- Generally performs faster than
UNIONbecause duplicates are not removed.
- Merge data from multiple queries.
- Understand the difference between
UNIONandUNION ALL. - Choose the appropriate operation based on reporting requirements.
By completing these exercises, you will develop the ability to:
- Summarize data using aggregate functions.
- Filter records using SQL operators.
- Work with date and time values.
- Retrieve related information using joins.
- Handle missing data using NULL functions.
- Combine datasets using
UNIONandUNION ALL. - Write efficient and readable SQL queries.
Before attempting these exercises, you should be familiar with:
- Basic SQL syntax
SELECTstatementsFROMclauseWHEREclauseORDER BY- Basic database concepts
The SQL techniques covered in these exercises are commonly used in:
- Data Analytics
- Business Intelligence
- Database Administration
- Financial Reporting
- Sales and Marketing Analysis
- Inventory Management
- Customer Relationship Management (CRM)
These exercises provide practical experience with fundamental SQL functions and query operations that are widely used in relational databases. Mastering aggregates, operators, date functions, joins, NULL handling, and query unions enables you to retrieve, manipulate, and analyze data effectively, forming a strong foundation for advanced SQL and data analytics.