0% found this document useful (0 votes)
84 views

Queries With Table

This document contains an EmployeeInfo table with employee details, an EmployeePosition table with employee positions and salaries, and 30 SQL queries with answers to retrieve information from the tables. The queries include selecting, filtering, aggregating, joining, unioning data from the tables.

Uploaded by

Akhilesh
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)
84 views

Queries With Table

This document contains an EmployeeInfo table with employee details, an EmployeePosition table with employee positions and salaries, and 30 SQL queries with answers to retrieve information from the tables. The queries include selecting, filtering, aggregating, joining, unioning data from the tables.

Uploaded by

Akhilesh
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
You are on page 1/ 4

EmployeeInfo Table:

EmpID EmpFname EmpLname Department Project Address DOB Gender


1 Sanjay Mehra HR P1 Hyderabad(HYD) 01/12/1976 M
2 Ananya Mishra Admin P2 Delhi(DEL) 02/05/1968 F
3 Rohan Diwan Account P3 Mumbai(BOM) 01/01/1980 M
4 Sonia Kulkarni HR P1 Hyderabad(HYD) 02/05/1992 F
5 Ankit Kapoor Admin P2 Delhi(DEL) 03/07/1994 M

EmployeePosition Table:

EmpID EmpPosition DateOfJoining Salary


1 Manager 01/05/2022 500000
2 Executive 02/05/2022 75000
3 Manager 01/05/2022 90000
2 Lead 02/05/2022 85000
1 Executive 01/05/2022 300000

1. Write a query to fetch the EmpFname from the EmployeeInfo table in the upper case
and use the ALIAS name as EmpName.
2. Write a query to fetch the number of employees working in the department ‘HR’.
3. Write a query to get the current date.
4. Write a query to retrieve the first four characters of EmpLname from the
EmployeeInfo table.
5. Write a query to fetch only the place name (string before brackets) from the Address
column of EmployeeInfo table.
6. Write a query to create a new table that consists of data and structure copied from
the other table.
7. Write q query to find all the employees whose salary is between 50000 to 100000.
8. Write a query to find the names of employees that begin with ‘S’
9. Write a query to fetch top N records.
10. Write a query to retrieve the EmpFname and EmpLname in a single column as
“FullName”. The first name and the last name must be separated with space.

Answer:
1. SELECT UPPER(EmpFname) AS EmpName FROM EmployeeInfo;
2. SELECT COUNT(*) FROM EmployeeInfo WHERE Department = 'HR';
3. SELECT GETDATE();
SELECT SYSTDATE();
4. SELECT SUBSTRING(EmpLname, 1, 4) FROM EmployeeInfo;
5. SELECT MID(Address, 0, LOCATE('(',Address)) FROM EmployeeInfo;
SELECT SUBSTRING(Address, 1, CHARINDEX('(',Address)) FROM EmployeeInfo;

6. SELECT * INTO NewTable FROM EmployeeInfo WHERE 1 = 0;


CREATE TABLE NewTable AS SELECT * FROM EmployeeInfo;

7. SELECT * FROM EmployeePosition WHERE Salary BETWEEN '50000' AND '100000';

8. SELECT * FROM EmployeeInfo WHERE EmpFname LIKE 'S%';


9. SELECT TOP N * FROM EmployeePosition ORDER BY Salary DESC;
SELECT * FROM EmpPosition ORDER BY Salary DESC LIMIT N;

10. SELECT CONCAT(EmpFname, ' ', EmpLname) AS 'FullName' FROM EmployeeInfo;


11. SELECT COUNT(*), Gender FROM EmployeeInfo WHERE DOB BETWEEN '02/05/1970 '
AND '31/12/1975' GROUP BY Gender;
12. SELECT * FROM EmployeeInfo ORDER BY EmpFname desc, Department asc;

13. SELECT * FROM EmployeeInfo WHERE EmpLname LIKE '____a';


14. SELECT * FROM EmployeeInfo WHERE EmpFname NOT IN ('Sanjay','Sonia');
15. SELECT * FROM EmployeeInfo WHERE Address LIKE 'DELHI(DEL)%';
16. SELECT E.EmpFname, E.EmpLname, P.EmpPosition
FROM EmployeeInfo E INNER JOIN EmployeePosition P ON
E.EmpID = P.EmpID AND P.EmpPosition IN ('Manager');
17. SELECT Department, count(EmpID) AS EmpDeptCount
FROM EmployeeInfo GROUP BY Department
ORDER BY EmpDeptCount ASC;

18. SELECT EmpID FROM (SELECT rowno, EmpID from EmployeeInfo) WHERE
MOD(rowno,2)=0;
SELECT EmpID FROM (SELECT rowno, EmpID from EmployeeInfo) WHERE MOD(rowno,2)=1;
19. SELECT * FROM EmployeeInfo E
WHERE EXISTS (SELECT * FROM EmployeePosition P WHERE E.EmpId = P.EmpId);
20. SELECT DISTINCT Salary FROM EmployeePosition E1

WHERE 2 >= (SELECTCOUNT(DISTINCT Salary)FROM EmployeePosition E2


   WHERE E1.Salary >= E2.Salary) ORDER BY E1.Salary DESC;

SELECT DISTINCT Salary FROM EmployeePosition E1


WHERE 2 >= (SELECTCOUNT(DISTINCT Salary) FROM EmployeePosition E2
   WHERE E1.Salary <= E2.Salary) ORDER BY E1.Salary DESC;
21. SELECT Salary
FROM EmployeePosition E1
WHERE N-1 = (
      SELECT COUNT( DISTINCT ( E2.Salary ) )
      FROM EmployeePosition E2
      WHERE E2.Salary >  E1.Salary );

22. SELECT EmpID, EmpFname, Department COUNT(*)


FROM EmployeeInfo GROUP BY EmpID, EmpFname, Department
HAVING COUNT(*) > 1;
23. Select DISTINCT E.EmpID, E.EmpFname, E.Department
FROM EmployeeInfo E, Employee E1
WHERE E.Department = E1.Department AND E.EmpID != E1.EmpID;

24. SELECT * FROM EmployeeInfo WHERE


EmpID <=3 UNION SELECT * FROM
(SELECT * FROM EmployeeInfo E ORDER BY E.EmpID DESC)
AS E1 WHERE E1.EmpID <=3;

25. SELECT TOP 1 salary


FROM(
SELECT TOP 3 salary
FROM employee_table
ORDER BY salary DESC) AS emp
ORDER BY salary ASC;
26. SELECT * FROM EmployeeInfo WHERE EmpID = (SELECT MIN(EmpID) FROM
EmployeeInfo);
SELECT * FROM EmployeeInfo WHERE EmpID = (SELECT MAX(EmpID) FROM EmployeeInfo);
27. SELECT Email FROM EmployeeInfo WHERE NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]
+@[A-Z0-9.-]+.[A-Z]{2,4}’, ‘i’);
28. SELECT DEPARTMENT, COUNT(EmpID) as 'EmpNo' FROM EmployeeInfo GROUP BY
DEPARTMENT HAVING COUNT(EmpD) < 2;
29. SELECT EmpPosition, SUM(Salary) from EmployeePosition GROUP BY EmpPosition;

30. SELECT *
FROM EmployeeInfo WHERE
EmpID <= (SELECT COUNT(EmpID)/2 from EmployeeInfo);

You might also like