The document contains SQL queries for various tasks related to an Employees table, including finding the highest-paid employee in each department, retrieving employees with salaries above average, counting employees per department, identifying duplicate names, joining with a Departments table, and finding the second-highest salary. Each task is accompanied by a corresponding SQL query. The queries demonstrate fundamental SQL operations such as selection, aggregation, and joins.
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 ratings0% found this document useful (0 votes)
10 views2 pages
demo.sql
The document contains SQL queries for various tasks related to an Employees table, including finding the highest-paid employee in each department, retrieving employees with salaries above average, counting employees per department, identifying duplicate names, joining with a Departments table, and finding the second-highest salary. Each task is accompanied by a corresponding SQL query. The queries demonstrate fundamental SQL operations such as selection, aggregation, and joins.
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
You are on page 1/ 2
3/22/25, 3:46 PM demo.
sql
demo.sql
1 You have a table called Employees with the following columns:
2 3 EmployeeID (integer) 4 5 Name (varchar) 6 7 Department (varchar) 8 9 Salary (integer) 10 11 Write a SQL query to find the highest-paid employee in each department. 12 13 Answer: 14 15 SELECT Department, Name, Salary 16 FROM Employees e 17 WHERE Salary = ( 18 SELECT MAX(Salary) 19 FROM Employees 20 WHERE Department = e.Department 21 ); 22 23 1. Retrieve Employees with Salaries Above Average: 24 Question: Find all employees whose salary is above the average salary. 25 26 SELECT Name, Salary 27 FROM Employees 28 WHERE Salary > (SELECT AVG(Salary) FROM Employees); 29 30 2. Count Employees per Department: 31 Question: Count the number of employees in each department. 32 33 SELECT Department, COUNT(*) AS EmployeeCount 34 FROM Employees 35 GROUP BY Department; 36 37 3. Find Duplicate Records: 38 Question: Identify duplicate employee names in the Employees table. 39 40 SELECT Name, COUNT(*) AS NameCount 41 FROM Employees 42 GROUP BY Name 43 HAVING COUNT(*) > 1; 44 45 4. Join Two Tables: 46 Question: Retrieve employee names and their department names from two tables: Employees and Departments. 47 48 SELECT e.Name, d.DepartmentName 49 FROM Employees e 50 JOIN Departments d ON e.DepartmentID = d.DepartmentID; 51 localhost:4699/b3ba8b78-5cc8-4d11-876b-ed64fe37e47b/ 1/2 3/22/25, 3:46 PM demo.sql
52 5. Find the Second-Highest Salary:
53 Question: Get the second-highest salary in the Employees table. 54 55 SELECT MAX(Salary) AS SecondHighestSalary 56 FROM Employees 57 WHERE Salary < (SELECT MAX(Salary) FROM Employees);