SQL Part 1 Q&A
SQL Part 1 Q&A
7. Display the names of all employees who are working in department number 10.
SELECT ENAME
FROM EMP
WHERE DEPTNO = 10;
8. Display the names of all employees working as clerks and drawing a salary of
more than 3000.
SELECT ENAME
FROM EMP
WHERE JOB = ‘CLERK’ AND SAL > 3000;
Anjana Kuiri
9. Display employee number 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 AND COMM = 0;
11. Display the names of employees who are working as clerk, salesman or analyst and
drawing a salary more than 3000.
SELECT ENAME
FROM EMP
WHERE (JOB = ‘CLERK’ OR JOB = ‘SALESMAN’ OR JOB = ‘ANALYST’)
AND SAL > 3000;
or,
SELECT ENAME
FROM EMP
WHERE JOB IN (‘CLERK’, ‘SALESMAN’, ‘ANALYST’) AND SAL>3000;
12. Display the names of employees who are working in the company for the past
5 years.
SELECT ENAME
FROM EMP
WHERE SYSDATE-HIREDATE > 5*365;
13. Display the list of employees who have joined the company before 30 th June 90 or
after 31st dec 90.
SELECT *
FROM EMP
WHERE HIREDATE BETWEEN ‘30-JUN-1990’ AND ‘31-DEC-1990’;
14. Display current date.
SELECT SYSDATE
FROM DUAL;
15. Display the list of users in your database (using log table).
SELECT *
FROM DBA_USERS;
Anjana Kuiri
16. Display the names of all tables from the current user.
SELECT *
FROM TAB;
19. Display the names of employees whose name starts with alphabet S.
SELECT ENAME
FROM EMP
WHERE ENAME LIKE ‘S%’;
20. Display employee names for employees whose name ends with alphabet S.
SELECT ENAME
FROM EMP
WHERE ENAME LIKE ‘%S’;
21. Display the names of employees whose names have second alphabet A in their names.
SELECT ENAME
FROM EMP
WHERE ENAME LIKE ‘_A%’;
22. Display the names of employees whose names are exactly five characters in length.
SELECT ENAME
FROM EMP
WHERE LENGTH(ENAME) = 5;
or,
SELECT ENAME
FROM EMP
WHERE ENAME LIKE ‘_____’;
Anjana Kuiri
23. Display the names of employees who are not working as SALESMAN or CLERK or
ANALYST.
SELECT ENAME
FROM EMP
WHERE JOB NOT IN (‘CLERK’, ‘SMAN’, ‘ANALYST’);
Anjana Kuiri
31. Display the total salary drawn by an analyst working in dept no 40.
SELECT SUM(SAL)+SUM(NVL(COMM,0))
FROM EMP
WHERE DEPTNO = 40 AND JOB = ‘ANALYST’;
32. Display the names of employees in order of salary i.e. the name of the employee
earning the lowest salary should appear first.
SELECT ENAME
FROM EMP
ORDER BY SAL;
34. Display empno, ename, deptno, and sal. Sort the output first based on name and
within name by deptno and within deptno by Sal;
SELECT EMPNO, ENAME, DEPTNO, SAL
FROM EMP
ORDER BY ENAME, DEPTNO, SAL;
35. Display dept numbers and total number of employees within each group.
SELECT DEPTNO, COUNT(*)
FROM EMP
GROUP BY DEPTNO;
36. Display department numbers and maximum salary for each department.
SELECT DEPTNO, MAX(SAL), MIN(SAL)
FROM EMP
GROUP BY DEPTNO;
37. Display the various jobs and total salary for each job.
SELECT JOB, SUM(SAL)
FROM EMP
GROUP BY JOB;
Anjana Kuiri
38. Display the department numbers with more than three employees in each dept.
SELECT DEPTNO, COUNT(*)
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*) > 3;
39. Display the various jobs along with total salary for each of the jobs where total
salary is greater than 40000.
SELECT JOB, SUM(SAL)
FROM EMP
GROUP BY JOB
HAVING SUM(SAL) > 40000;
41. Display the employee number and name of employee working as CLERK and
earning the highest salary among CLERKS.
SELECT EMPNO, ENAME
FROM EMP
WHERE JOB = ‘CLERK’
AND SAL = (SELECT MAX(SAL)
FROM EMP
WHERE JOB = ‘CLERK’);
42. Display the names of the salesman who earns a salary more than the highest salary of
any clerk.
SELECT ENAME
FROM EMP
WHERE JOB = ‘SALESMAN’
AND SAL > (SELECT MAX(SAL)
FROM EMP
WHERE JOB = ‘CLERK’);
Anjana Kuiri
43. Display the names of clerks who earn salary more than that of James or that of salary
lesser than that of Scott.
SELECT ENAME
FROM EMP
WHERE JOB = ‘CLERK’
AND SAL < (SELECT SAL FROM EMP WHERE ENAME = ‘SCOTT’)
AND SAL > (SELECT SAL FROM EMP WHERE ENAME = ‘JAMES’);
44. Display the names of employees who earn a salary more than that of James or that of
salary greater than that of Scott.
SELECT ENAME
FROM EMP
WHERE SAL > (SELECT SAL FROM EMP WHERE ENAME = ‘JAMES’)
OR SAL > (SELECT SAL FROM EMP WHERE ENAME = ‘SCOTT’);
45. Display the name of the employees along with their annual salary. The name of the
employees earning the highest annual salary should appear first.
SELECT ENAME, (SAL+NVL(COMM,0))*12 ANNUAL_SAL
FROM EMP
ORDER BY (SAL+NVL(COMM,0))*12 DESC;
Anjana Kuiri