CodeEraFree roadmap

SQL Practice / AND, OR and NOT

SQL AND, OR, NOT Operators: practice questions with answers

7 questions with solved queries and real output. Outputs checked on MariaDB 10.11 (MySQL-compatible) on 23 November 2026.

AND needs both conditions to be true, OR needs at least one, and NOT flips a condition. When you mix AND and OR, SQL evaluates AND first. Put brackets around the OR part whenever you mean it to be checked first.

WHERE condition1 AND condition2
WHERE condition1 OR condition2
WHERE NOT condition
WHERE a = 1 AND (b = 2 OR c = 3)

These questions use the employees table (30 rows). Create it first with the copy-and-paste setup script. Here is the data, so you can check your answers by eye.

See the employees table

Table: employees

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1002RobertLee694101998-09-02M2019-12-26IT
1003MaryWood395181997-11-07F2019-09-12Business Development
1004JohnCole362641999-02-01M2021-04-14HR
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1007DavidMills394001998-04-26M2021-10-17IT
1008WilliamHill263921999-10-21M2019-09-30Business Development
1009LindaPotter552311998-01-24F2021-08-07Finance
1010RichardMartin256001996-08-07M2021-03-23IT
1011SusanHall541521999-12-27F2020-08-07Finance
1012JosephTurner605561997-05-13M2019-09-02Marketing
1013MaryGill582461999-01-21F2021-04-30Business Development
1014ThomasRyan236761996-10-12M2022-03-02IT
1015CharlesWilliams572262000-01-29M2019-11-05NULL
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1017NancyParker669001996-10-24F2022-01-09Finance
1018BettyBarker352881998-11-13F2019-12-26HR
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1020DanielElliott440121999-01-21M2023-01-08IT
1021DavidMillsNULL1998-01-24M2019-12-26IT
1022SarahHarrison446921996-03-20F2020-05-03Finance
1023MatthewHill413651998-05-16M2019-05-23IT
1024AnthonyMiller567941996-06-30M2019-02-28Business Development
1025MarkWood651201998-10-29M2020-07-30Business Development
1026ElizabethWalker470301998-08-11F2021-06-18NULL
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1028JessicaClark638361998-01-24F2021-05-23IT
1029NancyCole636692000-01-26F2019-12-26Finance
1030JoeBrooks440001999-08-05M2021-03-09Business Development

How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.

Question 1Display all the records of employees who belong to either the 'HR' or the 'Marketing' department.

Show the answer and the output
SELECT * FROM employees WHERE department = 'HR' OR department = 'Marketing';

Output

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1004JohnCole362641999-02-01M2021-04-14HR
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1012JosephTurner605561997-05-13M2019-09-02Marketing
1018BettyBarker352881998-11-13F2019-12-26HR
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR

Question 2Get emp_no, first_name and salary of employees whose emp_no is more than 1010 and whose salary is also more than 40000.

Show the answer and the output
SELECT emp_no, first_name, salary FROM employees WHERE emp_no > 1010 AND salary > 40000;

Output

emp_nofirst_namesalary
1011Susan54152
1012Joseph60556
1013Mary58246
1015Charles57226
1017Nancy66900
1019Christopher59341
1020Daniel44012
1022Sarah44692
1023Matthew41365
1024Anthony56794
1025Mark65120
1026Elizabeth47030

Showing the first 12 of 15 rows.

Question 3Display all the records of female employees who do not belong to the 'IT' department.

Show the answer and the output
SELECT * FROM employees WHERE gender = 'F' AND NOT department = 'IT';

Output

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1003MaryWood395181997-11-07F2019-09-12Business Development
1005JenniferGill489411996-06-05F2019-06-28Marketing
1009LindaPotter552311998-01-24F2021-08-07Finance
1011SusanHall541521999-12-27F2020-08-07Finance
1013MaryGill582461999-01-21F2021-04-30Business Development
1017NancyParker669001996-10-24F2022-01-09Finance
1018BettyBarker352881998-11-13F2019-12-26HR
1022SarahHarrison446921996-03-20F2020-05-03Finance
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1029NancyCole636692000-01-26F2019-12-26Finance

Employees with no department (NULL) are not returned, because NOT NULL is still unknown, not true.

Question 4Get emp_no, department, salary and joining_date of employees who either joined after 1 April 2020 or have a salary below 33000.

Show the answer and the output
SELECT emp_no, department, salary, joining_date
FROM employees
WHERE joining_date > '2020-04-01' OR salary < 33000;

Output

emp_nodepartmentsalaryjoining_date
1001Marketing453002020-11-29
1004HR362642021-04-14
1006HR238002022-06-09
1007IT394002021-10-17
1008Business Development263922019-09-30
1009Finance552312021-08-07
1010IT256002021-03-23
1011Finance541522020-08-07
1013Business Development582462021-04-30
1014IT236762022-03-02
1017Finance669002022-01-09
1020IT440122023-01-08

Showing the first 12 of 18 rows.

Question 5Get all the records of male employees whose department is 'Business Development', 'Marketing' or 'Finance'.

Show the answer and the output
SELECT * FROM employees
WHERE gender = 'M'
  AND (department = 'Business Development'
    OR department = 'Marketing'
    OR department = 'Finance');

Output

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1008WilliamHill263921999-10-21M2019-09-30Business Development
1012JosephTurner605561997-05-13M2019-09-02Marketing
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1024AnthonyMiller567941996-06-30M2019-02-28Business Development
1025MarkWood651201998-10-29M2020-07-30Business Development
1030JoeBrooks440001999-08-05M2021-03-09Business Development

The brackets matter. Without them, AND runs first and the last two departments would no longer be limited to males.

Question 6Get emp_no, first_name and birth_date of employees whose emp_no is below 1020 and who were not born after 9 March 1999.

Show the answer and the output
SELECT emp_no, first_name, birth_date
FROM employees
WHERE emp_no < 1020 AND birth_date <= '1999-03-09';

Output

emp_nofirst_namebirth_date
1001James1998-01-19
1002Robert1998-09-02
1003Mary1997-11-07
1004John1999-02-01
1005Jennifer1996-06-05
1007David1998-04-26
1009Linda1998-01-24
1010Richard1996-08-07
1012Joseph1997-05-13
1013Mary1999-01-21
1014Thomas1996-10-12
1017Nancy1996-10-24

Showing the first 12 of 14 rows.

Different from the PDF: The PDF used birth_date < '1999-03-09'. 'Not born after 9 March 1999' also includes people born on that day, so it should be <=.

Question 7Display all the records of employees whose first name is 'David' or 'Mary' and whose last name is not 'Gill'.

Show the answer and the output
SELECT * FROM employees
WHERE (first_name = 'David' OR first_name = 'Mary')
  AND NOT last_name = 'Gill';

Output

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1003MaryWood395181997-11-07F2019-09-12Business Development
1007DavidMills394001998-04-26M2021-10-17IT
1021DavidMillsNULL1998-01-24M2019-12-26IT

Want these as PDFs? Download the question PDF or the answer PDF to practise offline.