0% found this document useful (0 votes)
27 views28 pages

LinkedIn SQL Questions

The document provides SQL statements to create and manage `emp` and `dept` tables in MySQL, including table creation, data insertion, and various queries to retrieve employee and department information. It includes detailed queries for displaying employee details, salaries, and department statistics, as well as specific conditions for filtering data. The SQL syntax is adapted for MySQL, highlighting differences from Oracle syntax.
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)
27 views28 pages

LinkedIn SQL Questions

The document provides SQL statements to create and manage `emp` and `dept` tables in MySQL, including table creation, data insertion, and various queries to retrieve employee and department information. It includes detailed queries for displaying employee details, salaries, and department statistics, as well as specific conditions for filtering data. The SQL syntax is adapted for MySQL, highlighting differences from Oracle syntax.
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/ 28

-- To create the `emp` and `dept` tables and address the corresponding queries in MySQL, I'll first

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.

### Step 1: Create the Tables


use leetcode;
USE leetcode;
drop table dept;
drop table emp;
CREATE TABLE dept (
deptno INT PRIMARY KEY,
dname VARCHAR(50),
loc VARCHAR(50)
);

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,
FOREIGN KEY (deptno) REFERENCES dept(deptno)
);

-- Insert data into dept table


INSERT INTO dept (deptno, dname, loc) VALUES
(10, 'ACCOUNTING', 'NEW YORK'),
(20, 'RESEARCH', 'DALLAS'),
(30, 'SALES', 'CHICAGO'),
(40, 'OPERATIONS', 'BOSTON');

-- Insert data into emp table


INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000.00, NULL, 10),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975.00, NULL, 20),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850.00, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450.00, NULL, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000.00, NULL, 20),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000.00, NULL, 20),
(7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500.00, 0.00, 30),
(7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950.00, NULL, 30),
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250.00, 1400.00, 30),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600.00, 300.00, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250.00, 500.00, 30),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300.00, NULL, 10);

### Step 2: Write Queries in MySQL (Answers)

-- 1. **Display department information from the `dept` table:**


SELECT * FROM dept;

-- 2. **Display details of all employees:**


SELECT * FROM emp;

-- 3. **Display name and job for all employees:**


SELECT ename, job FROM emp;

-- 4. **Display name and salary for all employees:**


SELECT ename, sal FROM emp;

-- 5. **Display employee number and total salary for each employee:**

SELECT empno, sal + IFNULL(comm, 0) AS total_salary FROM emp;


SELECT empno, sal FROM emp;

-- 6. **Display employee name and annual salary for all employees:**

SELECT empno, ename, 12 * sal + IFNULL(comm, 0) AS annual_salary FROM emp;

-- 7. **Display names of employees working in department number 10:**

SELECT ename FROM emp WHERE deptno = 10;

-- 8. **Display names of clerks earning more than 3000:**

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;

-- 10. **Display names of employees who do not earn any commission:**

SELECT empno, ename FROM emp WHERE comm IS NULL OR 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;

-- 12. **Display employees working for the past 5 years:**

SELECT ename FROM emp WHERE DATEDIFF(CURDATE(), hiredate) > 5 * 365;

-- 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';

-- 14. **Display the current date:**


SELECT CURDATE();

-- 15. **Display list of users in your database:**


--
SELECT * FROM mysql.user;

-- 16. **Display names of all tables from the current user:**

SHOW TABLES;

-- 17. **Display the name of the current user:**

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":**

SELECT ename FROM emp WHERE ename LIKE 'S%';

-- 20. **Display names of employees whose names end with the letter "S":**

SELECT ename FROM emp WHERE ename LIKE '%S';

-- 21. **Display names of employees with the second alphabet as "A" in their names:**

SELECT ename FROM emp WHERE ename LIKE '_A%';

-- 22. **Display names of employees whose name is exactly five characters long:**

SELECT ename FROM emp WHERE CHAR_LENGTH(ename) = 5;

-- 23. **Display employees who are not managers:**

SELECT * FROM emp WHERE empno NOT IN (SELECT mgr FROM emp WHERE mgr IS NOT NULL);

-- 24. **Display employees who are not salesmen, clerks, or analysts:**


