Question 1Easy
What does this query return?
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT COUNT(DISTINCT salary) FROM employees;
- 3
- 5
- 1
- 4
Answer: D. 4
Explanation
The distinct salaries are 60000, 45000, 50000 and 70000. The duplicate 45000 is counted once.
Step-by-step solution
- Salaries: 60000, 45000, 50000, 70000, 45000.
- COUNT(DISTINCT salary) counts each value once: 60000, 45000, 50000, 70000.
- The count is 4 (option D).
Why the other options are wrong
- A. Three misses one of the four distinct salaries.
- B. Five counts the duplicate 45000 twice; DISTINCT removes it.
- C. One would mean all salaries are the same.
Tip: COUNT(DISTINCT col) counts unique non-NULL values.
Question 2Easy
How many rows does this query count?
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT COUNT(*) FROM employees
WHERE salary BETWEEN 45000 AND 60000;
- 4
- 3
- 2
- 5
Answer: A. 4
Explanation
BETWEEN is inclusive at both ends, so 45000 (Ravi, Kiran), 50000 (Meera) and 60000 (Asha) all match.
Step-by-step solution
- BETWEEN 45000 AND 60000 includes both ends.
- Matches: Asha 60000, Ravi 45000, Meera 50000, Kiran 45000. John 70000 is out.
- The count is 4 (option A).
Why the other options are wrong
- B. 3 would leave out one of the 45000 or boundary matches; Kiran and Ravi both match.
- C. 2 counts too few; four employees are in the range.
- D. 5 includes John, whose salary 70000 is above 60000.
Tip: BETWEEN includes both boundary values.
Question 3Easy
What does this query return?
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT COUNT(*) FROM employees WHERE salary > 45000;
- 5
- 3
- 4
- 2
Answer: B. 3
Explanation
Only Asha (60000), Meera (50000) and John (70000) have salary strictly greater than 45000.
Step-by-step solution
- Salaries above 45000: Asha 60000, Meera 50000 and John 70000.
- Ravi and Kiran have exactly 45000, which is not strictly greater.
- The count is 3 (option B).
Why the other options are wrong
- A. Five counts every employee; Ravi and Kiran at 45000 are not above 45000.
- C. Four would include an employee at exactly 45000, which fails >.
- D. Two misses one of the three employees above 45000.
Tip: > is strict; use >= when the boundary should count.
Question 5Easy
What does this query return? (MySQL, PostgreSQL or SQLite)
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT name FROM employees
ORDER BY salary DESC
LIMIT 1;
- John
- Asha
- Kiran
- Ravi
Answer: A. John
Explanation
Sorting by salary in descending order puts John (70000) first, and LIMIT 1 keeps only that row.
Step-by-step solution
- ORDER BY salary DESC puts the highest salary first.
- John has 70000, the highest.
- LIMIT 1 keeps only John (option A).
Why the other options are wrong
- B. Asha has 60000, the second highest.
- C. Kiran has 45000, one of the lowest.
- D. Ravi has 45000, which is not the highest.
Tip: ORDER BY ... DESC LIMIT 1 returns the top row.
Question 6Medium
What does this query return?
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT COUNT(*) FROM employees WHERE manager_id = NULL;
- 2
- 0
- 5
- 3
Answer: B. 0
Explanation
Comparing anything with NULL using = yields UNKNOWN, never TRUE, so no row matches. Use IS NULL instead, which would return 2.
Step-by-step solution
- manager_id = NULL compares with NULL, which is UNKNOWN, never TRUE.
- So no row passes the WHERE.
- The count is 0 (option B).
Why the other options are wrong
- A. Two is the count of rows with manager_id IS NULL, but = NULL never matches.
- C. Five would need the WHERE to be removed.
- D. Three is not any count the query can give.
Tip: Use IS NULL or IS NOT NULL to test for NULL; = NULL is never TRUE.
Question 7Medium
Which names does this query return?
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT name FROM employees WHERE name LIKE '_a%';
- (no rows)
- Ravi
- Asha, Ravi
- Asha
Answer: B. Ravi
Explanation
_ matches exactly one character and % matches any sequence, so the second letter must be 'a'. Only Ravi qualifies.
Step-by-step solution
- LIKE '_a%' means: one character, then 'a', then anything.
- Asha's second letter is 's'; Ravi's second letter is 'a'; Meera, John, Kiran fail.
- Only Ravi matches (option B).
Why the other options are wrong
- A. Ravi's name has 'a' as the second letter, so a row matches.
- C. Asha's second letter is 's', so she does not match.
- D. Asha's second letter is 's', not 'a', so she is wrong; Ravi is the match.
Tip: In LIKE, _ matches one character and % matches any run of characters.
Question 8Hard
Which names does this query return?
employees
id | name | dept | salary | manager_id
1 | Asha | Sales | 60000 | NULL
2 | Ravi | Sales | 45000 | 1
3 | Meera | HR | 50000 | 1
4 | John | IT | 70000 | NULL
5 | Kiran | IT | 45000 | 4
SELECT name FROM employees
WHERE dept = 'IT' OR dept = 'Sales' AND salary > 50000
ORDER BY id;
- John, Kiran
- Asha, John
- Asha, John, Kiran
- Asha
Answer: C. Asha, John, Kiran
Explanation
AND binds tighter than OR, so the condition is dept = 'IT' OR (dept = 'Sales' AND salary > 50000). That gives all IT employees (John, Kiran) plus Asha.
Step-by-step solution
- AND binds tighter than OR: dept = 'IT' OR (dept = 'Sales' AND salary > 50000).
- IT employees: John and Kiran. Sales with salary above 50000: Asha (60000).
- Ordered by id: Asha, John, Kiran (option C).
Why the other options are wrong
- A. Asha qualifies through Sales with 60000 above 50000, so she is missing.
- B. Kiran is in IT, so he qualifies and is missing.
- D. John and Kiran are both IT employees, so they qualify.
Tip: Use parentheses to make AND and OR precedence explicit.