Practice Questions — SELECT Queries and the WHERE Clause
← Back to NotesTopic-Specific Questions
Question 1
Easy
Given the employees table, what does this return?
SELECT name FROM employees WHERE id = 3;Look up the row with id = 3.
Rohan GuptaQuestion 2
Easy
How many rows does this return?
SELECT * FROM employees WHERE department = 'IT';Count IT department employees in the sample table.
4 rowsQuestion 3
Easy
How many rows does this return?
SELECT * FROM employees WHERE salary > 100000;Who earns strictly more than 100000?
1 rowQuestion 4
Easy
How many rows does this return?
SELECT name FROM employees WHERE department != 'IT';Total minus IT employees.
6 rowsQuestion 5
Easy
Write a query to find all employees in the IT department earning more than 50000. Return name and salary.
Use WHERE with AND combining two conditions.
SELECT name, salary FROM employees
WHERE department = 'IT' AND salary > 50000;Question 6
Easy
Find all employees with salary between 60000 and 90000. Return all columns. Use BETWEEN.
Remember BETWEEN is inclusive on both ends.
SELECT * FROM employees
WHERE salary BETWEEN 60000 AND 90000;Question 7
Easy
Find employees in HR or Marketing departments. Use the IN operator.
IN takes a comma-separated list in parentheses.
SELECT name, department FROM employees
WHERE department IN ('HR', 'Marketing');Question 8
Easy
What does this return?
SELECT name AS emp FROM employees WHERE id = 7;Find id 7 and note the aliased column.
Column header: emp
Value: Karan MehtaQuestion 9
Medium
How many rows does this return?
SELECT * FROM employees
WHERE department = 'IT' OR salary > 80000;Union of IT employees and high earners.
6 rowsQuestion 10
Medium
How many rows?
SELECT * FROM employees
WHERE department = 'IT' AND salary > 80000;Intersection of IT and high earners.
2 rowsQuestion 11
Medium
What is returned?
SELECT name FROM employees
WHERE department = 'IT' OR department = 'HR' AND salary > 60000;Trace the precedence: AND binds tighter than OR.
All 4 IT employees (Aarav, Rohan, Vikram, Diya) and zero HR employees (neither Priya nor Ishita earns above 60000). Total: 4 rows.Question 12
Medium
How many rows?
SELECT * FROM employees
WHERE (department = 'IT' OR department = 'HR') AND salary > 60000;Parentheses force OR first.
3 rowsQuestion 13
Medium
Find employees who joined before January 1, 2021. Return name and join_date.
Compare join_date against a date literal in single quotes.
SELECT name, join_date FROM employees
WHERE join_date < '2021-01-01';Question 14
Medium
Find IT employees NOT earning more than 70000. Return name and salary.
Use NOT or the opposite operator.
SELECT name, salary FROM employees
WHERE department = 'IT' AND NOT (salary > 70000);
-- Equivalent:
SELECT name, salary FROM employees
WHERE department = 'IT' AND salary <= 70000;Question 15
Medium
What does this return?
SELECT name FROM employees
WHERE department NOT IN ('IT', 'Finance', 'HR');Which departments are left?
Sneha Iyer
Aditya RaoQuestion 16
Medium
Find employees whose salary is NOT between 50000 and 80000 (i.e., outside this range). Return name and salary.
Use NOT BETWEEN or OR with < and >.
SELECT name, salary FROM employees
WHERE salary NOT BETWEEN 50000 AND 80000;
-- Equivalent:
SELECT name, salary FROM employees
WHERE salary < 50000 OR salary > 80000;Question 17
Medium
Find employees who joined in 2021, showing name with alias 'emp' and join_date with alias 'joined'.
Use BETWEEN on dates and AS for aliases.
SELECT name AS emp, join_date AS joined FROM employees
WHERE join_date BETWEEN '2021-01-01' AND '2021-12-31';Question 18
Easy
What is the difference between SELECT * and SELECT column1, column2?
Think about columns returned and production concerns.
SELECT * returns all columns of the table. SELECT column1, column2 returns only the listed columns. The star is convenient during exploration but should be avoided in production code because (a) it fetches unnecessary data, (b) the result shape changes if the schema changes, and (c) indexes cannot be used as effectively.Question 19
Easy
Why does SQL use single = for equality comparison instead of ==?
SQL predates most modern languages.
SQL was designed in the 1970s before languages like C, Java, and Python adopted
==. In SQL, there is no assignment operator inside queries (column values are set with = in UPDATE statements), so reusing = for equality comparison caused no ambiguity. Writing == in SQL is a syntax error.Question 20
Medium
Explain why NOT IN with a NULL value returns zero rows. Give a way to fix it.
SQL's three-valued logic: TRUE, FALSE, NULL.
In SQL, comparisons with NULL return NULL (not TRUE, not FALSE). The WHERE clause keeps only rows where the condition is TRUE.
x NOT IN (1, 2, NULL) is equivalent to x != 1 AND x != 2 AND x != NULL. The last part is always NULL, so the AND is NULL, so no row passes. Fix: filter out NULLs explicitly using AND col IS NOT NULL, or use NOT EXISTS with a subquery instead of NOT IN.Question 21
Medium
What does this return?
SELECT name FROM employees
WHERE salary IN (45000, 52000, 110000);Three exact salary matches.
Vikram Reddy (45000)
Priya Patel (52000)
Diya Nair (110000)Question 22
Medium
Find HR employees earning less than 50000. Return name and salary.
AND with two conditions.
SELECT name, salary FROM employees
WHERE department = 'HR' AND salary < 50000;Question 23
Medium
Find employees in Finance or Marketing earning at least 65000. Return name, department, and salary.
IN combined with a salary filter.
SELECT name, department, salary FROM employees
WHERE department IN ('Finance', 'Marketing')
AND salary >= 65000;Question 24
Medium
Why is it recommended to always use parentheses when combining AND and OR?
Precedence and readability.
Because AND is evaluated before OR in SQL, the meaning of a query can change in non-obvious ways. A query like
a = 1 OR b = 2 AND c = 3 is actually a = 1 OR (b = 2 AND c = 3). Parentheses make the intent explicit and prevent bugs. Reviewers can verify logic at a glance instead of tracing precedence rules in their heads.Question 25
Medium
What does this return?
SELECT name FROM employees
WHERE id BETWEEN 3 AND 7;BETWEEN on integer ids, inclusive.
Rohan Gupta (3)
Ananya Singh (4)
Vikram Reddy (5)
Sneha Iyer (6)
Karan Mehta (7)Question 26
Hard
Find employees who are (in IT AND earning above 80000) OR (in Finance AND joined before 2021). Return name, department, salary, join_date.
Two grouped conditions joined by OR.
SELECT name, department, salary, join_date FROM employees
WHERE (department = 'IT' AND salary > 80000)
OR (department = 'Finance' AND join_date < '2021-01-01');Question 27
Hard
Find all employees NOT in IT whose salary is between 50000 and 70000 inclusive. Use NOT and BETWEEN.
NOT with department, BETWEEN with salary.
SELECT name, department, salary FROM employees
WHERE NOT (department = 'IT')
AND salary BETWEEN 50000 AND 70000;
-- Equivalent cleaner version:
SELECT name, department, salary FROM employees
WHERE department != 'IT'
AND salary BETWEEN 50000 AND 70000;Question 28
Hard
What does this return?
SELECT name FROM employees
WHERE department = 'IT' AND NOT salary > 50000;NOT applies to the comparison.
Vikram ReddyQuestion 29
Hard
Using aliases, find name (as 'emp'), salary (as 'pay_inr') for employees in ('IT','HR') who joined between 2020 and 2022 inclusive, and earn more than 50000.
Combine IN, BETWEEN, comparison, and AS.
SELECT name AS emp, salary AS pay_inr FROM employees
WHERE department IN ('IT', 'HR')
AND join_date BETWEEN '2020-01-01' AND '2022-12-31'
AND salary > 50000;Question 30
Medium
What is the difference between != and <> in SQL? Which should you use?
Both mean the same thing but have different origins.
Both
!= and <> mean "not equal to" and work identically in MySQL, PostgreSQL, and SQL Server. <> is the SQL standard (ANSI SQL). != is more familiar to programmers. Use != for readability unless your team's style guide mandates the ANSI standard. Both are correct.Mixed & Application Questions
Question 1
Easy
Write a query that returns just the names of all employees.
Single column, no WHERE.
SELECT name FROM employees;Question 2
Easy
Find the employee with id 5. Return all columns.
WHERE id = 5.
SELECT * FROM employees WHERE id = 5;Question 3
Easy
How many rows?
SELECT * FROM employees WHERE salary = 45000;Exact match on salary.
1 rowQuestion 4
Easy
Find all Marketing employees.
WHERE department = 'Marketing'.
SELECT * FROM employees WHERE department = 'Marketing';Question 5
Easy
How many rows?
SELECT * FROM employees WHERE salary >= 50000 AND salary <= 70000;Range 50000-70000 inclusive.
5 rowsQuestion 6
Easy
Return employee names with alias 'worker_name'.
Use AS.
SELECT name AS worker_name FROM employees;Question 7
Easy
Find employees in IT. Use a straightforward WHERE clause.
Department equals IT.
SELECT * FROM employees WHERE department = 'IT';Question 8
Easy
How many rows?
SELECT * FROM employees WHERE salary < 50000;Two low earners.
2 rowsQuestion 9
Medium
Find employees whose name is either 'Priya Patel' or 'Karan Mehta'. Use IN.
IN with string values.
SELECT * FROM employees
WHERE name IN ('Priya Patel', 'Karan Mehta');Question 10
Medium
Find all employees except those in IT. Return name and department.
Use NOT or !=.
SELECT name, department FROM employees
WHERE department != 'IT';Question 11
Medium
Find employees who joined in 2022. Use BETWEEN with dates.
Full year range.
SELECT name, join_date FROM employees
WHERE join_date BETWEEN '2022-01-01' AND '2022-12-31';Question 12
Medium
Find employees in HR earning more than 50000 OR in Finance earning more than 70000.
Two grouped conditions joined by OR.
SELECT name, department, salary FROM employees
WHERE (department = 'HR' AND salary > 50000)
OR (department = 'Finance' AND salary > 70000);Question 13
Medium
How many rows?
SELECT * FROM employees
WHERE department = 'IT' OR department = 'IT';Redundant OR.
4 rowsQuestion 14
Medium
Find employees whose salary is exactly 75000, 52000, or 92000.
IN with three numeric values.
SELECT name, salary FROM employees
WHERE salary IN (75000, 52000, 92000);Question 15
Medium
Find employees who joined on or after 2022-01-01 and work in IT.
AND with date and department.
SELECT name, join_date FROM employees
WHERE join_date >= '2022-01-01'
AND department = 'IT';Question 16
Medium
What does this return?
SELECT name, salary * 12 AS annual_salary
FROM employees WHERE id = 1;Expression with alias.
Aarav Sharma, 900000Question 17
Medium
Find employees in IT earning between 50000 and 100000 (inclusive). Return name and salary.
Combine department check and BETWEEN.
SELECT name, salary FROM employees
WHERE department = 'IT'
AND salary BETWEEN 50000 AND 100000;Question 18
Medium
How many rows?
SELECT * FROM employees
WHERE department != 'IT' AND department != 'HR' AND department != 'Finance';Equivalent to NOT IN.
2 rowsQuestion 19
Medium
Find employees with the name 'Aarav Sharma'. Return all columns.
Exact string match with single quotes.
SELECT * FROM employees WHERE name = 'Aarav Sharma';Question 20
Hard
Find employees who either (a) are in Marketing or (b) joined after 2022-06-01 and earn below 60000. Return everything.
OR between two grouped conditions.
SELECT * FROM employees
WHERE department = 'Marketing'
OR (join_date > '2022-06-01' AND salary < 60000);Question 21
Hard
How many rows?
SELECT * FROM employees
WHERE salary > 60000 AND salary < 80000
OR department = 'HR';AND binds tighter than OR.
5 rowsQuestion 22
Hard
Find employees whose id is NOT between 3 and 7 (exclusive). Use NOT BETWEEN and return name.
NOT BETWEEN is inclusive of endpoints too.
SELECT name FROM employees WHERE id NOT BETWEEN 3 AND 7;Question 23
Hard
Find all employees whose salary multiplied by 12 exceeds 1000000 (i.e., annual > 10 lakh). Use an expression in WHERE.
You can compute expressions in WHERE.
SELECT name, salary FROM employees
WHERE salary * 12 > 1000000;Question 24
Hard
What does this return?
SELECT name FROM employees
WHERE NOT (department = 'IT' OR salary > 80000);De Morgan's law: NOT (A OR B) = NOT A AND NOT B.
Priya Patel
Ananya Singh
Sneha Iyer
Ishita Kumar
Aditya RaoQuestion 25
Hard
Find all employees except those in the HR department who earn less than 50000. (Keep HR folks who earn >= 50000, keep all non-HR.)
Exclude specific subgroup. Use NOT with parentheses.
SELECT * FROM employees
WHERE NOT (department = 'HR' AND salary < 50000);Multiple Choice Questions
MCQ 1
Which keyword is used to retrieve data from a table in SQL?
Answer: C
C is correct.
C is correct.
SELECT is the SQL keyword for retrieving data. FETCH exists in some SQL dialects but only inside cursors, not for top-level queries. GET and RETRIEVE are not SQL keywords.MCQ 2
What does SELECT * FROM employees do?
Answer: B
B is correct. The asterisk
B is correct. The asterisk
* means all columns. Without a WHERE clause, all rows are returned. So SELECT * FROM employees returns the entire table.MCQ 3
Which operator is used for equality comparison in SQL?
Answer: B
B is correct. SQL uses a single
B is correct. SQL uses a single
= for equality. Writing == causes a syntax error. This differs from Python, Java, JavaScript, and many other languages.MCQ 4
Which keyword renames a column in the output?
Answer: C
C is correct.
C is correct.
SELECT name AS employee_name renames the output column. RENAME is used for renaming tables/columns permanently (with ALTER TABLE), not for query-time aliasing.MCQ 5
How do you write a string literal in SQL?
Answer: B
B is correct. Single quotes are the SQL standard for string literals. Double quotes are for identifiers in ANSI SQL. Backticks are for identifiers in MySQL. Strings without quotes are column references or keywords.
B is correct. Single quotes are the SQL standard for string literals. Double quotes are for identifiers in ANSI SQL. Backticks are for identifiers in MySQL. Strings without quotes are column references or keywords.
MCQ 6
Which operator checks if a value equals any in a list?
Answer: C
C is correct.
C is correct.
column IN (v1, v2, v3) is shorthand for multiple ORs. LIKE does pattern matching. IS is used with NULL (IS NULL, IS NOT NULL). HAS is not SQL.MCQ 7
Is BETWEEN inclusive or exclusive?
Answer: B
B is correct.
B is correct.
BETWEEN x AND y is equivalent to >= x AND <= y. Both endpoints are included. This is a classic interview question and catches many beginners.MCQ 8
Which is NOT a valid comparison operator in SQL?
Answer: C
C is correct (the invalid one). SQL does not have
C is correct (the invalid one). SQL does not have
==. Both != and <> mean 'not equal.' >= is 'greater than or equal.' Using == produces a syntax error.MCQ 9
Which query finds IT employees earning more than 50000?
Answer: B
B is correct. IT is a string (single quotes), equality uses a single =, and AND requires both conditions true. Option A misses quotes around IT (parsed as a column name). Option C uses invalid ==. Option D uses OR which would return a totally different set.
B is correct. IT is a string (single quotes), equality uses a single =, and AND requires both conditions true. Option A misses quotes around IT (parsed as a column name). Option C uses invalid ==. Option D uses OR which would return a totally different set.
MCQ 10
In SQL, which has higher precedence: AND or OR?
Answer: B
B is correct. AND has higher precedence than OR, just like multiplication vs addition.
B is correct. AND has higher precedence than OR, just like multiplication vs addition.
a OR b AND c is a OR (b AND c). Always use parentheses when mixing them to avoid confusion.MCQ 11
What does WHERE salary NOT IN (50000, 70000, NULL) return?
Answer: B
B is correct. When NOT IN has NULL, SQL's three-valued logic makes the comparison to NULL yield NULL. WHERE requires TRUE, so no rows pass. This is a famous gotcha and interview question.
B is correct. When NOT IN has NULL, SQL's three-valued logic makes the comparison to NULL yield NULL. WHERE requires TRUE, so no rows pass. This is a famous gotcha and interview question.
MCQ 12
Which is equivalent to: WHERE salary BETWEEN 50000 AND 80000?
Answer: B
B is correct. BETWEEN is inclusive, so it matches
B is correct. BETWEEN is inclusive, so it matches
>= x AND <= y. Option A would be exclusive. Option C only matches the two exact values, not the range.MCQ 13
Which clause filters rows before they are returned?
Answer: B
B is correct. WHERE filters rows. SELECT picks columns. FROM identifies the table. AS creates aliases. Without WHERE, all rows are returned.
B is correct. WHERE filters rows. SELECT picks columns. FROM identifies the table. AS creates aliases. Without WHERE, all rows are returned.
MCQ 14
What does this return? SELECT * FROM employees WHERE department = 'IT' OR department = 'HR' AND salary > 60000;
Answer: A
A is correct. AND binds tighter than OR, so the query is: IT employees (any salary) OR (HR AND salary > 60000). All IT employees qualify. HR employees must also earn > 60000. To restrict both departments to high salary, you need parentheses.
A is correct. AND binds tighter than OR, so the query is: IT employees (any salary) OR (HR AND salary > 60000). All IT employees qualify. HR employees must also earn > 60000. To restrict both departments to high salary, you need parentheses.
MCQ 15
What is the output of SELECT name AS n FROM employees WHERE id = 1?
Answer: B
B is correct. AS renames the output column only. The underlying table column is still 'name'. The value retrieved (Aarav Sharma) is unchanged. AS works fine alongside WHERE.
B is correct. AS renames the output column only. The underlying table column is still 'name'. The value retrieved (Aarav Sharma) is unchanged. AS works fine alongside WHERE.
MCQ 16
Which is the BEST replacement for: WHERE dept = 'IT' OR dept = 'HR' OR dept = 'Finance'?
Answer: A
A is correct. IN is exactly designed for this. Option B treats strings alphabetically and returns unexpected results. Option C is a tuple mismatch error. Option D doesn't do anything close.
A is correct. IN is exactly designed for this. Option B treats strings alphabetically and returns unexpected results. Option C is a tuple mismatch error. Option D doesn't do anything close.
MCQ 17
What does SELECT * FROM employees WHERE salary > 60000 AND (department = 'IT' OR department = 'Finance') return for our 10-row sample?
Answer: C
C is correct. IT or Finance: Aarav (75000), Rohan (92000), Ananya (68000), Vikram (45000), Karan (81000), Diya (110000). Of those, salary > 60000 removes only Vikram. 5 rows: Aarav, Rohan, Ananya, Karan, Diya.
C is correct. IT or Finance: Aarav (75000), Rohan (92000), Ananya (68000), Vikram (45000), Karan (81000), Diya (110000). Of those, salary > 60000 removes only Vikram. 5 rows: Aarav, Rohan, Ananya, Karan, Diya.
MCQ 18
Which statement is TRUE about the SQL = operator?
Answer: B
B is correct. SQL's = means assignment in UPDATE statements (
B is correct. SQL's = means assignment in UPDATE statements (
SET salary = 60000) and equality in WHERE (WHERE salary = 60000). Context distinguishes the two. String case-sensitivity depends on column collation — MySQL is case-insensitive by default.MCQ 19
What does NOT (A AND B) equal according to De Morgan's law?
Answer: B
B is correct. De Morgan's law:
B is correct. De Morgan's law:
NOT (A AND B) = NOT A OR NOT B. And NOT (A OR B) = NOT A AND NOT B. Useful for rewriting complex negations in WHERE clauses.MCQ 20
Why is SELECT * considered bad practice in production code?
Answer: C
C is correct. If someone adds a column, your code silently starts fetching extra data. Also, the database optimizer cannot use covering indexes with
C is correct. If someone adds a column, your code silently starts fetching extra data. Also, the database optimizer cannot use covering indexes with
*. In production, always list columns explicitly.Coding Challenges
Challenge 1: Top IT Earners
EasyReturn all IT employees earning more than 70000. Include name, salary, and join_date.
Sample Input
Uses the employees table defined in the chapter.
Sample Output
name, salary, join_date:
Aarav Sharma 75000 2021-03-15
Rohan Gupta 92000 2019-01-10
Diya Nair 110000 2018-08-12
Use WHERE with AND. Do not use subqueries.
SELECT name, salary, join_date
FROM employees
WHERE department = 'IT' AND salary > 70000;Challenge 2: Mid-Band Salaries
EasyFind employees with salary between 55000 and 85000 (inclusive). Show name and salary, alias salary as 'pay'.
Sample Input
employees table.
Sample Output
Aarav Sharma 75000
Ananya Singh 68000
Sneha Iyer 58000
Karan Mehta 81000
Aditya Rao 63000
Use BETWEEN.
SELECT name, salary AS pay
FROM employees
WHERE salary BETWEEN 55000 AND 85000;Challenge 3: Multi-Department Filter
MediumFind all employees in IT, Finance, or Marketing who earn more than 60000 and joined after 2020-01-01. Return name, department, salary, join_date.
Sample Input
employees table.
Sample Output
Aarav Sharma IT 75000 2021-03-15
Ananya Singh Finance 68000 2022-11-05
Sneha Iyer Marketing 58000 ... (actually Sneha fails, 58000 < 60000)
Aditya Rao Marketing 63000 2022-03-08
Combine IN, comparison, and date filter. Correct output should be 3 rows: Aarav, Ananya, Aditya.
SELECT name, department, salary, join_date
FROM employees
WHERE department IN ('IT', 'Finance', 'Marketing')
AND salary > 60000
AND join_date > '2020-01-01';Challenge 4: Exclude Low HR Earners
MediumReturn every employee except HR staff earning below 50000. Keep all non-HR employees and HR employees earning 50000 or more.
Sample Input
employees table.
Sample Output
9 rows — everyone except Ishita Kumar (HR, 47000).
Use NOT with parentheses. The condition should exclude (department = 'HR' AND salary < 50000).
SELECT *
FROM employees
WHERE NOT (department = 'HR' AND salary < 50000);Challenge 5: Annual Salary Report
MediumFor each employee, return name and their annual salary (salary * 12) with alias 'annual_pay', but only for employees whose annual pay exceeds 800000.
Sample Input
employees table.
Sample Output
Rohan Gupta 1104000
Karan Mehta 972000
Diya Nair 1320000
Aarav Sharma 900000
Use an expression in SELECT and WHERE.
SELECT name, salary * 12 AS annual_pay
FROM employees
WHERE salary * 12 > 800000;Challenge 6: Complex Recruitment Filter
HardThe recruitment team wants to review: (a) all Marketing employees regardless of salary, OR (b) any employee who joined in the last 3 years (after 2023-04-16) AND earns less than 50000. Return name, department, salary, join_date.
Sample Input
employees table. Today's date treated as 2026-04-16.
Sample Output
Sneha Iyer Marketing 58000 2021-09-30
Aditya Rao Marketing 63000 2022-03-08
Ishita Kumar HR 47000 2023-06-25
Use OR with two grouped conditions.
SELECT name, department, salary, join_date
FROM employees
WHERE department = 'Marketing'
OR (join_date > '2023-04-16' AND salary < 50000);Challenge 7: Find Specific Employees by Name
HardThe manager wants details of three specific employees: Aarav Sharma, Ananya Singh, and Diya Nair. Return all their columns in a single query.
Sample Input
employees table.
Sample Output
3 rows — Aarav (id 1), Ananya (id 4), Diya (id 10).
Use IN on the name column.
SELECT *
FROM employees
WHERE name IN ('Aarav Sharma', 'Ananya Singh', 'Diya Nair');Challenge 8: Salary Bonus Eligibility
HardAn employee is eligible for a bonus if they (earn at least 70000 AND joined before 2022-01-01) OR (are in Marketing AND earn less than 65000). Return name, department, and a computed column 'bonus_eligible' showing the word 'YES' using alias.
Sample Input
employees table.
Sample Output
Aarav Sharma IT YES
Rohan Gupta IT YES
Karan Mehta Finance YES
Sneha Iyer Marketing YES
Aditya Rao Marketing YES
Diya Nair IT YES
Use a string literal in SELECT with AS. Include parentheses in the WHERE clause.
SELECT name, department, 'YES' AS bonus_eligible
FROM employees
WHERE (salary >= 70000 AND join_date < '2022-01-01')
OR (department = 'Marketing' AND salary < 65000);Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to master SQL and databases with a mentor?
Explore our MySQL Masterclass