SELECT & Filtering Questions with Answers

8 practice questions on SELECT & Filtering (SQL) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

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;
  1. 3
  2. 5
  3. 1
  4. 4

Answer: D. 4

Explanation

The distinct salaries are 60000, 45000, 50000 and 70000. The duplicate 45000 is counted once.

Step-by-step solution

  1. Salaries: 60000, 45000, 50000, 70000, 45000.
  2. COUNT(DISTINCT salary) counts each value once: 60000, 45000, 50000, 70000.
  3. 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;
  1. 4
  2. 3
  3. 2
  4. 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

  1. BETWEEN 45000 AND 60000 includes both ends.
  2. Matches: Asha 60000, Ravi 45000, Meera 50000, Kiran 45000. John 70000 is out.
  3. 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;
  1. 5
  2. 3
  3. 4
  4. 2

Answer: B. 3

Explanation

Only Asha (60000), Meera (50000) and John (70000) have salary strictly greater than 45000.

Step-by-step solution

  1. Salaries above 45000: Asha 60000, Meera 50000 and John 70000.
  2. Ravi and Kiran have exactly 45000, which is not strictly greater.
  3. 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 4Easy

Which clause filters individual rows before any grouping takes place?

  1. WHERE
  2. ORDER BY
  3. GROUP BY
  4. HAVING

Answer: A. WHERE

Explanation

WHERE is applied to rows before GROUP BY. HAVING filters the groups produced by GROUP BY.

Step-by-step solution

  1. WHERE runs before grouping.
  2. HAVING runs after on groups.

Why the other options are wrong

  • B. ORDER BY sorts results.
  • C. GROUP BY forms groups.
  • D. HAVING filters groups.

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;
  1. John
  2. Asha
  3. Kiran
  4. 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

  1. ORDER BY salary DESC puts the highest salary first.
  2. John has 70000, the highest.
  3. 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;
  1. 2
  2. 0
  3. 5
  4. 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

  1. manager_id = NULL compares with NULL, which is UNKNOWN, never TRUE.
  2. So no row passes the WHERE.
  3. 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%';
  1. (no rows)
  2. Ravi
  3. Asha, Ravi
  4. 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

  1. LIKE '_a%' means: one character, then 'a', then anything.
  2. Asha's second letter is 's'; Ravi's second letter is 'a'; Meera, John, Kiran fail.
  3. 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;
  1. John, Kiran
  2. Asha, John
  3. Asha, John, Kiran
  4. 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

  1. AND binds tighter than OR: dept = 'IT' OR (dept = 'Sales' AND salary > 50000).
  2. IT employees: John and Kiran. Sales with salary above 50000: Asha (60000).
  3. 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.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all SQL topics · All topics