Assessment 5

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Table – Employee Details

Emp Id Full Name Manager Id Date Of Joining City


121 John Snow 321 01/31/2014 Toronto
321 Walter White 986 01/30/2015 California
421 Kuldeep Rana 876 27/11/2016 New Delhi

Table – Employee Salary


Emp Id Project Salary Variable
121 P1 8000 500
321 P2 10000 1000
421 P1 12000 0

• Write an SQL query to fetch the Emp Id and Full Name of all the employees
working under Manager with id – ‘986’.
ans --- SELECT EmpId, FullName FROM EmployeeDetails WHERE ManagerId = 986 ;

• Write an SQL query to fetch the different projects available from the Employee
Salary table.
ans --- SELECT DISTINCT(Project) FROM EmployeeSalary ;

• Write an SQL query to fetch the count of employees working in project ‘P1’.
ans --- SELECT COUNT(EmpId) FROM EmployeeSalary WHERE Project = 'P1' ;
• Write an SQL query to find the maximum, minimum, and average salary of the
employees.
ans --- SELECT MAX(Salary), MIN(Salary) , AVG(Salary) FROM EmployeeSalary ;
• Write an SQL query to fetch those employees who live in Toronto and work under
manager with Manager Id – 321.
ans --- SELECT * FROM EmployeeDetails AS ED WHERE ED.City = 'Toronto' AND
ED.ManagerId = 321;
• Write an SQL query to fetch all the employees who either live in California or
work under a manager with Manager Id – 321.
ans --- SELECT * FROM EmployeeDetails WHERE City = 'California' OR ManagerId =
321;

• Write an SQL query to fetch all those employees who work on Project other than
P1.
ans --- SELECT ED.* FROM EmployeeDetails ED INNER JOIN EmployeeSalary ES ON
ED.EmpId = ES.EmpId WHERE Project <> 'P1';

ALTERNATE:
SELECT empid, name FROM employe_details ( ...... )

• Write an SQL query to display the total salary of each employee adding the
Salary with Variable value.
ans --- SELECT ED.FullName, (ES.Salary + ES.Variable) FROM EmployeeDetails ED
INNER JOIN EmployeeSalary ES ON ED.EmpId = ES.EmpId;
join me pehle small table likhte fir baad me bada table taki kam search krna pade

• Write an SQL query to fetch the employees whose name begins with any two
characters, followed by a text “hn” and ending with any sequence of characters.
ans --- SELECT * FROM EmployeeDetails WHERE FullName LIKE '__hn%';

• Write an SQL query to fetch the Emp Ids that are present in Employee Details but
not in Employee Salary.
ans --- SELECT * FROM EmployeeDetails ED LEFT JOIN EmployeeSalary ES ON
ED.EmpId = ES.EmpId WHERE ES.EmpId IS NULL;

• (12)Write an SQL query to fetch the employee full names and replace the space
with ‘-’.
ans --- SELECT replace(FullName,' ','-') FROM EmployeeDetails;

• Write an SQL query to fetch the position of a given character(s) in a field.


ans --- SELECT CHARINDEX('h', FullName) AS Position, FullName FROM
EmployeeDetails WHERE FullName LIKE '%h%';
// for position of h

• (13) Write an SQL query to find the count of the total occurrences of a particular
character – ‘n’ in the Full Name field.
ans --- SELECT SUM(length(FullName) - length(REPLACE(FullName, 'n', ''))) AS
CharacterCount FROM EmployeeDetails;

• Write an SQL query to update the employee names by removing leading and
trailing spaces.
ans --- UPDATE EmployeeDetails SET FullName = LTRIM(RTRIM(FullName));

