SQL Set 2: query an EMPLOYEE table with WHERE, DISTINCT, IN, BETWEEN, LIKE and IS NULL
Expt. No.20
Page No.40
EMPLOYEE: WHERE, IN, BETWEEN, LIKE, NULL
Aim
To write SQL queries on an EMPLOYEE table using DISTINCT, aliasing, arithmetic, IN, BETWEEN, LIKE, IS NULL and logical operators.
Concepts used
Queries
USE company;-- Departments without repeatsSELECT DISTINCT dept FROM employee;-- Annual salary with a column aliasSELECT name, salary, salary * 12 AS annual_salary FROM employee;-- Employees of the Sales or HR departmentSELECT name, dept FROM employee WHERE dept IN ('Sales', 'HR');-- Salary between 40000 and 60000SELECT name, salary FROM employee WHERE salary BETWEEN 40000 AND 60000;-- Names starting with ASELECT name FROM employee WHERE name LIKE 'A%';-- Names whose second letter is aSELECT name FROM employee WHERE name LIKE '_a%';-- Employees who get no commissionSELECT name, commission FROM employee WHERE commission IS NULL;-- Sales employees earning more than 45000SELECT name, salary FROM employee WHERE dept = 'Sales' AND salary > 45000;-- Employees who joined before 2019SELECT name, doj FROM employee WHERE doj < '2019-01-01';
A marked line continues the line above it. In your file, write it on the same line.
Table used
Table: employee
+-------+--------+----------+--------+------------+------------+ | empno | name | dept | salary | commission | doj | +-------+--------+----------+--------+------------+------------+ | 101 | Anil | Sales | 45000 | 5000 | 2019-04-12 | | 102 | Bhavna | HR | 52000 | NULL | 2018-07-01 | | 103 | Charu | IT | 68000 | NULL | 2020-01-15 | | 104 | Amit | Sales | 39000 | 3000 | 2021-09-20 | | 105 | Farhan | IT | 75000 | NULL | 2017-03-05 | | 106 | Gauri | Accounts | 48000 | NULL | 2022-06-11 | | 107 | Harish | Sales | 56000 | 6500 | 2016-11-30 | | 108 | Kavita | HR | 41000 | NULL | 2023-02-14 | +-------+--------+----------+--------+------------+------------+
Output
mysql> USE company;
Database changed
mysql> SELECT DISTINCT dept FROM employee;
+----------+
| dept |
+----------+
| Sales |
| HR |
| IT |
| Accounts |
+----------+
4 rows in set
mysql> SELECT name, salary, salary * 12 AS annual_salary FROM employee;
+--------+--------+---------------+
| name | salary | annual_salary |
+--------+--------+---------------+
| Anil | 45000 | 540000 |
| Bhavna | 52000 | 624000 |
| Charu | 68000 | 816000 |
| Amit | 39000 | 468000 |
| Farhan | 75000 | 900000 |
| Gauri | 48000 | 576000 |
| Harish | 56000 | 672000 |
| Kavita | 41000 | 492000 |
+--------+--------+---------------+
8 rows in set
mysql> SELECT name, dept FROM employee WHERE dept IN ('Sales', 'HR');
+--------+-------+
| name | dept |
+--------+-------+
| Anil | Sales |
| Bhavna | HR |
| Amit | Sales |
| Harish | Sales |
| Kavita | HR |
+--------+-------+
5 rows in set
mysql> SELECT name, salary FROM employee WHERE salary BETWEEN 40000 AND 60000;
+--------+--------+
| name | salary |
+--------+--------+
| Anil | 45000 |
| Bhavna | 52000 |
| Gauri | 48000 |
| Harish | 56000 |
| Kavita | 41000 |
+--------+--------+
5 rows in set
mysql> SELECT name FROM employee WHERE name LIKE 'A%';
+------+
| name |
+------+
| Anil |
| Amit |
+------+
2 rows in set
mysql> SELECT name FROM employee WHERE name LIKE '_a%';
+--------+
| name |
+--------+
| Farhan |
| Gauri |
| Harish |
| Kavita |
+--------+
4 rows in set
mysql> SELECT name, commission FROM employee WHERE commission IS NULL;
+--------+------------+
| name | commission |
+--------+------------+
| Bhavna | NULL |
| Charu | NULL |
| Farhan | NULL |
| Gauri | NULL |
| Kavita | NULL |
+--------+------------+
5 rows in set
mysql> SELECT name, salary FROM employee WHERE dept = 'Sales' AND salary > 45000;
+--------+--------+
| name | salary |
+--------+--------+
| Harish | 56000 |
+--------+--------+
1 row in set
mysql> SELECT name, doj FROM employee WHERE doj < '2019-01-01';
+--------+------------+
| name | doj |
+--------+------------+
| Bhavna | 2018-07-01 |
| Farhan | 2017-03-05 |
| Harish | 2016-11-30 |
+--------+------------+
3 rows in set
✓ Printed by running this query set, not typed by hand.
Result
DISTINCT listed the 4 departments once each. IN found 5 employees in Sales or HR, BETWEEN found 5 salaries from 40000 to 60000 (both ends included),
LIKE 'A%' found Anil and Amit, and LIKE '_a%' found the 4 names with a as the second letter. 5 employees have no commission (IS NULL).Viva voce: what the examiner may ask
01What do % and _ mean in LIKE?
% matches any number of characters (even none); _ matches exactly one character.
02Is BETWEEN inclusive?
Yes. BETWEEN 40000 AND 60000 includes 40000 and 60000.
03Why can't you write commission = NULL?
NULL means "unknown", and comparing anything with NULL gives unknown, not true. Use IS NULL.
04What is a column alias?
A temporary heading for a column in the output, given with AS.
Variations for the lab test
- Display employees who are not in the IT department.
- Display names ending with "a".
- Display name and salary + commission for employees who get a commission.
! Common mistake Writing
WHERE dept = 'Sales' OR 'HR' does not test HR at all; use dept = 'Sales' OR dept = 'HR' or IN ('Sales', 'HR').