Sql Query Practice Problems
Enhance Your SQL Skills with Practice Problems
Sql Query Practice Problems
SQL query practice problems are a valuable resource for honing your SQL skills. These problems typically present a scenario or requirement that you need to translate into a SQL query. By solving these problems, you can improve your understanding of SQL syntax, data manipulation techniques, and query optimization. Practice problems often cover a wide range of topics, such as selecting data, filtering results, joining tables, aggregating data, and working with subqueries. Engaging with these problems can help you become more proficient in writing SQL queries, which is a crucial skill for anyone working with databases or data analysis.
To Download Our Brochure: https://www.justacademy.co/download-brochure-for-free
Message us for more information: +91 9987184296
1 - Write an SQL query to find the second highest salary from the Employee table.
Answer:
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1;
2) Write an SQL query to calculate the total number of employees in each department.
Answer:
SELECT Department, COUNT(*) AS TotalEmployees FROM Employee GROUP BY Department;
3) Write an SQL query to fetch the employee names with the same salary.
Answer:
SELECT Name, Salary FROM Employee GROUP BY Salary HAVING COUNT(*) > 1;
4) Write an SQL query to find employees who have been with the company for more than 5 years.
Answer:
SELECT Name, StartDate FROM Employee WHERE DATEDIFF(CURDATE(), StartDate) > 1825;
5) Write an SQL query to display the top 3 highest salaries from the Employee table.
Answer:
SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 3;
6) Write an SQL query to calculate the average salary of employees in each department.
Answer:
SELECT Department, AVG(Salary) AS AverageSalary FROM Employee GROUP BY Department;
7) Write an SQL query to find the employees who joined in the year 2020.
Answer:
SELECT Name, StartDate FROM Employee WHERE YEAR(StartDate) = 2020;
8) Write an SQL query to list the departments along with the total salary paid in each department.
Answer:
SELECT Department, SUM(Salary) AS TotalSalary FROM Employee GROUP BY Department;
9) Write an SQL query to find employees whose name starts with ‘A’ and have a salary greater than 50000.
To Download Our Brochure: Click Here
Message us for more information: +91 9987184296
Answer:
SELECT Name, Salary FROM Employee WHERE Name LIKE ‘A%’ AND Salary > 50000;
10) Write an SQL query to display the employee with the highest salary.
Answer:
SELECT * FROM Employee WHERE Salary = (SELECT MAX(Salary) FROM Employee);
11) Write an SQL query to fetch the employees who have the same job title and salary.
Answer:
SELECT Name, JobTitle, Salary FROM Employee GROUP BY JobTitle, Salary HAVING COUNT(*) > 1;
12) Write an SQL query to calculate the average years of service of employees in the company.
Answer:
SELECT AVG(DATEDIFF(CURDATE(), StartDate)) AS AverageYearsOfService FROM Employee;
13) Write an SQL query to find the employees who have the letter ‘a’ in their names.
Answer:
SELECT Name FROM Employee WHERE Name LIKE ‘%a%’;
14) Write an SQL query to count the number of employees in each salary range (e.g., 0 50000, 50001 100000, etc.).
Answer:
SELECT CASE WHEN Salary BETWEEN 0 AND 50000 THEN ‘0 50000’ WHEN Salary BETWEEN 50001 AND 100000 THEN ‘50001 100000’ ELSE ‘Above 100000’ END AS SalaryRange, COUNT(*) AS NumberOfEmployees FROM Employee GROUP BY SalaryRange;
15) Write an SQL query to find the second lowest salary from the Employee table.
Answer:
SELECT DISTINCT Salary FROM Employee ORDER BY Salary ASC LIMIT 1 OFFSET 1;
Browse our course links : https://www.justacademy.co/all-courses
To Join our FREE DEMO Session: Click Here
Contact Us for more info:
- Message us on Whatsapp: +91 9987184296
- Email id: info@justacademy.co
Azure Active Directory Interview Questions
Devops Technical Interview Questions
Array Programming Interview Questions In Java