WHERE keeps only the rows that match a condition. Use comparison operators (=, != or <>, <, >, <=, >=) with numbers, text and dates. Text and dates go in single quotes, and dates use the YYYY-MM-DD format.
SELECT columns FROM table_name WHERE condition;
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 whose joining_date is after 20 March 2022.
Show the answer and the output
SELECT * FROM employees WHERE joining_date > '2022-03-20';Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
Question 2Display emp_no, first_name, last_name and department of the employees whose department is 'Finance'.
Show the answer and the output
SELECT emp_no, first_name, last_name, department
FROM employees
WHERE department = 'Finance';Output
| emp_no | first_name | last_name | department |
|---|---|---|---|
| 1009 | Linda | Potter | Finance |
| 1011 | Susan | Hall | Finance |
| 1017 | Nancy | Parker | Finance |
| 1022 | Sarah | Harrison | Finance |
| 1029 | Nancy | Cole | Finance |
Question 3Display emp_no, salary and department of the employees whose salary is below 35000.
Show the answer and the output
SELECT emp_no, salary, department FROM employees WHERE salary < 35000;Output
| emp_no | salary | department |
|---|---|---|
| 1006 | 23800 | HR |
| 1008 | 26392 | Business Development |
| 1010 | 25600 | IT |
| 1014 | 23676 | IT |
Rows where salary is NULL are not returned, because NULL is never less than 35000.
Question 4Display all the records of employees whose birth_date is on or after 24 January 1998.
Show the answer and the output
SELECT * FROM employees WHERE birth_date >= '1998-01-24';Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 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 |
| 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 |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
Showing the first 12 of 21 rows.
Question 5Display emp_no, first_name and department of employees whose department is other than 'IT'.
Show the answer and the output
SELECT emp_no, first_name, department FROM employees WHERE department != 'IT';Output
| emp_no | first_name | department |
|---|---|---|
| 1001 | James | Marketing |
| 1003 | Mary | Business Development |
| 1004 | John | HR |
| 1005 | Jennifer | Marketing |
| 1006 | Michael | HR |
| 1008 | William | Business Development |
| 1009 | Linda | Finance |
| 1011 | Susan | Finance |
| 1012 | Joseph | Marketing |
| 1013 | Mary | Business Development |
| 1017 | Nancy | Finance |
| 1018 | Betty | HR |
Showing the first 12 of 19 rows.
Employees whose department is NULL are also left out. To include them, add OR department IS NULL.
Question 6Display all the records of employees whose emp_no is 1010 or below.
Show the answer and the output
SELECT * FROM employees WHERE emp_no <= 1010;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 |
| 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 |
Question 7Display the unique first names of male employees.
Show the answer and the output
SELECT DISTINCT first_name FROM employees WHERE gender = 'M';Output
| first_name |
|---|
| James |
| Robert |
| John |
| Michael |
| David |
| William |
| Richard |
| Joseph |
| Thomas |
| Charles |
| Christopher |
| Daniel |
Showing the first 12 of 16 rows.
Question 8Display emp_no, first_name and salary, and add 10000 to the salary of employees whose salary is below 25000. Name the new column 'final_salary'.
Show the answer and the output
SELECT emp_no, first_name, salary, salary + 10000 AS final_salary
FROM employees
WHERE salary < 25000;Output
| emp_no | first_name | salary | final_salary |
|---|---|---|---|
| 1006 | Michael | 23800 | 33800 |
| 1014 | Thomas | 23676 | 33676 |
Question 9Display emp_no and the full name as 'full_name' for employees whose emp_no is more than 1015.
Show the answer and the output
SELECT emp_no, CONCAT(first_name, ' ', last_name) AS full_name
FROM employees
WHERE emp_no > 1015;Output
| emp_no | full_name |
|---|---|
| 1016 | Jessica Clark |
| 1017 | Nancy Parker |
| 1018 | Betty Barker |
| 1019 | Christopher Gibson |
| 1020 | Daniel Elliott |
| 1021 | David Mills |
| 1022 | Sarah Harrison |
| 1023 | Matthew Hill |
| 1024 | Anthony Miller |
| 1025 | Mark Wood |
| 1026 | Elizabeth Walker |
| 1027 | Barbara Anderson |
Showing the first 12 of 15 rows.
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
