LinkedIn SQL Questions
LinkedIn SQL Questions
provide the necessary SQL `CREATE TABLE` statements, followed by answers for each query. MySQL
uses slightly different syntax than Oracle, so I'll ensure to correct the queries accordingly.
SELECT ename FROM emp WHERE job = 'CLERK' AND sal > 3000;
-- 9. **Display employee numbers and names for employees who earn commission:**
SELECT empno, ename FROM emp WHERE comm IS NOT NULL AND comm > 0;
-- 11. **Display names of clerks, salesmen, or analysts earning more than 3000:**
SELECT ename FROM emp WHERE job IN ('CLERK', 'SALESMAN', 'ANALYST') AND sal > 3000;
-- 13. **Display employees who joined before June 30, 1990 or after December 31, 1990:**
SELECT * FROM emp WHERE hiredate < '1990-06-30' OR hiredate > '1990-12-31';
SHOW TABLES;
SELECT USER();
-- 18. **Display names of employees working in department 10, 20, 40, or as clerks, salesmen, or
analysts:**
SELECT ename FROM emp WHERE deptno IN (10, 20, 40) OR job IN ('CLERK', 'SALESMAN',
'ANALYST');
-- 19. **Display names of employees whose names start with the letter "S":**
-- 20. **Display names of employees whose names end with the letter "S":**
-- 21. **Display names of employees with the second alphabet as "A" in their names:**
-- 22. **Display names of employees whose name is exactly five characters long:**
SELECT * FROM emp WHERE empno NOT IN (SELECT mgr FROM emp WHERE mgr IS NOT NULL);
-- 25. **Display all rows from the `emp` table (wait for each screen full of information):**
-- MySQL does not have a `set pause on` command like Oracle.
SELECT * FROM emp;
SELECT SUM(sal) + SUM(IFNULL(comm, 0)) AS total_salary FROM emp WHERE deptno = 40 AND job
= 'ANALYST';
-- 39. **Display `empno`, `ename`, `deptno`, and `sal`, sorted by name, then department number,
then salary:**
SELECT empno, ename, deptno, sal FROM emp ORDER BY ename, deptno, sal;
-- 40. **Display employee names with their annual salary (in descending order of salary):**
SELECT ename, 12 * (sal + IFNULL(comm, 0)) AS annual_salary FROM emp ORDER BY annual_salary
DESC;
-- 41. **Display employee details with HRA (15%), DA (10%), PF (5%), and total salary:**
SELECT ename, sal, sal * 0.15 AS HRA, sal * 0.05 AS PF, sal * 0.10 AS DA,
(sal + sal * 0.15 + sal * 0.10 - sal * 0.05) AS total_salary
FROM emp ORDER BY total_salary DESC;
-- 42. **Display department numbers and total number of employees per department:**
-- 43. **Display job titles and the total number of employees per job:**
-- 44. **Display department numbers and total salary for each department:**
-- 45. **Display department numbers and the maximum and minimum salary for each department:**
SELECT deptno, MAX(sal) AS max_salary, MIN(sal) AS min_salary FROM emp GROUP BY deptno;
SELECT deptno, COUNT(*) FROM emp GROUP BY deptno HAVING COUNT(*) > 3;
SELECT job, SUM(sal) FROM emp GROUP BY job HAVING SUM(sal) > 40000;
SELECT job, COUNT(*) FROM emp GROUP BY job HAVING COUNT(*) > 3;
SELECT ename FROM emp WHERE sal = (SELECT MAX(sal) FROM emp);
SELECT empno, ename FROM emp WHERE job = 'CLERK' AND sal = (SELECT MAX(sal) FROM emp
WHERE job = 'CLERK');
-- 53. **Display salesmen who earn more than the highest paid clerk:**
SELECT ename FROM emp WHERE job = 'SALESMAN' AND sal > (SELECT MAX(sal) FROM emp
WHERE job = 'CLERK');
-- 54. **Display clerks who earn more than James but less than Scott:**
SELECT ename FROM emp WHERE job = 'CLERK' AND sal > (SELECT sal FROM emp WHERE ename =
'JAMES')
AND sal < (SELECT sal FROM emp WHERE ename = 'SCOTT');
-- 55. **Display employees who earn more than James but less than Scott:**
SELECT ename FROM emp WHERE sal > (SELECT sal FROM emp WHERE ename = 'JAMES')
AND sal < (SELECT sal FROM emp WHERE ename = 'SCOTT');
USE leetcode;
-- 1. Display the names of the employees who earn highest salary in their respective departments.
SELECT * FROM emp e WHERE sal = (SELECT MAX(sal) FROM emp WHERE deptno = e.deptno);
-- 2. Display the names of employees who earn highest salaries in their respective job groups.
SELECT * FROM emp e WHERE sal IN (SELECT MAX(sal) FROM emp GROUP BY job HAVING e.job = job);
-- 5. Display the job groups having total salary greater than the maximum salary for managers.
SELECT job, SUM(sal) FROM emp GROUP BY job HAVING SUM(sal) > (SELECT MAX(sal) FROM emp
WHERE job = 'MANAGER');
-- 6. Display the names of employees from department number 10 with salary greater than that of any
employee working in other departments.
SELECT ename, sal, deptno FROM emp e WHERE deptno = 10 AND sal > ANY (SELECT sal FROM emp
WHERE e.deptno != deptno);
-- 7. Display the names of employees from department number 10 with salary greater than that of all
employees working in other departments.
SELECT ename, sal, deptno FROM emp e WHERE deptno = 10 AND sal > ALL (SELECT sal FROM emp
WHERE e.deptno != deptno);
-- 11. Find out the length of your name using appropriate function.
SELECT LENGTH('India') FROM dual;
-- 13. Display the name of the employee concatenated with EMP no.
SELECT CONCAT(ename, empno) FROM emp;
-- 14. Use appropriate function and extract 3 characters starting from 2 characters from the following
string 'Oracle' i.e. the output should be 'rac'.
SELECT SUBSTR('Oracle', 2, 3) FROM dual;
-- 15. Find the first occurrence of character 'a' from the following string 'computer maintenance
corporation'.
SELECT INSTR('computer maintenance corporation', 'a', 1, 1) FROM dual;
-- 16. Replace every occurrence of alphabet 'A' with 'B' in the string 'Allens' (use translate function).
SELECT REPLACE('Allens', 'A', 'B') FROM dual;
-- 17. Display the information from EMP table. Wherever job 'manager' is found it should be displayed
as 'boss' (replace function).
SELECT empno, ename, REPLACE(job, 'MANAGER', 'Boss') AS job FROM emp;
-- 18. Display empno, ename, deptno from EMP table. Instead of displaying department numbers,
display the related department name (use decode function).
SELECT e.empno, e.ename, d.dname FROM emp e JOIN dept d ON e.deptno = d.deptno;
-- 19. Display your age in days.
SELECT ROUND(SYSDATE - TO_DATE('15-AUG-1947')) FROM dual;
-- 21. Display current date as '15th August Friday nineteen forty seven'.
SELECT TO_CHAR(SYSDATE, 'ddth Month Day Year') FROM dual;
-- 22. Display the following output for each row from EMP table as 'scott has joined the company on
Wednesday 13th August nineteen ninety'.
SELECT ename || ' has joined the company on ' || TO_CHAR(hiredate, 'Day ddth Month Year') FROM
emp;
-- 25. Display the date three months before the current date.
SELECT ADD_MONTHS(SYSDATE, -3) FROM dual;
USE leetcode;
-- 1. Display the names of the employees who earn highest salary in their respective departments.
SELECT * FROM emp e WHERE sal = (SELECT MAX(sal) FROM emp WHERE deptno = e.deptno);
-- 2. Display the names of employees who earn highest salaries in their respective job groups.
SELECT * FROM emp e WHERE sal IN (SELECT MAX(sal) FROM emp GROUP BY job HAVING e.job = job);
-- 5. Display the job groups having total salary greater than the maximum salary for managers.
SELECT job, SUM(sal) FROM emp GROUP BY job HAVING SUM(sal) > (SELECT MAX(sal) FROM emp
WHERE job = 'MANAGER');
-- 6. Display the names of employees from department number 10 with salary greater than that of any
employee working in other departments.
SELECT ename, sal, deptno FROM emp e WHERE deptno = 10 AND sal > ANY (SELECT sal FROM emp
WHERE e.deptno != deptno);
-- 7. Display the names of employees from department number 10 with salary greater than that of all
employees working in other departments.
SELECT ename, sal, deptno FROM emp e WHERE deptno = 10 AND sal > ALL (SELECT sal FROM emp
WHERE e.deptno != deptno);
-- 8. Display the names of employees in upper case.
SELECT UPPER(ename) FROM emp;
-- 11. Find out the length of your name using appropriate function.
SELECT LENGTH('India') FROM dual;
-- 13. Display the name of the employee concatenated with EMP no.
SELECT CONCAT(ename, empno) FROM emp;
-- 14. Use appropriate function and extract 3 characters starting from 2 characters from the following
string 'Oracle' i.e. the output should be 'rac'.
SELECT SUBSTR('Oracle', 2, 3) FROM dual;
-- 15. Find the first occurrence of character 'a' from the following string 'computer maintenance
corporation'.
SELECT INSTR('computer maintenance corporation', 'a', 1, 1) FROM dual;
-- 16. Replace every occurrence of alphabet 'A' with 'B' in the string 'Allens' (use translate function).
SELECT REPLACE('Allens', 'A', 'B') FROM dual;
-- 17. Display the information from EMP table. Wherever job 'manager' is found it should be displayed
as 'boss' (replace function).
SELECT empno, ename, REPLACE(job, 'MANAGER', 'Boss') AS job FROM emp;
-- 18. Display empno, ename, deptno from EMP table. Instead of displaying department numbers,
display the related department name (use decode function).
SELECT e.empno, e.ename, d.dname FROM emp e JOIN dept d ON e.deptno = d.deptno;
-- 21. Display current date as '15th August Friday nineteen forty seven'.
SELECT TO_CHAR(SYSDATE, 'ddth Month Day Year') FROM dual;
-- 22. Display the following output for each row from EMP table as 'scott has joined the company on
Wednesday 13th August nineteen ninety'.
SELECT ename || ' has joined the company on ' || TO_CHAR(hiredate, 'Day ddth Month Year') FROM
emp;
-- 26. Display the common jobs from department number 10 and 20.
SELECT job FROM emp WHERE deptno = 10 AND job IN (SELECT job FROM emp WHERE deptno = 20);
-- OR
SELECT job FROM emp WHERE deptno = 10 INTERSECT SELECT job FROM emp WHERE deptno = 20;
-- 27. Display the jobs found in department number 10 and 20 eliminate duplicate jobs.
SELECT DISTINCT(job) FROM emp WHERE deptno = 10 AND job IN (SELECT job FROM emp WHERE
deptno = 20);
-- OR
SELECT job FROM emp WHERE deptno = 10 INTERSECT SELECT job FROM emp WHERE deptno = 20;
-- 26. Display the common jobs from department number 10 and 20.
SELECT job FROM emp WHERE deptno = 10 AND job IN (SELECT job FROM emp WHERE deptno = 20);
-- OR
SELECT job FROM emp WHERE deptno = 10 INTERSECT SELECT job FROM emp WHERE deptno = 20;
-- 27. Display the jobs found in department number 10 and 20 eliminate duplicate jobs.
SELECT DISTINCT(job) FROM emp WHERE deptno = 10 AND job IN (SELECT job FROM emp WHERE
deptno = 20);
-- OR
SELECT job FROM emp WHERE deptno = 10 INTERSECT SELECT job FROM emp WHERE deptno = 20;
-- 29. Display the details of those who do not have any person working under them.
SELECT empno FROM emp WHERE empno NOT IN (SELECT mgr FROM emp WHERE mgr IS NOT NULL);
-- 30. Display the details of employees who are in sales dept and grade is 3.
SELECT * FROM emp WHERE sal >= (SELECT losal FROM salgrade WHERE grade = 3)
AND sal <= (SELECT hisal FROM salgrade WHERE grade = 3)
AND deptno = (SELECT deptno FROM dept WHERE dname = 'SALES');
-- 31. Display those who are not managers and who are managers any one.
SELECT * FROM emp WHERE empno IN (SELECT mgr FROM emp)
UNION
SELECT * FROM emp WHERE empno NOT IN (SELECT mgr FROM emp WHERE mgr IS NOT NULL);
-- 32. Display those employees whose name contains not less than 4 chars.
SELECT * FROM emp WHERE LENGTH(ename) >= 4;
-- 33. Display those departments whose name start with 'S' while location name end with 'O'.
SELECT * FROM dept WHERE dname LIKE 'S%' AND loc LIKE '%O';
-- 35. Display those employees whose salary is more than 3000 after giving 20% increment.
SELECT * FROM emp WHERE sal * 1.20 > 3000;
-- OR
SELECT * FROM emp WHERE sal + sal * 0.20 > 3000;
-- 38. Display employee name, deptname, salary and comm. for those Sal in between 2000 to 5000
while location is Chicago.
SELECT empno, ename, deptno FROM emp WHERE deptno = (SELECT deptno FROM dept WHERE loc =
'CHICAGO')
AND sal BETWEEN 2000 AND 5000;
-- 39. Display those employees whose salary is greater than their manager's salary.
SELECT * FROM emp e WHERE sal > (SELECT sal FROM emp WHERE empno = e.mgr);
-- 40. Display those employees who are working in the same dept where their manager is working.
SELECT * FROM emp e WHERE deptno = (SELECT deptno FROM emp WHERE empno = e.mgr);
-- 41. Display those employees who are not working under any manager.
SELECT * FROM emp WHERE mgr IS NULL OR empno = mgr;
-- 42. Display grade and employees name for the dept no 10 or 30 but grade is not 4, while joined the
company before 31-dec-82.
SELECT empno, ename, sal, deptno, hiredate, grade FROM emp e, salgrade s
WHERE e.sal >= s.losal AND e.sal <= s.hisal
AND deptno IN (10, 30) AND grade <> 4 AND hiredate < '1981-12-01';
-- 43. Update the salary of each employee by 10% increments that are not eligible for commission.
UPDATE emp SET sal = sal + (sal * 0.10) WHERE comm IS NULL;
-- 44. Delete those employees who joined the company before 31-dec-82 while their dept location is
'NEW YORK' or 'CHICAGO'.
DELETE FROM emp WHERE hiredate < '1982-12-31' AND deptno IN
(SELECT deptno FROM dept WHERE loc IN ('NEW YORK', 'CHICAGO'));
-- 45. Display employee name, job, deptname, location for all who are working as managers.
SELECT ename, job, dname, loc FROM emp e JOIN dept d ON e.deptno = d.deptno
WHERE empno IN (SELECT mgr FROM emp);
-- 46. Display those employees whose manager name is Jones, and also display their manager name.
SELECT e.empno, e.ename, m.ename AS MANAGER FROM emp e JOIN emp m
ON e.mgr = m.empno WHERE m.ename = 'JONES';
-- 47. Display name and salary of Ford if his salary is equal to high salary of his grade.
SELECT ename, sal FROM emp e WHERE ename = 'FORD' AND sal =
(SELECT hisal FROM salgrade WHERE grade =
(SELECT grade FROM salgrade WHERE e.sal >= losal AND e.sal <= hisal));
USE leetcode;
-- 1. Display employee name, his job, his dept name, his manager name, his grade and make out of an
under department wise.
SELECT d.deptno, e.ename, e.job, d.dname, m.ename AS manager, s.grade
FROM emp e
JOIN emp m ON e.mgr = m.empno
JOIN dept d ON e.deptno = d.deptno
JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal
ORDER BY e.deptno;
-- 2. List out all the employees name, job, and salary grade and department name for everyone in the
company except 'CLERK'. Sort on salary display the highest salary.
SELECT e.empno, e.ename, e.sal, d.dname, s.grade
FROM emp e
JOIN dept d ON e.deptno = d.deptno
JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal
WHERE e.job <> 'CLERK'
ORDER BY e.sal DESC;
-- 3. Display employee name, his job and his manager. Display also employees who are without
manager.
SELECT e.ename, e.job, m.ename AS manager
FROM emp e
LEFT JOIN emp m ON e.mgr = m.empno
UNION
SELECT ename, job, 'no manager'
FROM emp
WHERE mgr IS NULL;
-- 5. Display the name of those employees who are getting the highest salary.
SELECT empno, ename, sal
FROM emp
WHERE sal = (SELECT MAX(sal) FROM emp);
-- 6. Display those employees whose salary is equal to the average of maximum and minimum.
SELECT *
FROM emp
WHERE sal = (SELECT (MAX(sal) + MIN(sal)) / 2 FROM emp);
-- 7. Display count of employees in each department where count is greater than 3.
SELECT deptno, COUNT(*)
FROM emp
GROUP BY deptno
HAVING COUNT(*) > 3;
-- 8. Display dname where at least 3 are working and display only dname.
SELECT dname
FROM dept
WHERE deptno IN (SELECT deptno FROM emp GROUP BY deptno HAVING COUNT(*) > 3);
-- 9. Display name of those managers whose salary is more than the average salary of the company.
SELECT ename, sal
FROM emp
WHERE empno IN (SELECT mgr FROM emp)
AND sal > (SELECT AVG(sal) FROM emp);
-- 10. Display those managers whose salary is more than the average salary of their employees.
SELECT ename, sal
FROM emp e
WHERE empno IN (SELECT mgr FROM emp)
AND e.sal > (SELECT AVG(sal) FROM emp WHERE mgr = e.empno);
-- 11. Display employee name, Sal, comm and net pay for those employees whose net pay is greater
than or equal to any other employee's salary of the company.
SELECT ename, sal, comm, sal + IFNULL(comm, 0) AS netPay
FROM emp
WHERE sal + IFNULL(comm, 0) >= ANY (SELECT sal FROM emp);
-- 12. Display those employees whose salary is less than their manager but more than the salary of
any other managers.
SELECT *
FROM emp e
WHERE sal < (SELECT sal FROM emp WHERE empno = e.mgr)
AND sal > ANY (SELECT sal FROM emp WHERE empno != e.mgr);
-- 13. Display all employees' names with the total salary of the company with employee name.
SELECT ename, (SELECT SUM(sal) FROM emp) AS total_salary
FROM emp;
-- 15. Find out the number of employees whose salary is greater than their manager's salary.
SELECT COUNT(*)
FROM emp e
WHERE sal > (SELECT sal FROM emp WHERE empno = e.mgr);
-- 16. Display those managers who are not working under the president but they are working under
any other manager.
SELECT *
FROM emp e
WHERE mgr IN (SELECT empno FROM emp WHERE ename <> 'KING');
-- 17. Delete those departments where no employee is working.
DELETE FROM dept d
WHERE 0 = (SELECT COUNT(*) FROM emp WHERE deptno = d.deptno);
-- 18. Delete those records from EMP table whose deptno is not available in dept table.
DELETE FROM emp
WHERE deptno NOT IN (SELECT deptno FROM dept);
-- 19. Display those earners whose salary is out of the grade available in Sal grade table.
SELECT *
FROM emp
WHERE sal < (SELECT MIN(losal) FROM salgrade)
OR sal > (SELECT MAX(hisal) FROM salgrade);
-- 20. Display employee name, Sal, comm. and whose net pay is greater than any other in the
company.
SELECT ename, sal, comm
FROM emp
WHERE sal + sal * 0.15 - sal * 0.05 + sal * 0.10 =
(SELECT MAX(sal + sal * 0.15 - sal * 0.05 + sal * 0.10) FROM emp);
-- 21. Display name of those employees who are going to retire 31-dec-99. If the maximum job period
is 18 years.
SELECT *
FROM emp
WHERE (TO_DATE('31-dec-1999') - hiredate) / 365 > 18;
-- 24. Display those employees who joined the company in the month of DEC.
SELECT *
FROM emp
WHERE UPPER(TO_CHAR(hiredate, 'mon')) = 'DEC';
-- 27. Display those employees whose first 2 characters from hire date-last 2 characters of salary.
SELECT SUBSTR(hiredate, 1, 2) || SUBSTR(sal, LENGTH(sal) - 1, 2)
FROM emp;
-- OR
SELECT CONCAT(SUBSTR(hiredate, 1, 2), SUBSTR(sal, LENGTH(sal) - 1, 2))
FROM emp;
-- 28. Display those employees whose 10% of salary is equal to the year of joining.
SELECT *
FROM emp
WHERE TO_CHAR(hiredate, 'yy') = sal * 0.10;
-- 31. Display those employees who joined the company before 15th of the month.
SELECT empno, ename
FROM emp
WHERE hiredate < TO_DATE('15-' || TO_CHAR(hiredate, 'mon') || '-' || TO_CHAR(hiredate, 'yyyy'));
-- 32. Delete those records where the number of employees in a particular department is less than 3.
DELETE FROM emp
WHERE deptno IN (SELECT deptno FROM emp GROUP BY deptno HAVING COUNT(*) < 3);
-- 33. Delete those employees who joined the company 21 years back from today.
DELETE FROM emp
WHERE ROUND((SYSDATE - hiredate) / 365) > 21;
-- OR
DELETE FROM emp
WHERE (TO_CHAR(SYSDATE, 'yyyy') - TO_CHAR(hiredate, 'yyyy')) > 21;
-- 34. Display the department name the number of characters of which is equal to the number of
employees in any other department.
SELECT dname
FROM dept
WHERE LENGTH(dname) IN (SELECT COUNT(*) FROM emp GROUP BY deptno);
-- 36. Count the number of employees who are working as managers (use set operation).
SELECT COUNT(*)
FROM emp
WHERE empno IN (SELECT mgr FROM emp);
-- 37. Display the name of the department those employees who joined the company on the same
date.
SELECT empno, ename, hiredate, deptno
FROM emp e
WHERE hiredate IN (SELECT hiredate FROM emp WHERE empno <> e.empno);
-- 38. Display those employees whose grade is equal to any number of Sal but not equal to the first
number of Sal.
-- This query is unclear. Please provide more details or clarify the requirement.
-- 39. Display the manager who is having the maximum number of employees working under him.
SELECT mgr
FROM emp
GROUP BY mgr
HAVING COUNT(*) = (SELECT MAX(COUNT(mgr)) FROM emp GROUP BY mgr);
-- 40. List out employees' names and salary increased by 15% and expressed as a whole number of
dollars.
SELECT empno, ename, LPAD(CONCAT('$', ROUND(sal * 1.15)), 7) AS salary
FROM emp;
-- 41. Produce the output of the EMP table 'EMPLOYEE_AND_JOB' for ename and job.
SELECT *
FROM EMPLOYEE_AND_JOB;
-- 42. List all employees with hire date in the format 'June 4 1988'.
SELECT TO_CHAR(hiredate, 'Month DD YYYY')
FROM emp;
-- 43. Print a list of employees displaying 'Less Salary' if less than 1500, if exactly 1500 display as 'Exact
Salary' and if greater than 1500 display 'More Salary'.
SELECT empno, ename,
CASE
WHEN sal < 1500 THEN 'Less Salary ' || sal
WHEN sal = 1500 THEN 'Exact Salary ' || sal
ELSE 'More Salary ' || sal
END AS salary
FROM emp;
-- 44. Write query to calculate the length of employee has been with the company.
SELECT ROUND(SYSDATE - hiredate) AS length_of_service
FROM emp;
-- 45. Given a String of the format 'nn/nn', verify that the first and last 2 characters are numbers.
-- And that the middle character is 'y'. Print 'Yes' if valid, 'No' if not valid.
SELECT CASE
WHEN REGEXP_LIKE('nn/nn', '^[0-9]{2}/[0-9]{2}$') THEN 'Yes'
ELSE 'No'
END AS validation_result;
-- 46. Employees hired on 15th of any month are paid on the last Friday of that month.
-- Those hired after 15th are paid the last Friday of the following month.
-- Print a list of employees, their hire date, and first pay date, sorted by those whose Sal contains first
digits of their dept.
SELECT empno, ename, hiredate,
NEXT_DAY(LAST_DAY(hiredate) - 7, 'FRIDAY') AS first_pay_date
FROM emp
WHERE TO_CHAR(hiredate, 'DD') <= 15
UNION
SELECT empno, ename, hiredate,
NEXT_DAY(LAST_DAY(ADD_MONTHS(hiredate, 1)) - 7, 'FRIDAY') AS first_pay_date
FROM emp
WHERE TO_CHAR(hiredate, 'DD') > 15
ORDER BY INSTR(TO_CHAR(sal), TO_CHAR(deptno));
-- 47. Display those managers who are getting less than their employees' salary.
SELECT empno, ename
FROM emp e
WHERE sal < ANY (SELECT sal FROM emp WHERE mgr = e.empno);
-- 48. Print the details of all the employees who are subordinate to Blake.
SELECT *
FROM emp
WHERE mgr = (SELECT empno FROM emp WHERE ename = 'BLAKE');
-- ---------------------------------------------------------------------------------
-- Create the employee table
CREATE TABLE emp (
empno INT PRIMARY KEY,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate DATE,
sal DECIMAL(10, 2),
comm DECIMAL(10, 2),
deptno INT
);
-- 2. Display those employees whose manager's name is Jones and also with his manager's name.
SELECT * FROM emp WHERE mgr = (SELECT empno FROM emp WHERE ename = 'JONES')
UNION
SELECT * FROM emp WHERE empno = (SELECT mgr FROM emp WHERE ename = 'JONES');
-- 3. Define a variable representing the expression used to calculate an employee's total annual
remuneration.
select emp_ann_sal = (sal + NVL(comm, 0)) * 12;
-- 4. Use the variable in a statement which finds all employees who can earn 30,000 a year or more.
SELECT * FROM emp WHERE emp_ann_sal > 30000;
-- 5. Find out how many managers are there without listing them.
SELECT COUNT(*) FROM emp WHERE empno IN (SELECT mgr FROM emp);
-- 6. Find out the average salary and average total remuneration for each job type. Remember
salesmen earn commission.
SELECT job, AVG(sal + NVL(comm, 0)) AS avg_total_remuneration, SUM(sal + NVL(comm, 0)) AS
total_remuneration
FROM emp
GROUP BY job;
-- 8. List out the lowest paid employees working for each manager, exclude any groups where the
minimum salary is less than 1000, sort the output by salary.
SELECT e.ename, e.mgr, e.sal
FROM emp e
WHERE sal IN (SELECT MIN(sal) FROM emp WHERE mgr = e.mgr)
AND e.sal > 1000
ORDER BY sal;
-- 9. List ename, job, annual salary, deptno, dname, and grade for those who earn 30,000 per year and
who are not clerks.
SELECT e.ename, e.job, (e.sal + NVL(e.comm, 0)) * 12 AS annual_sal, e.deptno, d.dname, s.grade
FROM emp e
JOIN dept d ON e.deptno = d.deptno
JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal
WHERE (e.sal + NVL(e.comm, 0)) * 12 > 30000
AND e.job <> 'CLERK';
-- 10. Find out the job that was filled in the first half of 1983 and the same job that was filled during
the same period in 1984.
SELECT job
FROM emp
WHERE hiredate BETWEEN '1983-01-01' AND '1983-06-30'
INTERSECT
SELECT job
FROM emp
WHERE hiredate BETWEEN '1984-01-01' AND '1984-06-30';
-- 11. Find out all employees who joined the company before their manager.
SELECT * FROM emp e WHERE hiredate < (SELECT hiredate FROM emp WHERE empno = e.mgr);
-- 12. List out all employees by name and number along with their manager’s name and number. Also
display 'No Manager' for those who have no manager.
SELECT e.empno, e.ename, m.empno AS Manager, m.ename AS ManagerName
FROM emp e
LEFT JOIN emp m ON e.mgr = m.empno
UNION
SELECT empno, ename, mgr, 'No Manager' FROM emp WHERE mgr IS NULL;
-- 13. Find out the employees who earned the highest salary in each job type, sorted in descending
salary order.
SELECT * FROM emp e WHERE sal = (SELECT MAX(sal) FROM emp WHERE job = e.job) ORDER BY sal
DESC;
-- 14. Find out the employees who earned the minimum salary for their job in ascending order.
SELECT * FROM emp e WHERE sal = (SELECT MIN(sal) FROM emp WHERE job = e.job) ORDER BY sal;
-- 15. Find out the most recently hired employees in each department, ordered by hire date.
SELECT * FROM emp ORDER BY deptno, hiredate DESC;
-- 16. Display ename, sal, and deptno for each employee who earns a salary greater than the average
of their department, ordered by deptno.
SELECT ename, sal, deptno FROM emp e WHERE sal > (SELECT AVG(sal) FROM emp WHERE deptno =
e.deptno) ORDER BY deptno;
-- 18. Display the department with the highest annual remuneration bill as compensation.
SELECT deptno, SUM(sal) AS total_sal FROM emp GROUP BY deptno HAVING SUM(sal) = (SELECT
MAX(SUM(sal)) FROM emp GROUP BY deptno);
-- 19. In which year did most people join the company? Display the year and number of employees.
SELECT TO_CHAR(hiredate, 'YYYY') AS year, COUNT(*) AS num_employees FROM emp GROUP BY
TO_CHAR(hiredate, 'YYYY') ORDER BY num_employees DESC;
-- 22. Display employees who can earn more than the lowest salary in dept no 30.
SELECT * FROM emp WHERE sal > (SELECT MIN(sal) FROM emp WHERE deptno = 30);
-- 23. Find employees who can earn more than every employee in dept no 30.
SELECT * FROM emp WHERE sal > (SELECT MAX(sal) FROM emp WHERE deptno = 30);
-- Alternative using ALL
SELECT * FROM emp WHERE sal > ALL (SELECT sal FROM emp WHERE deptno = 30);
-- 25. Find out the average salary and average total remuneration for each job type.
SELECT job, AVG(sal) AS avg_sal, AVG(sal + NVL(comm, 0)) AS avg_total_remuneration
FROM emp
GROUP BY job;
-- 27. Calculate the next pay day from their hire date for employees in the emp table.
SELECT ename, hiredate,
NEXT_DAY(TRUNC(hiredate, 'MM') + 15, 'FRIDAY') AS next_pay_day
FROM emp
WHERE TO_CHAR(hiredate, 'DD') <= 15
UNION
SELECT ename, hiredate,
NEXT_DAY(TRUNC(hiredate, 'MM') + 30, 'FRIDAY') AS next_pay_day
FROM emp
WHERE TO_CHAR(hiredate, 'DD') > 15;
-- 28. Calculate the review date for an employee hired today, which is 9 months after the joined date
and on the 1st of the next month.
SELECT ename, hiredate,
ADD_MONTHS(TRUNC(hiredate, 'MM'), 10) AS review_date
FROM emp;
-- 29. Display employee name and salary whose salary is greater than the highest average of dept no.
SELECT ename, sal
FROM emp
WHERE sal > (SELECT MAX(AVG(sal))
FROM emp
GROUP BY deptno);
-- 30. Display the 10th record of the EMP table (without using ROWID).
SELECT *
FROM emp
ORDER BY empno
LIMIT 1 OFFSET 9;
-- 31. Display half of the enames in upper case and the remaining in lower case.
SELECT CONCAT(UPPER(SUBSTR(ename, 1, LENGTH(ename) / 2)),
LOWER(SUBSTR(ename, LENGTH(ename) / 2 + 1))) AS formatted_ename
FROM emp;
-- 32. Display the 10th record of the EMP table without using GROUP BY and ROWID.
SELECT *
FROM (SELECT emp.*, ROW_NUMBER() OVER (ORDER BY empno) AS row_num
FROM emp)
WHERE row_num = 10;
-- 37. Display those employees whose joining month and grade are equal.
SELECT e.empno, e.ename
FROM emp e
JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal
WHERE TO_CHAR(e.hiredate, 'MM') = s.grade;
-- 38. Display those employees whose joining date is available in dept no.
SELECT *
FROM emp
WHERE TO_CHAR(hiredate, 'DD') = deptno;
-- 40. List out the employees' ename, sal, and PF from emp.
SELECT ename, sal, sal * 0.15 AS PF
FROM emp;
-- 47. Add a validation saying that salary cannot be greater than 10,000.
ALTER TABLE emp ADD CONSTRAINT emp_sal_check CHECK (sal < 10000);
-- 51. Add a foreign key constraint to the mgr column, relating it to empno.
ALTER TABLE emp ADD CONSTRAINT emp_mgr FOREIGN KEY (mgr) REFERENCES emp (empno);
-- 52. Add deptno column to your emp table.
ALTER TABLE emp ADD deptno NUMBER(3);
-- 53. Add a foreign key constraint to the deptno column, relating it to the dept table.
ALTER TABLE emp ADD CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno) REFERENCES dept
(deptno);
-- 54. Create a table called newemp and copy data from emp.
CREATE TABLE newemp AS SELECT * FROM emp;
-- 55. Create a table called newemp with only empno, ename, and dname.
CREATE TABLE newemp AS
SELECT e.empno, e.ename, d.dname
FROM emp e
JOIN dept d ON e.deptno = d.deptno;
-- 56. Delete rows of employees who have been working in the company for more than 2 years.
DELETE FROM emp WHERE FLOOR(SYSDATE - hiredate) > 2 * 365;
-- 57. Provide a commission to employees who are not earning any commission.
UPDATE emp SET comm = 300 WHERE comm IS NULL;
-- 58. Increment commission by 10% of salary for employees who have commission.
UPDATE emp SET comm = comm + (sal * 0.10) WHERE comm IS NOT NULL;
-- 59. Display employee name and department name for each employee.
SELECT e.ename, d.dname
FROM emp e
JOIN dept d ON e.deptno = d.deptno;
-- 60. Display employee number, name, and location of the department in which they are working.
SELECT e.empno, e.ename, d.loc
FROM emp e
JOIN dept d ON e.deptno = d.deptno;
-- 61. Display ename and dname even if there are no employees working in a particular department
(use outer join).
SELECT e.ename, d.dname
FROM emp e
RIGHT JOIN dept d ON e.deptno = d.deptno;
-- 63. Display the department name along with the total salary in each department.
SELECT d.dname, SUM(e.sal) AS total_salary
FROM emp e
JOIN dept d ON e.deptno = d.deptno
GROUP BY d.dname;
-- 64. Display the department name and total number of employees in each department.
SELECT d.dname, COUNT(e.empno) AS total_employees
FROM emp e
JOIN dept d ON e.deptno = d.deptno
GROUP BY d.dname;
-- 65. Add a foreign key constraint to emp1 table relating deptno to dept table.
ALTER TABLE emp1 ADD CONSTRAINT emp1_deptno_fk FOREIGN KEY (deptno) REFERENCES dept
(deptno);
-- 68. Check if the above changes are reflected in the other user.
-- This step involves using another client system to verify changes, which cannot be demonstrated in a
single SQL query.
-- 71. List all the employees who have at least one person reporting to them.
SELECT DISTINCT a.ename
FROM emp a, emp b
WHERE a.empno = b.mgr;
-- OR
SELECT ename
FROM emp
WHERE empno IN (SELECT mgr FROM emp);
-- 72. List the name of the employees with their immediate higher authority.
SELECT a.ename AS "EMPLOYEE", b.ename AS "REPORTS TO"
FROM emp a, emp b
WHERE a.mgr = b.empno;
-- 74. Write a query to display a '*' against the row of the most recently hired employee.
SELECT ename, hiredate, LPAD('*', 8) AS "RECENTLY HIRED"
FROM emp
WHERE hiredate = (SELECT MAX(hiredate) FROM emp)
UNION
SELECT ename, hiredate, LPAD(' ', 15) AS "RECENTLY HIRED"
FROM emp
WHERE hiredate != (SELECT MAX(hiredate) FROM emp);
-- 75. Write a correlated sub-query to list out the employees who earn more than the average salary
of their department.
SELECT ename, sal
FROM emp e
WHERE sal > (SELECT AVG(sal) FROM emp f WHERE e.deptno = f.deptno);
-- 77. Select the duplicate records (Records, which are inserted, that already exist) in the EMP table.
SELECT *
FROM emp a
WHERE a.empno IN (SELECT empno FROM emp GROUP BY empno HAVING COUNT(empno) > 1)
AND a.rowid != (SELECT MIN(rowid) FROM emp b WHERE a.empno = b.empno);
-- 78. Write a query to list the length of service of the employees (of the form n years and m months).
SELECT ename AS "EMPLOYEE",
TO_CHAR(TRUNC(MONTHS_BETWEEN(SYSDATE, hiredate) / 12)) || ' YEARS ' ||
TO_CHAR(TRUNC(MOD(MONTHS_BETWEEN(SYSDATE, hiredate), 12))) || ' MONTHS ' AS "LENGTH OF
SERVICE"
FROM emp;
-- 81. To select salary and its occurrences according to its order in ascending.
SELECT sal, COUNT(*)
FROM emp
GROUP BY sal
ORDER BY sal;
-- 91. To print each department having salary greater than 1000 including the salary, deptno, and their
count.
SELECT dname, a.deptno, sal, COUNT(*)
FROM emp a, dept b
WHERE a.deptno = b.deptno
HAVING sal > 1000
GROUP BY a.deptno, dname, sal
ORDER BY dname;