0% found this document useful (0 votes)
10 views7 pages

SQL Answers For 50 Questions

The document contains a series of SQL queries designed to retrieve various information from an EMP table. It includes commands to display employee details, filter by job titles, salaries, hire dates, and calculate aggregates like averages and totals. Additionally, it addresses specific conditions such as employees without managers and those with commissions above a certain threshold.
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)
10 views7 pages

SQL Answers For 50 Questions

The document contains a series of SQL queries designed to retrieve various information from an EMP table. It includes commands to display employee details, filter by job titles, salaries, hire dates, and calculate aggregates like averages and totals. Additionally, it addresses specific conditions such as employees without managers and those with commissions above a certain threshold.
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
You are on page 1/ 7

1.

Display all the information of the EMP table:


SQL
SELECT * FROM EMP;
2. Display unique Jobs from EMP table:
SQL
SELECT DISTINCT job FROM EMP;
3. List the emps in the ascending order of their Salaries:
SQL
SELECT * FROM EMP ORDER BY sal ASC;
4. Display all the details of all ‘Mgrs’
SQL
SELECT * FROM EMP WHERE job = 'MGR';
5. List the emps who joined before 1981:
SQL
SELECT * FROM EMP WHERE hiredate < '1981-01-01';
6. List the Empno, Ename, Sal of all emps working for Mgr 7369
SQL
SELECT empno, ename, sal FROM EMP WHERE mgr = 7369;
7. Display number and name of each employee, and calculates their annual
income
SQL
SELECT empno, ename, sal * 12 AS annual_income FROM EMP;
8. List all employees who are not salesmen
SQL
SELECT * FROM EMP WHERE job != 'SALESMAN';
9. List all employees who do not earn more than 1500
SQL
SELECT * FROM EMP WHERE sal <= 1500;
10. List all employees whose salary is in between 1000 and 2000
SQL
SELECT * FROM EMP WHERE sal BETWEEN 1000 AND 2000;
11. List the emps along with their Exp and Daily Sal is more than Rs.100
SQL
-- Assuming a 'hiredate' column and need to calculate experience (years)
SELECT empno, ename,
DATEDIFF(YEAR, CURDATE(), hiredate) AS experience,
sal / DAY(CURDATE()) AS daily_sal
FROM EMP
WHERE sal / DAY(CURDATE()) > 100;
12. List the emps who are either ‘CLERK’ or ‘ANALYST’ in the Desc order.
SQL
SELECT * FROM EMP WHERE job IN ('CLERK', 'ANALYST') ORDER BY job DESC;
13. List the emps who joined on 1-MAY-81,3-DEC-81,17-DEC-81,19-JAN-80 in asc
order of seniority.
SQL
SELECT * FROM EMP
WHERE hiredate IN ('1981-05-01', '1981-12-03', '1981-12-17', '1980-01-19')
ORDER BY hiredate ASC;
14. List the emps who are joined in the year 81.
SQL
SELECT * FROM EMP WHERE YEAR(hiredate) = 1981;
15. List the emps who are joined in the month of Aug 1980.
SQL
SELECT * FROM EMP WHERE MONTH(hiredate) = 8 AND YEAR(hiredate) = 1980;
16. List the Enames those are having five characters in their Names.
SQL
SELECT ename FROM EMP WHERE LENGTH(ename) = 5;
17. List the Enames those are starting with ‘S’ and with five characters.
SQL
SELECT ename FROM EMP WHERE ename LIKE 'S_____' AND LENGTH(ename) = 5;
18. List the emps those are having four chars and third character must be ‘r’
SQL
SELECT * FROM EMP WHERE LENGTH(ename) = 4 AND SUBSTRING(ename, 3, 1) =
'r';
19. List the emps whose names having a character set ‘ll’ together.
SQL
SELECT * FROM EMP WHERE ename LIKE '%ll%';
20. List the emps who joined in January.
SQL
SELECT * FROM EMP WHERE MONTH(hiredate) = 1;
21. List the details of the emps whose Salaries more than the employee BLAKE.
SQL
-- Assuming a 'sal' column for BLAKE's salary
SELECT * FROM EMP WHERE sal > (SELECT sal FROM EMP WHERE ename = 'BLAKE');

22. List the emps whose Jobs are same as ALLEN