-- ```sql
SELECT ename FROM emp WHERE job NOT IN ('CLERK', 'SALESMAN', 'ANALYST');

-- 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;

-- 26. **Display total number of employees:**

SELECT COUNT(*) FROM emp;

-- 27. **Display total salary being paid to all employees:**

SELECT SUM(sal) + SUM(IFNULL(comm, 0)) AS total_salary FROM emp;

-- 28. **Display the maximum salary from the `emp` table:**

SELECT MAX(sal) FROM emp;

-- 29. **Display the minimum salary from the `emp` table:**

SELECT MIN(sal) FROM emp;

-- 30. **Display the average salary from the `emp` table:**

SELECT AVG(sal) FROM emp;

-- 31. **Display the maximum salary being paid to clerks:**

SELECT MAX(sal) FROM emp WHERE job = 'CLERK';

-- 32. **Display the maximum salary being paid in department 20:**

SELECT MAX(sal) FROM emp WHERE deptno = 20;

-- 33. **Display the minimum salary being paid to salesmen:**

SELECT MIN(sal) FROM emp WHERE job = 'SALESMAN';

-- 34. **Display the average salary drawn by managers:**


-- ```sql
SELECT AVG(sal) FROM emp WHERE job = 'MANAGER';
-- ```

-- 35. **Display total salary drawn by analysts in department 40:**

SELECT SUM(sal) + SUM(IFNULL(comm, 0)) AS total_salary FROM emp WHERE deptno = 40 AND job
= 'ANALYST';

-- 36. **Display employees in order of salary (ascending):**

SELECT ename FROM emp ORDER BY sal;


-- 37. **Display employees in descending order of salary:**

SELECT ename FROM emp ORDER BY sal DESC;

-- 38. **Display details from `emp` in order of employee name:**

SELECT * FROM emp ORDER BY ename;

-- 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:**

SELECT deptno, COUNT(*) FROM emp GROUP BY deptno;

-- 43. **Display job titles and the total number of employees per job:**

SELECT job, COUNT(*) FROM emp GROUP BY job;

-- 44. **Display department numbers and total salary for each department:**

SELECT deptno, SUM(sal) FROM emp GROUP BY deptno;

-- 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;

-- 46. **Display jobs and total salary for each job:**

SELECT job, SUM(sal) FROM emp GROUP BY job;

-- 47. **Display jobs with the minimum salary in each job


SELECT job, MIN(sal) FROM emp GROUP BY job;

-- 48. **Display departments with more than three employees:**

SELECT deptno, COUNT(*) FROM emp GROUP BY deptno HAVING COUNT(*) > 3;

-- 49. **Display jobs with total salary greater than 40,000:**

SELECT job, SUM(sal) FROM emp GROUP BY job HAVING SUM(sal) > 40000;

-- 50. **Display jobs with more than three employees:**

SELECT job, COUNT(*) FROM emp GROUP BY job HAVING COUNT(*) > 3;

-- 51. **Display the employee who earns the highest salary:**

SELECT ename FROM emp WHERE sal = (SELECT MAX(sal) FROM emp);

-- 52. **Display the clerk who earns the highest salary:**

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;

CREATE TABLE dept (


deptno INT PRIMARY KEY,
dname VARCHAR(50),
loc VARCHAR(50)
);

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,
FOREIGN KEY (deptno) REFERENCES dept(deptno)
);
-- Insert data into dept table
INSERT INTO dept (deptno, dname, loc) VALUES
(10, 'ACCOUNTING', 'NEW YORK'),
(20, 'RESEARCH', 'DALLAS'),
(30, 'SALES', 'CHICAGO'),
(40, 'OPERATIONS', 'BOSTON');

-- Insert data into emp table


INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000.00, NULL, 10),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975.00, NULL, 20),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850.00, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450.00, NULL, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000.00, NULL, 20),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000.00, NULL, 20),
(7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500.00, 0.00, 30),
(7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950.00, NULL, 30),
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250.00, 1400.00, 30),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600.00, 300.00, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250.00, 500.00, 30),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300.00, NULL, 10);

-- 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);

-- 3. Display the employee names who are working in accounting dept.


SELECT ename FROM emp WHERE deptno = (SELECT deptno FROM dept WHERE dname =
'ACCOUNTING');
-- OR
SELECT ename FROM emp WHERE deptno IN (SELECT deptno FROM dept WHERE dname =
'ACCOUNTING');

-- 4. Display the employee names who are working in Chicago.


SELECT ename FROM emp WHERE deptno = (SELECT deptno FROM dept WHERE loc = 'CHICAGO');

-- 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;

-- 9. Display the names of employees in lower case.


SELECT LOWER(ename) FROM emp;

-- 10. Display the names of employees in proper case.


SELECT INITCAP(ename) FROM emp;

-- 11. Find out the length of your name using appropriate function.
SELECT LENGTH('India') FROM dual;

-- 12. Display the length of all employees' names.


SELECT SUM(LENGTH(ename)) FROM emp;

-- 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;

-- 20. Display your age in months.


SELECT FLOOR(MONTHS_BETWEEN(SYSDATE, '15-AUG-1947')) AS "age in months" 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;

-- 23. Find the date of nearest Saturday after current day.


SELECT NEXT_DAY(SYSDATE, 'SATURDAY') FROM dual;

-- 24. Display current time.


SELECT TO_CHAR(SYSDATE, 'HH:MI:SS') AS Time FROM dual;

-- 25. Display the date three months before the current date.
SELECT ADD_MONTHS(SYSDATE, -3) FROM dual;

USE leetcode;

CREATE TABLE dept (


deptno INT PRIMARY KEY,
dname VARCHAR(50),
loc VARCHAR(50)
);

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,
FOREIGN KEY (deptno) REFERENCES dept(deptno)
);

CREATE TABLE salgrade (


grade INT PRIMARY KEY,
losal DECIMAL(10,2),
hisal DECIMAL(10,2)
);

-- Insert data into dept table


INSERT INTO dept (deptno, dname, loc) VALUES
(10, 'ACCOUNTING', 'NEW YORK'),
(20, 'RESEARCH', 'DALLAS'),
(30, 'SALES', 'CHICAGO'),
(40, 'OPERATIONS', 'BOSTON');

-- Insert data into emp table


INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000.00, NULL, 10),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975.00, NULL, 20),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850.00, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450.00, NULL, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000.00, NULL, 20),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000.00, NULL, 20),
(7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500.00, 0.00, 30),
(7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950.00, NULL, 30),
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250.00, 1400.00, 30),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600.00, 300.00, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250.00, 500.00, 30),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300.00, NULL, 10);

-- Insert data into salgrade table


INSERT INTO salgrade (grade, losal, hisal) VALUES
(1, 700.00, 1200.00),
(2, 1201.00, 1400.00),
(3, 1401.00, 2000.00),
(4, 2001.00, 3000.00),
(5, 3001.00, 9999.00);

-- 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);

-- 3. Display the employee names who are working in accounting dept.


SELECT ename FROM emp WHERE deptno = (SELECT deptno FROM dept WHERE dname =
'ACCOUNTING');
-- OR
SELECT ename FROM emp WHERE deptno IN (SELECT deptno FROM dept WHERE dname =
'ACCOUNTING');

-- 4. Display the employee names who are working in Chicago.


SELECT ename FROM emp WHERE deptno = (SELECT deptno FROM dept WHERE loc = 'CHICAGO');

-- 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;

-- 9. Display the names of employees in lower case.


SELECT LOWER(ename) FROM emp;

-- 10. Display the names of employees in proper case.


SELECT INITCAP(ename) FROM emp;

-- 11. Find out the length of your name using appropriate function.
SELECT LENGTH('India') FROM dual;

-- 12. Display the length of all employees' names.


SELECT SUM(LENGTH(ename)) FROM emp;

-- 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;

-- 20. Display your age in months.


SELECT FLOOR(MONTHS_BETWEEN(SYSDATE, '15-AUG-1947')) AS "age in months" 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;

-- 23. Find the date of nearest Saturday after current day.


SELECT NEXT_DAY(SYSDATE, 'SATURDAY') FROM dual;

-- 24. Display current time.


SELECT TO_CHAR(SYSDATE, 'HH:MI:SS') AS Time FROM dual;
-- 25. Display the date three months before the current date.
SELECT ADD_MONTHS(SYSDATE, -3) FROM dual;

-- 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;

-- 28. Display the jobs which are unique to dept no 10.


SELECT job FROM emp WHERE deptno = 10 MINUS SELECT job FROM emp WHERE deptno != 10;
-- OR
SELECT job FROM emp WHERE deptno = 10 AND job NOT IN

-- 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;

-- 28. Display the jobs which are unique to dept no 10.


SELECT job FROM emp WHERE deptno = 10 MINUS SELECT job FROM emp WHERE deptno != 10;
-- OR
SELECT job FROM emp WHERE deptno = 10 AND job NOT IN (SELECT job FROM emp WHERE deptno
<> 10);

-- 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';

-- 34. Display those employees whose manager name is JONES.


SELECT * FROM emp WHERE mgr = (SELECT empno FROM emp WHERE ename = 'JONES');

-- 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;

-- 36. Display all employees with their dept name.


SELECT ename, dname FROM emp e JOIN dept d ON e.deptno = d.deptno;

-- 37. Display ename who are working in sales dept.


SELECT empno, ename FROM emp WHERE deptno = (SELECT deptno FROM dept WHERE dname =
'SALES');

-- 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;

CREATE TABLE dept (


deptno INT PRIMARY KEY,
dname VARCHAR(50),
loc VARCHAR(50)
);

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,
FOREIGN KEY (deptno) REFERENCES dept(deptno)
);

CREATE TABLE salgrade (


grade INT PRIMARY KEY,
losal DECIMAL(10,2),
hisal DECIMAL(10,2)
);

-- Insert data into dept table


INSERT INTO dept (deptno, dname, loc) VALUES
(10, 'ACCOUNTING', 'NEW YORK'),
(20, 'RESEARCH', 'DALLAS'),
(30, 'SALES', 'CHICAGO'),
(40, 'OPERATIONS', 'BOSTON');

-- Insert data into emp table


INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000.00, NULL, 10),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975.00, NULL, 20),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850.00, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450.00, NULL, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000.00, NULL, 20),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000.00, NULL, 20),
(7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500.00, 0.00, 30),
(7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950.00, NULL, 30),
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250.00, 1400.00, 30),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600.00, 300.00, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250.00, 500.00, 30),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300.00, NULL, 10);

-- Insert data into salgrade table


INSERT INTO salgrade (grade, losal, hisal) VALUES
(1, 700.00, 1200.00),
(2, 1201.00, 1400.00),
(3, 1401.00, 2000.00),
(4, 2001.00, 3000.00),
(5, 3001.00, 9999.00);

-- 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;

-- 4. Find out the top 5 earners of the company.


SELECT *
FROM emp e
WHERE 5 > (SELECT COUNT(*) FROM emp WHERE sal > e.sal)
ORDER BY sal DESC;

-- 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;

-- 14. Find out the last 5 (least) earners of the company.


SELECT *
FROM emp e
WHERE 5 > (SELECT COUNT(*) FROM emp WHERE sal < e.sal)
ORDER BY sal;

-- 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;

-- 22. Display those employees whose salary is an ODD value.


SELECT *
FROM emp
WHERE MOD(sal, 2) = 1;

-- 23. Display those employees whose salary contains at least 4 digits.


SELECT *
FROM emp
WHERE LENGTH(sal) >= 4;

-- 24. Display those employees who joined the company in the month of DEC.
SELECT *
FROM emp
WHERE UPPER(TO_CHAR(hiredate, 'mon')) = 'DEC';

-- 25. Display those employees whose name contains 'A'.


SELECT *
FROM emp
WHERE INSTR(ename, 'A', 1, 1) > 0;

-- 26. Display those employees whose deptno is available in salary.


SELECT *
FROM emp
WHERE INSTR(sal, deptno, 1, 1) > 0;

-- 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;

-- 29. Display those employees who are working in sales or research.


SELECT *
FROM emp
WHERE deptno IN (SELECT deptno FROM dept WHERE dname IN ('SALES', 'RESEARCH'));

-- 30. Display the grade of Jones.


SELECT grade
FROM salgrade
WHERE losal <= (SELECT sal FROM emp WHERE ename = 'JONES')
AND hisal >= (SELECT sal FROM emp WHERE ename = 'JONES');

-- 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);

-- 35. Display those employees who are working as managers.


SELECT *
FROM emp
WHERE empno IN (SELECT mgr FROM emp);

-- 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
);

-- Insert sample values into the employee table


INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES
(7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, NULL, 20),
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10),
(7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30),
(7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);

-- 1. Display those who are working as managers using a correlated subquery.


SELECT * FROM emp WHERE empno IN (SELECT mgr FROM emp);

-- 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;

-- 7. Check whether all employee numbers are indeed unique.


SELECT COUNT(empno), COUNT(DISTINCT empno) FROM emp HAVING COUNT(empno) =
COUNT(DISTINCT empno);

-- 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;

-- 17. Display the departments where there are no employees.


SELECT deptno, dname FROM dept WHERE deptno NOT IN (SELECT DISTINCT deptno FROM emp);

-- 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;

-- 20. Display the average salary figure for each department.


SELECT deptno, AVG(sal) AS avg_sal FROM emp GROUP BY deptno;
-- 21. Display the row of the most recently hired employee, showing ename, hiredate, and max date.
SELECT ename, hiredate, MAX(hiredate) OVER () AS max_date
FROM emp
WHERE hiredate = (SELECT MAX(hiredate) FROM emp);

-- 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);

-- 24. Select dept name, dept no, and sum of salary.


SELECT e.deptno, d.dname, SUM(e.sal) AS total_sal
FROM emp e
JOIN dept d ON e.deptno = d.deptno
GROUP BY e.deptno, d.dname
ORDER BY e.deptno;

-- 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;

-- 26. Find all departments which have more than 3 employees.


SELECT deptno
FROM emp
GROUP BY deptno
HAVING COUNT(*) > 3;

-- 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;

-- 33. Delete the 10th record of the EMP table.


DELETE FROM emp
WHERE empno = (SELECT empno
FROM (SELECT empno, ROW_NUMBER() OVER (ORDER BY empno) AS row_num
FROM emp)
WHERE row_num = 10);

-- 34. Create a copy of the EMP table.


CREATE TABLE emp1 AS SELECT * FROM emp;

-- 35. Select ename if ename exists more than once.


SELECT ename
FROM emp
GROUP BY ename
HAVING COUNT(*) > 1;

-- 36. Display all enames in reverse order.


SELECT ename
FROM emp
ORDER BY ename DESC;

-- 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;

-- 39. Display employees' names as follows: A ALLEN, B BLAKE.


SELECT SUBSTR(ename, 1, 1) || ' ' || ename AS formatted_ename
FROM emp;

-- 40. List out the employees' ename, sal, and PF from emp.
SELECT ename, sal, sal * 0.15 AS PF
FROM emp;

-- 41. Display RSPS from emp without using updating or inserting.


SELECT 'RSPS'
FROM emp;

-- 42. Create a table emp with only one column empno.


CREATE TABLE emp (empno int(5));

-- 43. Add a column ename VARCHAR(20) to the emp table.


ALTER TABLE emp ADD ename VARCHAR(20) NOT NULL;

-- 44. Add a primary key constraint to the emp table.


ALTER TABLE emp ADD CONSTRAINT emp_empno_pk PRIMARY KEY (empno);

-- 45. Increase the length of the ename column to 30 characters.


ALTER TABLE emp MODIFY ename VARCHAR(30);

-- 46. Add salary column to emp table.


ALTER TABLE emp ADD sal NUMBER(7, 2);

-- 47. Add a validation saying that salary cannot be greater than 10,000.
ALTER TABLE emp ADD CONSTRAINT emp_sal_check CHECK (sal < 10000);

-- 48. Disable the salary constraint.


ALTER TABLE emp DISABLE CONSTRAINT emp_sal_check;

-- 49. Enable the salary constraint.


ALTER TABLE emp ENABLE CONSTRAINT emp_sal_check;

-- 50. Add a column called mgr to your emp table.


ALTER TABLE emp ADD mgr NUMBER(5);

-- 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;

-- 62. Display employee name and their manager's name.


SELECT e.ename AS employee_name, m.ename AS manager_name
FROM emp e
JOIN emp m ON e.mgr = m.empno;

-- 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);

-- 66. Delete employees where job name is 'CLERK'.


DELETE FROM emp WHERE job = 'CLERK';

-- 67. Insert into emp without giving any further commands.


-- This step involves using another client system to verify changes, which cannot be demonstrated in a
single SQL query.

-- 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.

-- 69. Commit the changes and verify in the second system.


-- This step involves using another client system to verify changes, which cannot be demonstrated in a
single SQL query.

-- 70. Display the current date and time.


SELECT TO_CHAR(SYSDATE, 'Month Mon DD YY YYYY HH:MI:SS') AS current_date_time
FROM dual;

-- 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;

-- 73. Which department has the highest annual remuneration bill?


SELECT deptno, LPAD(SUM(12 * (sal + NVL(comm, 0))), 15) AS "COMPENSATION"
FROM emp
GROUP BY deptno
HAVING SUM(12 * (sal + NVL(comm, 0))) =
(SELECT MAX(SUM(12 * (sal + NVL(comm, 0)))) FROM emp GROUP BY deptno);

-- 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);

-- 76. Find the nth maximum salary.


SELECT ename, sal
FROM emp a
WHERE &n = (SELECT COUNT(DISTINCT sal) FROM emp b WHERE a.sal <= b.sal);

-- 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;

-- 79. What is the output of the below query?


SELECT rownum AS rank, ename, sal
FROM (SELECT ename, sal FROM emp ORDER BY sal DESC)
WHERE rownum <= 4
MINUS
SELECT rownum, emp.ename, emp.sal
FROM emp
WHERE 1 = (SELECT COUNT(DISTINCT b.sal) FROM emp b WHERE b.sal >= emp.sal);

-- 80. To get nth salary.


SELECT a.*
FROM emp a
WHERE &n >= (SELECT COUNT(DISTINCT b.sal) FROM emp b WHERE a.sal <= b.sal)
MINUS
SELECT a.*
FROM emp a
WHERE 1 = (SELECT COUNT(DISTINCT b.sal) FROM emp b WHERE a.sal <= b.sal);

-- 81. To select salary and its occurrences according to its order in ascending.
SELECT sal, COUNT(*)
FROM emp
GROUP BY sal
ORDER BY sal;

-- 82. Selecting nth maximum salary from emp.


SELECT ename, sal
FROM emp a
WHERE &n = (SELECT COUNT(DISTINCT sal) FROM emp b WHERE b.sal >= a.sal);

-- 83. Selecting nth minimum salary from emp.


SELECT ename, sal
FROM emp a
WHERE &n = (SELECT COUNT(DISTINCT sal) FROM emp b WHERE b.sal <= a.sal);

-- 84. Selecting salary and rownum from emp.


SELECT sal, rownum
FROM emp
ORDER BY sal;

-- 85. Selecting alternative rows from a table.


SELECT empno, ename, sal
FROM (SELECT empno, ename, sal, MOD(rownum, 2) AS a FROM emp) b
WHERE b.a = 0;

-- 86. Insert values from one table to another table.


INSERT INTO empdup
SELECT * FROM emp;

-- 87. Deleting duplicate rows from a table.


DELETE FROM empdup a
WHERE rowid <> (SELECT MAX(rowid) FROM empdup b WHERE a.empno = b.empno);

-- 88. To print a number in words.


SELECT TO_CHAR(TO_DATE(1234, 'JSP'), 'JSP')
FROM dual;

-- 89. To find the number of columns in a table.


SELECT COUNT(*)
FROM user_tab_columns
WHERE table_name = 'EMP';

-- 90. To get the salaries greater than 1000 in each department.


SELECT sal, COUNT(sal), dname
FROM emp a, dept b
WHERE a.deptno = b.deptno
GROUP BY sal, dname
HAVING sal > 1000
ORDER BY dname;

-- 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;

You might also like