Question 51
MediumWhich query finds the second-highest distinct salary in table emp(salary)?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
DBMS
Which query finds the second-highest distinct salary in table emp(salary)?
SELECT SECOND(salary) FROM emp;
SELECT MAX(salary) FROM emp WHERE salary < (SELECT MAX(salary) FROM emp);
SELECT MAX(salary) - 1 FROM emp;
SELECT salary FROM emp ORDER BY salary LIMIT 2;
Sign up free to see the answer and a step-by-step explanation.
Why does SELECT dept, name, COUNT(*) FROM emp GROUP BY dept; fail in standard SQL?
dept must be aggregated
name is neither grouped nor aggregated, so it has no single value per group
GROUP BY must come before FROM
COUNT(*) cannot be used with GROUP BY
Sign up free to see the answer and a step-by-step explanation.
Why does WHERE COUNT() > 2 cause an error while HAVING COUNT() > 2 works?
WHERE filters rows before grouping, when aggregates are not yet computed
WHERE cannot contain numbers
COUNT(*) is not allowed in SELECT
HAVING runs before FROM
Sign up free to see the answer and a step-by-step explanation.
Table A has 3 rows and table B has 4 rows. A LEFT JOIN B on a condition that no row satisfies. How many rows are returned?
12
0
4
3
Sign up free to see the answer and a step-by-step explanation.
Table employee:
| id | name | dept | salary |
|---|---|---|---|
| 1 | Asha | HR | 40000 |
| 2 | Ravi | IT | 60000 |
| 3 | Meena | IT | 75000 |
| 4 | John | Sales | 50000 |
| 5 | Priya | IT | 60000 |
| 6 | Karan | Sales | NULL |
What does this query return?
SELECT name FROM employee
WHERE salary > (SELECT AVG(salary) FROM employee WHERE dept = 'IT');
Meena, Ravi and Priya
Meena
Sign up free to see the answer and a step-by-step explanation.
Table employee:
| id | name | dept | salary |
|---|---|---|---|
| 1 | Asha | HR | 40000 |
| 2 | Ravi | IT | 60000 |
| 3 | Meena | IT | 75000 |
| 4 | John | Sales | 50000 |
| 5 | Priya | IT | 60000 |
| 6 | Karan | Sales | NULL |
How many rows does this query return?
SELECT name FROM employee WHERE salary <> 60000;
Enter a number as the answer.
Sign up free to see the answer and a step-by-step explanation.
Table employee:
| id | name | dept | salary |
|---|---|---|---|
| 1 | Asha | HR | 40000 |
| 2 | Ravi | IT | 60000 |
| 3 | Meena | IT | 75000 |
| 4 | John | Sales | 50000 |
| 5 | Priya | IT | 60000 |
| 6 | Karan | Sales | NULL |
What is the result of this query?
SELECT name FROM (
SELECT name, DENSE_RANK() OVER (ORDER BY salary DESC) AS r
FROM employee WHERE salary IS NOT NULL
) t
WHERE r = 2;
One row: Meena
One row: Ravi
Sign up free to see the answer and a step-by-step explanation.
Table A has 4 rows and table B has 5 rows. How many rows does SELECT * FROM A CROSS JOIN B return?
Enter a number as the answer.
Sign up free to see the answer and a step-by-step explanation.
What is the difference between UNION and UNION ALL?
UNION removes duplicate rows; UNION ALL keeps them and is usually faster
UNION works only on one table
There is no difference
UNION ALL removes duplicates; UNION keeps them
Sign up free to see the answer and a step-by-step explanation.
What is a correlated subquery?
A subquery in the FROM clause
A subquery that runs only once
Two queries joined with UNION
A subquery that refers to columns of the outer query and is evaluated once per outer row
Sign up free to see the answer and a step-by-step explanation.
Asha
Ravi and Priya
Two rows: Ravi and Priya
Three rows: Ravi, Meena and Priya