0% found this document useful (0 votes)
7 views3 pages

SQL Queries for Employee and Uber Data

The document outlines an SQL project involving multiple tables: EMP, DEPT, and Uber_rides, with specific queries to extract data. It includes tasks such as finding the top earners by department, analyzing Uber rides for business purposes, and querying for specific ride durations and stick lengths. Each query is designed to retrieve targeted information from the respective tables.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

SQL Queries for Employee and Uber Data

The document outlines an SQL project involving multiple tables: EMP, DEPT, and Uber_rides, with specific queries to extract data. It includes tasks such as finding the top earners by department, analyzing Uber rides for business purposes, and querying for specific ride durations and stick lengths. Each query is designed to retrieve targeted information from the respective tables.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL Project – 2

Table 1: EMP Table 2: DEPT


emp_id emp_name Salary dept_id dept_id dept_name
1 Jim 9989 1 1 IT
2 Kim 9770 2 2 Fin
3 Lim 4061 2 3 Sales
4 Tim 6053 3
5 Vim 2730 1
6 Jay 6354 1
7 Kay 6498 3
8 Raj 1702 3
9 Ram 5069 1
10 Sam 6244 2
11 Ali 4890 1
12 Joe 4813 3
13 Rim 8926 2
14 Doug 1595 2

Q1: Write a query to find the top 3 earner by department. Refer to EMP
and DEPT table to print the output.
Q2: Uber rides table contains the mileage and the purpose of the rides.
Write the query logic to find business purposes that generate the most
miles driven for passengers that use Uber for their business
transportation. Find the top 3 business purpose categories by total
mileage.

Table 3: Uber_rides

Q3: Write a query to find the data of 3rd highest miles from Uber rides
table.

Q4: Write a query to find the rides that have the duration >40 min
Table 4: Sticks Desired output

Q5: Sticks table (table 4 above) has 3 columns S1, S2, and S3. Columns
S1, S2, and S3 has length of sticks stored in them. Write a query logic to
get the desired output with the 4th column (stick_len).

Common questions

Powered by AI

To add a computed column that sums up stick lengths from columns S1, S2, and S3, use a simple arithmetic operation within the SELECT statement. The SQL query would be: SELECT S1, S2, S3, (S1 + S2 + S3) AS stick_len FROM Sticks. This query adds the lengths of sticks from columns S1, S2, and S3 and assigns it to the new column 'stick_len'.

To determine departments with more than two employees earning above 5000, calculate the count per department, then filter based on this count. The query: SELECT dept_id FROM EMP WHERE Salary > 5000 GROUP BY dept_id HAVING COUNT(emp_id) > 2. This query groups employees by department, counts those earning over 5000, and lists departments meeting the specified condition.

This query involves calculating the average salary for each department and then comparing individual salaries to these averages. Use the following SQL query: WITH DeptAverage AS (SELECT dept_id, AVG(Salary) AS avg_salary FROM EMP GROUP BY dept_id) SELECT emp_id, emp_name, Salary, EMP.dept_id FROM EMP INNER JOIN DeptAverage ON EMP.dept_id = DeptAverage.dept_id WHERE Salary < avg_salary. This approach involves calculating department-wide average salaries and joining them with the main EMP table to filter out employees with below-average salaries.

To find the Uber rides with the third highest mileage, a useful approach is to utilize a subquery to order the rides by mileage descending and then select the distinct mileage values. You can use the `ROW_NUMBER()` function to get the third highest explicitly by ranking the rides first. The SQL query would be: SELECT miles FROM (SELECT miles, ROW_NUMBER() OVER (ORDER BY miles DESC) AS rownum FROM Uber_rides) AS ranked_rides WHERE rownum = 3. This query ranks the rides by mileage and selects those with the third highest mileage.

To find the total combined salary for employees in the 'Sales' department, use an SQL query with filtering and aggregation: SELECT SUM(Salary) AS total_salary FROM EMP INNER JOIN DEPT ON EMP.dept_id = DEPT.dept_id WHERE DEPT.dept_name = 'Sales'. This query calculates the sum of all salaries for employees who belong to the 'Sales' department by joining EMP and DEPT tables.

To identify the distribution of employees across departments, a simple aggregation query can be used: SELECT dept_id, COUNT(emp_id) AS emp_count FROM EMP GROUP BY dept_id ORDER BY emp_count DESC. This query groups employees by department IDs, counts the number per department, and orders the results by count, providing a distribution overview across departments.

To merge the EMP and DEPT tables and display department names with employee details, an INNER JOIN is used. The SQL query would be: SELECT EMP.emp_id, EMP.emp_name, EMP.Salary, DEPT.dept_name FROM EMP INNER JOIN DEPT ON EMP.dept_id = DEPT.dept_id. This query retrieves employee names, salaries, and department names by joining the EMP table with the DEPT table on the department ID.

To find Uber rides longer than 40 minutes, you need to filter the rides based on the duration column. Assuming the Uber_rides table has a column named 'duration', the SQL query would be: SELECT * FROM Uber_rides WHERE duration > 40. This query selects all columns from the Uber_rides table where the duration exceeds 40 minutes.

To find the business purposes with the highest total mileage, you would use an aggregate function to sum mileage per business purpose and then rank or order these sums. The SQL query can be constructed as follows: SELECT business_purpose, SUM(mileage) AS total_mileage FROM Uber_rides WHERE business_purpose IS NOT NULL GROUP BY business_purpose ORDER BY total_mileage DESC LIMIT 3. This query calculates the total mileage for each business purpose and orders them in descending order, selecting the top three.

To retrieve the top three earners for each department, you need to perform a ranking function within each department. Using a Common Table Expression (CTE), you can partition the salaries by department and rank them. The query can be written as follows: WITH RANKED_SALARIES AS (SELECT emp_id, emp_name, Salary, dept_id, RANK() OVER (PARTITION BY dept_id ORDER BY Salary DESC) as rank FROM EMP) SELECT emp_id, emp_name, Salary, dept_id FROM RANKED_SALARIES WHERE rank <= 3;

You might also like