SQL Queries for Employee and Uber Data
SQL Queries for Employee and Uber Data
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;