Sure!
Here are some tricky and complex SQL query questions based on the topics
you've provided. I'll cover a mix of all the mentioned topics, combining filters,
subqueries, special operators, and functions.
---
### 1. Query with Pattern Matching and Special Operators
Write a query to display the employee name, job title, and salary for employees:
- Whose job title contains the letter 'A'.
- Have a salary greater than 1000 but less than 3000.
- Are either working in departments 10 or 20, or have a commission greater than
500.
- Sort the results by salary in descending order and name in ascending order.
SELECT JOB, SAL
FROM EMP
WHERE JOB LIKE '%A%'
AND SAL BETWEEN 1000 AND 3000
AND ( DEPTNO IN (10,20) OR COMM > 500)
ORDER BY SAL DESC, ENAME;
---
### 2. Complex Subquery with Aggregate Functions
Write a query to display the employee name, job title, and department number:
- Where employees have a salary greater than the average salary of employees in
their respective departments.
- Only consider departments where the total number of employees is more than 2.
- Sort the results by department number in ascending order.
SELECT ENAME, JOB, DEPTNO
FROM EMP
WHERE SAL > (SELECT AVG(SAL) FROM EMP)
AND DEPTNO IN (SELECT DEPTNO FROM EMP GROUP BY DEPTNO HAVING COUNT(*) > 2)
ORDER BY DEPTNO;
MIND FUCK QUESTION
---
### 3. Combining Date Functions and Filtering
Write a query to display the employee number, name, and hire date of employees:
- Who were hired after January 1st, 1982.
- Whose salary is greater than or equal to 2000.
- Are not in departments 10 or 30.
- Sort by hire date in ascending order.
SELECT EMPNO, ENAME, HIREDATE
FROM EMP
WHERE HIREDATE > '01-JAN-1982'
AND SAL >= 2000
AND DEPTNO NOT IN (10, 30)
ORDER BY HIREDATE;
---
### 4. Use of DISTINCT, Aliases, and Operators
Write a query to display the distinct names of employees, their jobs (alias "Job
Title"), and their hire date:
- Who have a commission that is either NULL or less than 300.
- Have a salary between 1200 and 2500.
- Who are working in department 20 or 30.
- Sort the results by job title in ascending order and hire date in descending
order.
SELECT ENAME AS EMPLOYEE_NAME, JOB AS JOB_TITLE, HIREDATE
FROM EMP
WHERE (COMM IS NULL OR COMM < 300)
AND SAL BETWEEN 1200 AND 2500
AND DEPTNO IN (20, 30)
ORDER BY JOB, HIREDATE DESC;
---
### 5. Subquery with Special Operators and Functions
Write a query to display the names, jobs, and department numbers of employees:
- Who were hired before '01-JAN-1982' and are not working as a manager.
- Whose salaries are higher than the average salary of employees hired after 1982.
- Order the results by department number and job title.
select ename, job, deptno
from emp
where hiredate < '01-jan-1982'
and job not in 'MANAGER'
and sal > (select avg(sal) from emp where hiredate > '31-dec-1982')
order by deptno, job;
---
### 6. Nested Subqueries with Multiple Conditions
Write a query to display the names, hire dates, and job titles of employees:
- Who were hired before the most recent hire date of employees working as
‘MANAGER’.
- Whose salary is between 1000 and 3000, but not exactly 1250.
- Whose managers have a salary higher than the average salary across all
departments.
- Sort by hire date in ascending order and name alphabetically.
SELECT ENAME, HIREDATE, JOB
FROM EMP
WHERE HIREDATE < (SELECT MAX(HIREDATE)
FROM EMP
WHERE JOB = 'MANAGER')
AND SAL BETWEEN 1000 AND 3000
AND SAL != 1250
AND (SELECT MAX(SAL)
FROM EMP
WHERE JOB = 'MANAGER') > (SELECT AVG(SAL)
FROM EMP E2
WHERE E2.DEPTNO = EMP.DEPTNO)
ORDER BY HIREDATE ASC, ENAME;
wtf?
---
### 7. Multi-row Functions with GROUP BY and HAVING
Write a query to display the job title, department number, and average salary of
employees:
- Where the average salary is greater than 2000.
- Only include departments where the total number of employees is greater than 3.
- Sort by department number in descending order and average salary in ascending
order.
SELECT JOB, DEPTNO, AVG(SAL) AS AVG_SAL
FROM EMP
GROUP BY JOB, DEPTNO
HAVING AVG(SAL) > 2000
AND COUNT(*) > 3
ORDER BY DEPTNO DESC, AVG_SAL ASC;
i had enough!
---
### 8. Complex Filtering with String Functions
Write a query to display the employee number, name, and department number of
employees:
- Whose names contain at least one 'M' and do not contain any 'A'.
- Who have a salary greater than the average salary of their respective department.
- Sort the results by employee number in descending order.
---
### 9. Advanced Query with Joins and Aggregate Functions
Write a query to display the employee name, job title, and total compensation
(salary + commission, alias "Total Comp") of employees:
- Where the total compensation is higher than the average total compensation of
employees in department 30.
- Whose job title is either 'CLERK' or 'ANALYST'.
- Sort the results by total compensation in descending order and job title in
ascending order.
---
### 10. Query with Multiple Operators and Date Functions
Write a query to display the employee number, name, and hire date of employees:
- Whose salary is between 1000 and 2500.
- Were hired after 01-JAN-1981 and before 31-DEC-1982.
- Have a job title that starts with 'S' or 'C'.
- Order by hire date in ascending order and salary in descending order.
---
### 11. Nested Subquery with Comparison Operators
Write a query to display the names, job titles, and hire dates of employees:
- Who earn a salary higher than the average salary of employees working as
‘SALESMAN’.
- Are working in departments 20 or 10.
- Were hired in either 1981 or 1987.
- Sort by hire date in ascending order.
---
### 12. Advanced Query with Subqueries and GROUP BY
Write a query to display the department number, job title, and the average salary:
- Only for jobs where the total number of employees is greater than 2.
- Where the average salary of that job is greater than the overall company-wide
average salary.
- Only consider departments that have employees hired before '01-JAN-1981'.
- Sort by department number in ascending order.
---
### 13. Query with Multi-row Functions and HAVING Clause
Write a query to display the department number, average salary, and maximum salary:
- Where the average salary of the department is greater than 2000.
- Only include departments where the total number of employees is greater than 3.
- Sort by department number and average salary in descending order.
---
### 14. Subquery with Grouping and String Functions
Write a query to display the employee number, job title, and department number of
employees:
- Whose job title contains exactly 2 'E's.
- Are working in a department where the average salary is higher than 1500.
- Were hired after '01-JAN-1982'.
- Sort the results by department number and job title.
---
### 15. Advanced String Matching with Special Operators
Write a query to display the employee number, name, job title, and hire date:
- Whose names start with 'J', 'S', or 'M'.
- Have a job title that is either 'CLERK' or 'MANAGER'.
- Were hired in the year 1982.
- Sort by hire date in ascending order and employee number in descending order.
---
This should give you a comprehensive set of challenging SQL queries to practice,
combining a wide range of topics. Let me know if you need more!