SQL
SELECT * FROM EMP WHERE job = (SELECT job FROM EMP WHERE ename = 'ALLEN');
23. List the emps who are senior to King.
SQL
-- Assuming a 'hiredate' column
SELECT * FROM EMP WHERE hiredate < (SELECT hiredate FROM EMP WHERE ename =
'KING');
24. List the Emps of Deptno 20 whose Jobs are same as Deptno10
SQL
SELECT * FROM EMP WHERE deptno = 20 AND job IN (SELECT job FROM EMP WHERE
deptno = 10);
25. List the Emps whose Sal is same as FORD or SMITH in desc order of Sal
SQL
SELECT * FROM EMP
WHERE sal IN (SELECT sal FROM EMP WHERE ename IN ('FORD', 'SMITH'))
ORDER BY sal DESC;
26. List all the Grade2 and Grade 3 emps
Assuming a 'grade' column exists in the EMP table
SQL
SELECT * FROM EMP WHERE grade IN (2, 3);
27. Find the highest sal of EMP table.
SQL
SELECT MAX(sal) AS highest_salary FROM EMP;
28. Find details of highest paid employee.
SQL
SELECT * FROM EMP WHERE sal = (SELECT MAX(sal) FROM EMP);
29. Find the total sal given to the MGRs.
SQL
SELECT SUM(sal) AS total_sal_to_mgrs FROM EMP WHERE job = 'MGR';
30. Display the average salaries of all the clerks
SQL
SELECT AVG(sal) AS avg_clerk_sal FROM EMP WHERE job = 'CLERK';
31. Display the number of employee for each job group deptno wise.
SQL
This query cannot be answered without the DEPT table.
It requires information about department numbers (deptno).
32. List the department, details where at least two emps are working
SQL
This query cannot be answered without the DEPT table.
It requires department information to join with the EMP table.
33. Display the Grade, Number of emps, and max sal of each grade
Assuming a 'grade' column exists in the EMP table
SQL
SELECT grade, COUNT(*) AS number_of_emp, MAX(sal) AS max_sal_per_grade
FROM EMP
GROUP BY grade;
34. Display the emps whose manager name is jones
Assuming a 'mgr' column stores manager IDs and a separate table 'MGRS' stores
manager details
SQL
SELECT e.empno, e.ename, e.job, e.sal
FROM EMP e
INNER JOIN MGRS m ON e.mgr = m.mgr_id
WHERE m.mgr_name = 'jones';
35. List the employees whose salary is more than 3000 after giving 20% increment
SQL
SELECT empno, ename, sal * 1.2 AS incremented_sal
FROM EMP
WHERE sal * 1.2 > 3000;
36. List the emps who are not working in sales dept.
SQL
SELECT * FROM EMP WHERE job != 'SALESMAN';
37. List the emps name ,dept, sal and comm. For those whose salary is between
2000 and 5000 while loc is Chicago
SQL
SELECT ename, deptno, sal, comm
FROM EMP
WHERE loc = 'Chicago' AND sal BETWEEN 2000 AND 5000;
38. List the emps name, job who are with out manager.
SQL
SELECT empno, ename, job
FROM EMP
WHERE mgr IS NULL;
39. List the names of the emps who are getting the highest sal dept wise.
SQL
WITH dept_max_sal AS (
SELECT deptno, MAX(sal) AS max_sal
FROM EMP
GROUP BY deptno
)
SELECT e.ename, e.deptno
FROM EMP e
INNER JOIN dept_max_sal d

Answers for Questions 40-50 (Assuming an EMP table exists):

40. List the no. of emps in each department where the no. is more than 3.
SQL
SELECT deptno, COUNT(*) AS number_of_emp
FROM EMP
GROUP BY deptno
HAVING COUNT(*) > 3;

41. List the names of depts. Where atleast 3 are working in that department.
SQL
SELECT deptno
FROM EMP
GROUP BY deptno
HAVING COUNT(*) >= 3;
42. List the managers whose sal is more than his employess avg salary
SQL
WITH dept_avg_sal AS (
SELECT deptno, AVG(sal) AS avg_sal
FROM EMP
GROUP BY deptno
)
SELECT e.empno, e.ename
FROM EMP e
INNER JOIN dept_avg_sal d ON e.deptno = d.deptno
WHERE e.job = 'MGR' AND e.sal > d.avg_sal;
43. List the emps who are working as Managers
SQL
SELECT * FROM EMP WHERE job = 'MGR';
44. List the emps who joined in the company on the same date
SQL
SELECT * FROM EMP
GROUP BY hiredate
HAVING COUNT(*) > 1;
45. Count the No.of emps who are working as ‘Managers’
SQL
SELECT COUNT(*) AS number_of_mgrs FROM EMP WHERE job = 'MGR';
46. List those Managers who are getting less than his emps Salary.
SQL
WITH dept_avg_sal AS (
SELECT deptno, AVG(sal) AS avg_sal
FROM EMP
GROUP BY deptno
)
SELECT e.empno, e.ename
FROM EMP e
INNER JOIN dept_avg_sal d ON e.deptno = d.deptno
WHERE e.job = 'MGR' AND e.sal < d.avg_sal;
47. Find out how many Managers are their in the company
SQL
SELECT COUNT(*) AS number_of_mgrs FROM EMP WHERE job = 'MGR';
48. Find all the emps who earn the minimum Salary for each job wise in
ascending order.
SQL
WITH min_sal_per_job AS (
SELECT job, MIN(sal) AS min_sal
FROM EMP
GROUP BY job
)
SELECT e.empno, e.ename, e.job, e.sal
FROM EMP e
INNER JOIN min_sal_per_job m ON e.job = m.job AND e.sal = m.min_sal
ORDER BY e.job, e.sal ASC;
49. List the departments having employees in the ‘CLERK’ grade
Assuming a 'grade' column exists in the EMP table
SQL
SELECT deptno
FROM EMP
WHERE grade = 'CLERK'
GROUP BY deptno;
50. Display the details of all employees who have a commission greater than 500
SQL
SELECT * FROM EMP
WHERE comm > 500;

You might also like