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_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT |
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
| 1021 | David | Mills | NULL | 1998-01-24 | M | 2019-12-26 | IT |
| 1022 | Sarah | Harrison | 44692 | 1996-03-20 | F | 2020-05-03 | Finance |
| 1023 | Matthew | Hill | 41365 | 1998-05-16 | M | 2019-05-23 | IT |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business 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_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
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_no | first_name | salary |
|---|---|---|
| 1011 | Susan | 54152 |
| 1012 | Joseph | 60556 |
| 1013 | Mary | 58246 |
| 1015 | Charles | 57226 |
| 1017 | Nancy | 66900 |
| 1019 | Christopher | 59341 |
| 1020 | Daniel | 44012 |
| 1022 | Sarah | 44692 |
| 1023 | Matthew | 41365 |
| 1024 | Anthony | 56794 |
| 1025 | Mark | 65120 |
| 1026 | Elizabeth | 47030 |
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_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1022 | Sarah | Harrison | 44692 | 1996-03-20 | F | 2020-05-03 | Finance |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance |
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_no | department | salary | joining_date |
|---|---|---|---|
| 1001 | Marketing | 45300 | 2020-11-29 |
| 1004 | HR | 36264 | 2021-04-14 |
| 1006 | HR | 23800 | 2022-06-09 |
| 1007 | IT | 39400 | 2021-10-17 |
| 1008 | Business Development | 26392 | 2019-09-30 |
| 1009 | Finance | 55231 | 2021-08-07 |
| 1010 | IT | 25600 | 2021-03-23 |
| 1011 | Finance | 54152 | 2020-08-07 |
| 1013 | Business Development | 58246 | 2021-04-30 |
| 1014 | IT | 23676 | 2022-03-02 |
| 1017 | Finance | 66900 | 2022-01-09 |
| 1020 | IT | 44012 | 2023-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_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business 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_no | first_name | birth_date |
|---|---|---|
| 1001 | James | 1998-01-19 |
| 1002 | Robert | 1998-09-02 |
| 1003 | Mary | 1997-11-07 |
| 1004 | John | 1999-02-01 |
| 1005 | Jennifer | 1996-06-05 |
| 1007 | David | 1998-04-26 |
| 1009 | Linda | 1998-01-24 |
| 1010 | Richard | 1996-08-07 |
| 1012 | Joseph | 1997-05-13 |
| 1013 | Mary | 1999-01-21 |
| 1014 | Thomas | 1996-10-12 |
| 1017 | Nancy | 1996-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_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1021 | David | Mills | NULL | 1998-01-24 | M | 2019-12-26 | IT |
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