• (15)Write an SQL query to fetch employee names having a salary greater than or
equal to 5000 and less than or equal to 10000.
ans --- SELECT ED.FullName FROM EmployeeDetails AS ED JOIN EmployeeSalary
AS ES ON ED.EmpId = ES.EmpId WHERE ES.Salary BETWEEN 5000 AND 10000;
• Write an SQL query to fetch all the Employees details from EmployeeDetails table
who joined in the Year 2020.
ans --- SELECT * FROM EmployeeDetails WHERE DateOfJoining BETWEEN '2020-
01-01' AND '2020-12-31';
• (17)Write an SQL query to fetch all employee records from EmployeeDetails table
who have a salary record in EmployeeSalary table.
ans --- SELECT ED.* FROM EmployeeDetails ED JOIN EmployeeSalary ES ON
ED.EmpId = ES.EmpId;
• Write an SQL query to fetch project-wise count of employees sorted by project’s
count in descending order.
ans --- SELECT Project, COUNT(EmpId) AS EmployeeCount FROM EmployeeSalary
GROUP BY Project ORDER BY EmployeeCount DESC;
• (19)Write a query to fetch employee names and salary records. Display the
employee details even if the salary record is not present for the employee.
ans --- SELECT ED.EmpId, ED.FullName, ES.Project, ES.Salary, ES.Variable FROM
EmployeeDetails AS ED LEFT JOIN EmployeeSalary AS ES ON ED.EmpId =
ES.EmpId;
• (20)Write an SQL query to join 3 tables.
ans --- SELECT A.* FROM A JOIN B ON A.id = B.A.id
• Write an SQL query to fetch duplicate records from EmployeeDetails (without
considering the primary key – EmpId).
ans --- SELECT ED1.* FROM EmployeeDetails ED1 INNER JOIN ( SELECT FullName,
ManagerId, DateOfJoining, City FROM EmployeeDetails GROUP BY FullName,
ManagerId, DateOfJoining, City HAVING COUNT(*) > 1) ED2 ON ED1.FullName =
ED2.FullName AND ED1.ManagerId = ED2.ManagerId AND ED1.DateOfJoining =
ED2.DateOfJoining AND ED1.City = ED2.City ORDER BY ED1.FullName,
ED1.ManagerId, ED1.DateOfJoining, ED1.City;
• (22)Write an SQL query to remove duplicates from a table without using a
temporary table.
ans --- ***DELETE FROM EmployeeDetails WHERE EmpId NOT IN ( SELECT
MIN(EmpId) FROM EmployeeDetails GROUP BY FullName, ManagerId,
DateOfJoining, City);
ALTERNATE SOLUTION:
ALTER TABLE EmployeeDetails ADD COLUMN INT AUTO_INCREMENT;
• (23)Write an SQL query to fetch only odd rows from the table.
ans --- WITH OddRowNumbers AS (SELECT *, ROW_NUMBER() OVER (ORDER BY
(SELECT 0)) AS RowNum FROM EmployeeDetails )
SELECT * FROM OddRowNumbers WHERE RowNum % 2 <> 0;
• (24)Write an SQL query to fetch only even rows from the table.
ans --- WITH EvenRowNumbers AS (SELECT *, ROW_NUMBER() OVER (ORDER BY
(SELECT 0)) AS RowNum FROM EmployeeDetails )
SELECT * FROM EvenRowNumbers WHERE RowNum % 2 = 0;
• (25) Write an SQL query to create an empty table with the same structure as
some other table.
ans --- CREATE TABLE EmptyEmployeeDetails AS SELECT * FROM EmployeeDetails
WHERE 1 = 0;
• Write an SQL query to fetch top n records?
ans ---SELECT * FROM EmployeeSalary ORDER BY Salary DESC LIMIT n;
• Write an SQL query to find the nth highest salary from table.
ans --- SELECT * FROM EmployeeSalary ORDER BY Salary DESC LIMIT 3 OFFSET 2;
• (28)Write SQL query to find the 3rd highest salary from a table without using the
TOP/limit keyword.
ans ---SELECT DISTINCT Salary FROM EmployeeSalary es1 WHERE 3 = ( SELECT
COUNT(DISTINCT Salary) FROM EmployeeSalary es2 WHERE es1.Salary <=
es2.Salary );
• Write an SQL query to fetch all the Employees who are also managers from the
EmployeeDetails table.
ans ---
• (30)Write an SQL query to find the current date-time.
ans --- SELECT Date();

You might also like