0% found this document useful (0 votes)
20 views4 pages

SQL Query Practice with Solutions

The document contains a series of SQL practice questions along with their solutions. Each question focuses on different SQL concepts such as fetching employee data, calculating salaries, and using window functions. Expected outputs are provided for each query to illustrate the results.

Uploaded by

Manu Tyagi
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)
20 views4 pages

SQL Query Practice with Solutions

The document contains a series of SQL practice questions along with their solutions. Each question focuses on different SQL concepts such as fetching employee data, calculating salaries, and using window functions. Expected outputs are provided for each query to illustrate the results.

Uploaded by

Manu Tyagi
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 Practice Question Bank with Solutions

Q1. Write an SQL query to fetch all employees who earn more
than 50,000 salary.

Solution Query:
SELECT Name, Salary FROM Employees WHERE Salary > 50000;

Expected Output:
Name Salary

Rahul 60000
Sneha 55000

Q2. Find the second highest salary from Employees table.

Solution Query:
SELECT MAX(Salary) AS SecondHighest FROM Employees WHERE Salary < (SELECT
MAX(Salary) FROM Employees);

Expected Output:
SecondHighest

60000

Q3. Display department-wise average salary of employees.

Solution Query:
SELECT Dept, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Dept;

Expected Output:
Dept AvgSalary

IT 60000
HR 50000
Q4. Retrieve employee names who work in both 'IT' and 'HR'
departments.

Solution Query:
SELECT Name FROM Employees WHERE Dept = 'IT' INTERSECT SELECT Name FROM
Employees WHERE Dept = 'HR';

Expected Output:
Name

Neha

Q5. Write a query to fetch top 3 highest salaries using window


functions.

Solution Query:
SELECT Name, Salary FROM (SELECT Name, Salary, RANK() OVER (ORDER BY
Salary DESC) AS rnk FROM Employees) t WHERE rnk <= 3;

Expected Output:
Name Salary

Rohit 70000
Rahul 60000
Sneha 55000

Q6. Create a query to count employees in each department having


more than 2 employees.

Solution Query:
SELECT Dept, COUNT(*) AS EmpCount FROM Employees GROUP BY Dept HAVING
COUNT(*) > 2;

Expected Output:
Dept EmpCount

IT 3

Q7. Find all employees who do not have any manager assigned.
Solution Query:
SELECT Name FROM Employees WHERE ManagerID IS NULL;

Expected Output:
Name

Amit
Rohit

Q8. Display employees who joined in the year 2023.

Solution Query:
SELECT Name, JoinDate FROM Employees WHERE YEAR(JoinDate) = 2023;

Expected Output:
Name JoinDate

Sneha 2023-01-12
Neha 2023-07-18

Q9. Write an SQL query to fetch the highest salary from each
department.

Solution Query:
SELECT Dept, MAX(Salary) AS MaxSalary FROM Employees GROUP BY Dept;

Expected Output:
Dept MaxSalary

IT 70000
HR 55000

Q10. Retrieve the first employee (by joining date) from each
department.

Solution Query:
SELECT Name, Dept, JoinDate FROM (SELECT Name, Dept, JoinDate,
ROW_NUMBER() OVER (PARTITION BY Dept ORDER BY JoinDate ASC) AS rn FROM
Employees) t WHERE rn = 1;
Expected Output:
Name Dept JoinDate

Amit IT 2021-03-01
Sneha HR 2022-05-20

You might also like