SQL Subqueries for Data Analysis

SQLSQLBeginner
Practice Now

Introduction

In this project, you will learn how to use subqueries to retrieve relevant information from the employee (emp) and department (dept) tables in the personnel database. You will practice writing complex SQL queries to access and analyze data from multiple tables.

👀 Preview

Preview

🎯 Tasks

In this project, you will learn:

  • How to start the MySQL server and import the personnel database
  • How to use a subquery to find the employee with the highest salary
  • How to calculate the proportion of employees in a specific department compared to the entire company
  • How to retrieve all employees working in a specific location using a subquery
  • How to find employees whose salary exceeds the average salary in their department

🏆 Achievements

After completing this project, you will be able to:

  • Understand the concept and use of subqueries in SQL
  • Write complex SQL queries that combine data from multiple tables
  • Analyze and extract meaningful insights from a database using subqueries
  • Demonstrate your SQL skills in a practical, real-world scenario

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL mysql(("`MySQL`")) -.-> mysql/BasicKeywordsandStatementsGroup(["`Basic Keywords and Statements`"]) sql(("`SQL`")) -.-> sql/BasicSQLCommandsGroup(["`Basic SQL Commands`"]) sql(("`SQL`")) -.-> sql/DataManipulationandQueryingGroup(["`Data Manipulation and Querying`"]) sql(("`SQL`")) -.-> sql/AdvancedDataOperationsGroup(["`Advanced Data Operations`"]) sql(("`SQL`")) -.-> sql/DatabaseManagementandOptimizationGroup(["`Database Management and Optimization`"]) mysql/BasicKeywordsandStatementsGroup -.-> mysql/source("`External Code Execution`") sql/BasicSQLCommandsGroup -.-> sql/select("`SELECT statements`") sql/DataManipulationandQueryingGroup -.-> sql/where("`WHERE clause`") sql/DataManipulationandQueryingGroup -.-> sql/in("`IN clause`") sql/AdvancedDataOperationsGroup -.-> sql/join("`JOIN operations`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/select("`Data Retrieval`") sql/DataManipulationandQueryingGroup -.-> sql/group_by("`GROUP BY clause`") sql/DataManipulationandQueryingGroup -.-> sql/subqueries("`Subqueries`") sql/AdvancedDataOperationsGroup -.-> sql/numeric_functions("`Numeric functions`") sql/DatabaseManagementandOptimizationGroup -.-> sql/using_indexes("`Using Indexes`") subgraph Lab Skills mysql/source -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} sql/select -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} sql/where -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} sql/in -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} sql/join -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} mysql/select -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} sql/group_by -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} sql/subqueries -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} sql/numeric_functions -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} sql/using_indexes -.-> lab-301418{{"`SQL Subqueries for Data Analysis`"}} end

Start MySQL and Import the Database

In this step, you will learn how to start the MySQL server and import the personnel database.

  1. Start the MySQL server using the sudo command without any password:
sudo service mysql start
  1. Access the MySQL prompt:
sudo mysql
  1. Import the data from the personnel.sql file into the MySQL database:
SOURCE /home/labex/project/personnel.sql;

This will create the personnel database and populate it with the necessary tables and data.

Query the Employee With the Highest Salary

In this step, you will learn how to use a subquery to retrieve the name of the employee with the highest salary.

  1. Create a new file named subquery.sql in the /home/labex/project directory.
  2. In the subquery.sql file, add the following SQL query:
-- Query to retrieve the employee(s) with the highest salary
SELECT ename AS `Name` FROM emp WHERE sal = (SELECT MAX(sal) FROM emp);

This query first finds the maximum salary in the emp table using a subquery, and then selects the ename (employee name) of the employee(s) who have that maximum salary.

Calculate the Proportion of Employees in Department 10

In this step, you will learn how to use a subquery to calculate the proportion of employees in department 10 compared to the entire company.

  1. In the subquery.sql file, add the following SQL query:
-- Query to calculate the proportion of employees in department 10
SELECT d.dname AS Department, COUNT(e.empno) / (SELECT COUNT(empno) FROM emp) AS Proportion
FROM emp e
JOIN dept d ON e.deptno = d.deptno
WHERE d.deptno = 10;

This query first counts the number of employees in department 10 by joining the emp and dept tables. It then divides this count by the total number of employees in the emp table (using a subquery) to calculate the proportion.

Retrieve Employees Working in New York

In this step, you will learn how to use a subquery to retrieve all employees working in the New York location.

  1. In the subquery.sql file, add the following SQL query:
-- Query to retrieve all employees working in New York
SELECT e.*
FROM emp e
JOIN dept d ON e.deptno = d.deptno
WHERE d.loc = 'NEW YORK';

This query joins the emp and dept tables to retrieve the employees whose department is located in New York.

Retrieve Employees With Salaries Exceeding the Department Average

In this step, you will learn how to use a subquery to retrieve employees whose salary is higher than the average salary in their respective department.

  1. In the subquery.sql file, add the following SQL query:
-- Query to retrieve employees whose salary is higher than the average salary in their respective department
SELECT e.* FROM emp e
JOIN (SELECT deptno, AVG(sal) AS avg_sal FROM emp GROUP BY deptno) AS avg
ON e.deptno = avg.deptno
WHERE e.sal > avg.avg_sal;

This query first calculates the average salary for each department using a subquery. It then joins this subquery with the emp table to select the employees whose salary is higher than the average salary in their department.

After completing these steps, your subquery.sql file should contain all the necessary SQL queries to fulfill the project requirements.

  1. Save the file.
  2. In the MySQL prompt, run the following command to execute the subquery.sql script:
SOURCE /home/labex/project/subquery.sql;

You should see the following output:

Preview

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other SQL Tutorials you may like