edited by
42,524 views
109 109 votes

Consider the table employee(empId, name, department, salary) and the two queries $Q_1, \, Q_2$ below. Assuming that department $5$ has more than one employee, and we want to find the employees who get higher salary than anyone in the department $5,$ which one of the statements is TRUE for any arbitrary employee table?

$Q_1:$
Select e.empId
From employee e
Where not exists
    (Select * From employee s Where s.department = "5" and s.salary >= e.salary)
$Q_2:$
Select e.empId
From employee e
Where e.salary > Any
    (Select distinct salary From employee s Where s.department = "5")
  1. $Q_1$ is the correct query
  2. $Q_2$ is the correct query
  3. Both $Q_1$ and $Q_2$ produce the same answer
  4. Neither $Q_1$ nor $Q_2$ is the correct query

7 Answers

Best answer
70 70 votes

Answer: A
Create a table like this:

create table employee(empId int(50), name varchar(50), department int(50), salary int(50));
insert into employee values (1, 'a', 4, 90);
insert into employee values (2, 'b', 5, 30);
insert into employee values (3, 'c', 5, 50);
insert into employee values (4, 'd', 5, 80);
insert into employee values (8, 'f', 7, 10);

$Q_{1}$ returns $1$ for the above table. See here: http://sqlfiddle.com/#!9/9acce/1

$Q_{2}$ returns empId of those employees who get salary more than the minimum salary offered in department $5$. It returns $1,3,4$ for the above table. See here: http://sqlfiddle.com/#!9/9acce/2

According the question the answer should be $1$ for the above table.

PS: The question implies that the required employee must not be from department $5$. 

edited by
29 29 votes
Q1: Returns all employees such that there is no employee in dept 5 having same or higher salary. Hence the answer.
Q2: Returns all employees having salary higher than the minimum salary in department 5.

So, answer should be A.
18 18 votes

To solve this type of question, we will take a sample table which includes all possible cases. In this case, we have to find out the employees whose salary is greater than salary of any employee in department 5. So we have taken two employees in department 5 with salary 5000 and 10000 and three other employees:

empId 1 whose salary is less than both employees of department 5.
empId 2 whose salary is greater than both employees of department 5.
empId 4 whose salary is less than one employee of department 5(empId 5) and greater than other employee of department 5(empId 3).

empId name department salary
1 A 1 2000
2 B 2 12000
3 C 5 5000
4 D 3 7000
5 E 5 10000

So we will see whether two queries given in question work for these scenarios or not.

Q1 Select e.empId from employee e where not exists    

                       (Select * from employee s where s.department = “5” and s.salary >=e.salary) 

This is a correlated nested query. For every row in outer query, inner query is executed and result of inner query is used as an input of outer query. First row of employee table from outer query having e.empId=1 is passed to inner query, it will execute the inner query like:

select * from employee s where s.department=”5” and  s.salary>=2000(1.salary)

It will return rows with empId 3 and 5. But the outer query will not return anything for this row because not exists condition is failed.

For second row of employee table, inner query will return no row, and outer query will retun empId 2.

Similarly, we can do the same for other rows and the final output will be:

empId
2
 

Q2 Select e.empId from employee e   where e.salary > any     

(Select distinct salary From employee s Where s.department = “5”)

This type of query is independent nested query in which inner query is executed independently and its result is used in execution of outer query. Inner query will fetch distinct salaries of employees in department “5” as:

Select distinct salary From employee s Where s.department = “5”

its output will be 5000 and 10000.

The inner query will fetch those empId whose salary is greater than any salary from this set like:

Select e.empId from employee e   where e.salary > any(5000,10000)     

The final output will be:

empId
2
4
5

So the required output is drawn from Q2 but not Q1. Q1 is missing those rows where employee salary is greater than one employee of department 5 but less than other employee of department 5(empId 4). So answer will be (B).

7 7 votes

Answer Should be D.

Q1: It will also give all those employee name whose salary is greater or equal to all other employee of department 5. But i question only asked GREATER.

Q2. It will give all those employee name of Department 5  who have salary greater than anyone of the employee in department 5 but not ALL

2 2 votes
EVERY ANSWER HERE IS WRONG EXCEPT THE ONE THAT HAS BEEN FLAGGED AS "WRONG ANSWER".

Query 1 would have been correct if > was used instead of >= Q1 would not return any result if the highest salary  from employee table is drawn by someone from Dept 5

Some people have attached images from sql  but they haven't included this case

So, Q1 is correct IF AND ONLY IF no one from dept 5 is drawing the highest salary

I ran the query multiple times on sql.

In no version of sql Q1 can EVER be correct if someone in dept 5 is getting the highest salary out of all employees. Try it for yourself.

And Q2 is wrong for obvious reasons
Answer:
Position:
Show:

Related questions

137 137 votes
15 answers 15 answers
36.0k
36.0k views
Aravind asked Oct 4, 2014
36,009 views
Information about a collection of students is given by the relation $\text{studInfo(}\underline{\text{studId}},\text{ name, sex)}$. The relation $\text{enroll(}{\text{stu...
41 41 votes
7 answers 7 answers
14.2k
14.2k views
Kathleen asked Sep 21, 2014
14,221 views
Consider the following schedules involving two transactions. Which one of the following statements is TRUE?$S_1 :r_1(X); r_1(Y); r_2(X); r_2(Y); w_2(Y); w_1(X)$$S_2 :r_1(...
69 69 votes
9 answers 9 answers
37.8k
37.8k views
Kathleen asked Sep 21, 2014
37,832 views
The order of a leaf node in a $B^+$ - tree is the maximum number of (value, data record pointer) pairs it can hold. Given that the block size is $1K\;\text{bytes}$, data ...
122 122 votes
8 answers 8 answers
43.9k
43.9k views
Kathleen asked Sep 21, 2014
43,911 views
Which one of the following statements is $\text{FALSE}$?Any relation with two attributes is in $\text{BCNF}$A relation in which every key has only one attribute is in $\t...