0% found this document useful (0 votes)
9 views2 pages

SQL Questions

The document contains a series of SQL queries addressing various tasks such as finding the second-lowest salary, identifying employees with the same salary under the same manager, and calculating running totals. It also includes methods for handling null values, comparing salaries with department averages, and displaying total sales in a pivot format. Each query is designed to solve specific data retrieval or manipulation challenges in a database context.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

SQL Questions

The document contains a series of SQL queries addressing various tasks such as finding the second-lowest salary, identifying employees with the same salary under the same manager, and calculating running totals. It also includes methods for handling null values, comparing salaries with department averages, and displaying total sales in a pivot format. Each query is designed to solve specific data retrieval or manipulation challenges in a database context.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQL questions:

1. Find the second-lowest salary without using MIN

SELECT salary FROM employees


WHERE salary > (SELECT DISTINCT salary FROM employees ORDER BY salary ASC LIMIT 1)
ORDER BY salary ASC LIMIT 1;

2. Employees with the same salary under the same manager

SELECT e1.* FROM employees e1


JOIN employees e2 ON e1.manager_id = e2.manager_id
WHERE [Link] = [Link] AND e1.employee_id <> e2.employee_id;

3. Identify rows where a specific column has missing or null values and replace them with the column’s
average.

UPDATE table_name
SET column_name = (SELECT AVG(column_name) FROM table_name WHERE column_name IS NOT
NULL)
WHERE column_name IS NULL;

4. Top 5% salaries without percentile functions

SELECT * FROM employees


WHERE salary >= (SELECT salary FROM employees ORDER BY salary DESC LIMIT (SELECT COUNT(*) FROM
employees) * 5 / 100);

5. Calculate running total of sales

SELECT id, sales, SUM(sales) OVER (ORDER BY id) AS running_total


FROM sales_table;

6. Employees in multiple departments

SELECT employee_id FROM employee_department


GROUP BY employee_id
HAVING COUNT(DISTINCT department_id) > 1;
7. Compare salary with department average

SELECT employee_id, salary,


AVG(salary) OVER (PARTITION BY department_id) AS avg_dept_salary
FROM employees;

8. Departments with no employees

SELECT department_id FROM departments


WHERE department_id NOT IN (SELECT DISTINCT department_id FROM employees);

9. Write a query to display the total sales for each month in a pivot-like format.

SELECT
SUM(CASE WHEN month = 'January' THEN sales END) AS January,
SUM(CASE WHEN month = 'February' THEN sales END) AS February,
SUM(CASE WHEN month = 'March' THEN sales END) AS March
FROM sales_table;

10. Employees with >20% salary increase

SELECT e.employee_id FROM employees e


JOIN salary_history s ON e.employee_id = s.employee_id
WHERE [Link] > 1.2 * s.previous_salary;

You might also like