Question 71
HardWhat does this query return for the table below?
Table emp:
| id | name | dept | salary | mgr |
|---|---|---|---|---|
| 1 | Anil | IT | 90000 | NULL |
| 2 | Bina | IT | 70000 | 1 |
| 3 | Chetan | HR | 50000 | 1 |
| 4 | Divya | IT | 70000 | 2 |
| 5 | Esha | HR | 65000 | 3 |
| 6 | Farhan | Sales | 55000 | 1 |
| 7 | Gita | Sales | 60000 | 6 |
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
DBMS
What does this query return for the table below?
Table emp:
| id | name | dept | salary | mgr |
|---|---|---|---|---|
| 1 | Anil | IT | 90000 | NULL |
| 2 | Bina | IT | 70000 | 1 |
| 3 | Chetan | HR | 50000 | 1 |
| 4 | Divya | IT | 70000 | 2 |
| 5 | Esha | HR | 65000 | 3 |
| 6 | Farhan | Sales | 55000 | 1 |
| 7 | Gita | Sales | 60000 | 6 |
| 8 | Hari | Sales | NULL | 6 |
SELECT name, RANK() OVER (ORDER BY salary DESC) FROM emp WHERE salary IS NOT NULL ORDER BY 2, name LIMIT 4;
Anil | 1, Bina | 2, Divya | 2, Esha | 3
Anil | 1, Bina | 2, Divya | 3, Esha | 4
Anil | 1, Divya | 2, Bina | 3, Esha | 4
Anil | 1, Bina | 2, Divya | 2, Esha | 4
Sign up free to see the answer and a step-by-step explanation.
What does this query return for the table below?
Table emp:
| id | name | dept | salary | mgr |
|---|---|---|---|---|
| 1 | Anil | IT | 90000 | NULL |
| 2 | Bina | IT | 70000 | 1 |
| 3 | Chetan | HR | 50000 | 1 |
| 4 | Divya | IT | 70000 | 2 |
| 5 | Esha | HR | 65000 | 3 |
| 6 | Farhan | Sales | 55000 | 1 |
| 7 | Gita | Sales | 60000 | 6 |
| 8 | Hari | Sales | NULL | 6 |
SELECT dept, SUM(CASE WHEN salary >= 60000 THEN 1 ELSE 0 END) FROM emp GROUP BY dept ORDER BY dept;
HR | 1, IT | 3, Sales | 1
IT | 3
Sign up free to see the answer and a step-by-step explanation.
What does this query return for the table below?
Table emp:
| id | name | dept | salary | mgr |
|---|---|---|---|---|
| 1 | Anil | IT | 90000 | NULL |
| 2 | Bina | IT | 70000 | 1 |
| 3 | Chetan | HR | 50000 | 1 |
| 4 | Divya | IT | 70000 | 2 |
| 5 | Esha | HR | 65000 | 3 |
| 6 | Farhan | Sales | 55000 | 1 |
| 7 | Gita | Sales | 60000 | 6 |
| 8 | Hari | Sales | NULL | 6 |
SELECT name FROM emp WHERE mgr IN (SELECT id FROM emp WHERE mgr = 1) ORDER BY name;
Divya, Esha, Gita, Hari
Divya
Sign up free to see the answer and a step-by-step explanation.
Which query returns the departments where every employee earns more than 50000 (ignoring NULL salaries)?
SELECT DISTINCT dept FROM emp WHERE MIN(salary) > 50000
SELECT dept FROM emp GROUP BY dept HAVING MIN(salary) > 50000
SELECT dept FROM emp WHERE salary > 50000 GROUP BY dept
SELECT dept FROM emp HAVING salary > 50000
Sign up free to see the answer and a step-by-step explanation.
What is the difference between WHERE and HAVING when both appear in a grouped query?
WHERE can use aggregates
WHERE filters rows before grouping; HAVING filters groups after aggregation
HAVING runs first
They are interchangeable
Sign up free to see the answer and a step-by-step explanation.
What does UNION do that UNION ALL does not?
Joins on a key
Sorts by the first column always
Keeps duplicates
Removes duplicate rows from the combined result
Sign up free to see the answer and a step-by-step explanation.
Which is true about a LEFT JOIN with a condition on the right table placed in WHERE instead of ON?
It returns more rows
Rows with no match are filtered out, so it behaves like an INNER JOIN
Nothing changes
It becomes a RIGHT JOIN
Sign up free to see the answer and a step-by-step explanation.
Which SQL feature computes a running total of sales by date?
DISTINCT amount
HAVING SUM(amount)
SUM(amount) OVER (ORDER BY sale_date)
GROUP BY sale_date
Sign up free to see the answer and a step-by-step explanation.
What does a recursive common table expression (WITH RECURSIVE) let you do?
Speed up every query
Query hierarchical data such as an org chart to any depth
Create tables
Delete duplicates only
Sign up free to see the answer and a step-by-step explanation.
Which query finds duplicate emails in a users table?
SELECT email FROM users WHERE COUNT(*) > 1
SELECT DISTINCT email FROM users
SELECT email FROM users GROUP BY email HAVING COUNT(*) > 1
SELECT email FROM users ORDER BY email
Sign up free to see the answer and a step-by-step explanation.
HR | 1, IT | 2, Sales | 1
HR | 2, IT | 3, Sales | 2
Bina, Chetan, Farhan
Esha, Gita