SQL Queries
Here's a sample data structure for the "employees" table that you can use to solve the queries:
| employee_id | name | age | department | salary |
|-------------|-----------|-----|------------|--------|
| 1 | John | 30 | sales | 50000 |
| 2 | Emily | 35 | finance | 60000 |
| 3 | Michael | 40 | sales | 55000 |
| 4 | Sarah | 28 | finance | 62000 |
| 5 | David | 45 | HR | 48000 |
| 6 | Jessica | 32 | sales | 51000 |
| 7 | Andrew | 38 | finance | 58000 |
| 8 | Olivia | 29 | HR | 47000 |
Here are five SQL query questions for you to solve:
1. **Retrieve all columns from the "employees" table:**
2. **Retrieve the names and ages of all employees from the "employees" table:**
3. **Retrieve the names of all employees who are in the "sales" department from the "employees" table:**
4. **Count the number of employees in the "finance" department from the "employees" table:**
5. **Retrieve the names and salaries of all employees from the "employees" table, ordered by salary in descending order:**
- Write a query to find the average salary of all employees.
- Retrieve the maximum salary from the finance department.
- Get the total number of employees in each department.
- Find the employee with the highest salary.
- Calculate the average age of employees in the HR department.
Here are five questions where you would need to use the `GROUP BY` clause:
1. **Average Salary by Department:**
Write a query to find the average salary of employees in each department.
2. **Total Number of Employees by Department:**
Retrieve the total number of employees in each department.
3. **Maximum Salary by Age Group:**
Get the maximum salary for employees grouped by age ranges (e.g., 20-30, 31-40, etc.).
4. **Count of Employees by Department and Age Group:**
Find the count of employees grouped by department and age ranges.
5. **Total Salary Expenses by Department:**
Calculate the total salary expenses for each department.
These questions require grouping data based on specific criteria, such as department, age group, etc., to perform aggregate functions like `AVG`, `COUNT`, `MAX`, and `SUM`.
0 Comments