# Introduction to SQL Server

SQL Server, a relational database management system (RDBMS) developed by Microsoft, is a powerful tool for storing, managing, and retrieving data. It uses SQL (Structured Query Language) to interact with the database, enabling users to perform a wide range of operations. This post will cover the basics of SQL Server, focusing on SELECT, filtering, aggregate functions, and GROUP BY queries.

## 1\. Basic SELECT Queries

The SELECT statement is the foundation of SQL queries. It allows you to retrieve data from one or more tables.

### Example:

Suppose we have a table named `Employees` with the following columns: `EmployeeID`, `FirstName`, `LastName`, `Department`, and `Salary`.

To retrieve all columns from the `Employees` table, you would use:

```sql
SELECT * FROM Employees;
```

To retrieve specific columns:

```sql
SELECT FirstName, LastName, Department FROM Employees;
```

## 2\. Filtering Data with WHERE Clause

The `WHERE` clause is used to filter records that meet specific conditions.

### Example:

To find employees who work in the 'HR' department:

```sql
SELECT FirstName, LastName FROM Employees
WHERE Department = 'HR';
```

To find employees with a salary greater than $50,000:

```sql
SELECT FirstName, LastName, Salary FROM Employees
WHERE Salary > 50000;
```

## 3\. Aggregate Functions

Aggregate functions perform a calculation on a set of values and return a single value. Common aggregate functions include `COUNT`, `SUM`, `AVG`, `MAX`, and `MIN`.

### Example:

To count the number of employees in the `Employees` table:

```sql
SELECT COUNT(*) AS EmployeeCount FROM Employees;
```

To calculate the average salary of all employees:

```sql
SELECT AVG(Salary) AS AverageSalary FROM Employees;
```

To find the highest salary in the company:

```sql
SELECT MAX(Salary) AS HighestSalary FROM Employees;
```

## 4\. Grouping Data with GROUP BY

The `GROUP BY` clause groups rows that have the same values in specified columns into summary rows, often used with aggregate functions.

### Example:

To find the average salary for each department:

```sql
SELECT Department, AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department;
```

To count the number of employees in each department:

```sql
SELECT Department, COUNT(*) AS NumberOfEmployees
FROM Employees
GROUP BY Department;
```

## 5\. Filtering Groups with HAVING

The `HAVING` clause is used to filter groups based on a condition, similar to how the `WHERE` clause filters rows.

### Example:

To find departments with an average salary greater than $60,000:

```sql
SELECT Department, AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 60000;
```

> **NOTE**:  
> *Use* `WHERE` *clause for filtering before grouping and use* `HAVING` *clause for filtering data after grouping*

## Conclusion

SQL Server provides robust tools for managing and querying data. Understanding how to use basic SELECT statements, filtering with WHERE, employing aggregate functions, and grouping data with GROUP BY are essential skills for working with databases. With these fundamentals, you can efficiently retrieve and analyze data to support your business or project needs.
