0% found this document useful (0 votes)
5 views

SQL_Questions_and_Answers

Practice question

Uploaded by

Yogesh
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)
5 views

SQL_Questions_and_Answers

Practice question

Uploaded by

Yogesh
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/ 4

SQL Questions and Answers

### Questions and Answers ###

1. Retrieve all columns from the `emp` table.

**Solution:**

SELECT * FROM emp;

2. Retrieve all columns from the `dept` table.

**Solution:**

SELECT * FROM dept;

3. Fetch details of the employee with `empno` equal to 7900.

**Solution:**

SELECT * FROM emp WHERE empno = 7900;

4. List all employees from the `emp` table ordered by their salary in ascending order.

**Solution:**

SELECT * FROM emp ORDER BY sal;

5. List all employees from the `emp` table ordered by their salary in descending order.

**Solution:**

SELECT * FROM emp ORDER BY sal DESC;

6. List all employees from department number 20, ordered by their salary in descending order.

**Solution:**
SELECT * FROM emp WHERE deptno = 20 ORDER BY sal DESC;

7. Retrieve all employees ordered by their department number and salary in descending order.

**Solution:**

SELECT * FROM emp ORDER BY deptno, sal DESC;

8. Order employees based on the 6th and 8th column in the table.

**Solution:**

SELECT * FROM emp ORDER BY 6, 8;

9. Find the number of employees in each department.

**Solution:**

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

10. Retrieve the job roles and department numbers with their employee counts.

**Solution:**

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

...

46. Retrieve the department with the maximum number of employees.

**Solution:**

SELECT deptno

FROM emp e1

WHERE (SELECT COUNT(*)

FROM emp e2

WHERE e1.deptno = e2.deptno) = (SELECT MAX(employee_count)


FROM (SELECT deptno, COUNT(*) AS employee_count

FROM emp

GROUP BY deptno) dept_counts);

47. List employees whose salaries are greater than the salary of at least one employee in

department 30.

**Solution:**

SELECT *

FROM emp e

WHERE sal > ANY (SELECT sal

FROM emp

WHERE deptno = 30);

48. Find employees whose salaries are greater than all other employees in the same department.

**Solution:**

SELECT *

FROM emp e1

WHERE sal > ALL (SELECT sal

FROM emp e2

WHERE e1.deptno = e2.deptno

AND e1.empno <> e2.empno);

49. Find the department with the highest average salary.

**Solution:**

SELECT deptno

FROM emp e1

GROUP BY deptno
HAVING AVG(sal) = (SELECT MAX(avg_sal)

FROM (SELECT AVG(sal) AS avg_sal

FROM emp

GROUP BY deptno) avg_salaries);

50. List employees who earn more than the average salary across all employees with the same job

and are in department 10.

**Solution:**

SELECT *

FROM emp e

WHERE deptno = 10

AND sal > (SELECT AVG(sal)

FROM emp

WHERE job = e.job);

You might also like