We have a library (table library). In that library many shelves (column shelf). On those shelves can be books with different colors (column color).
The task: return the numbers of the shelves where stays only red books.
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (1, '2', 'red');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (2, '2', 'white');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (3, '3', 'red');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (4, '4', 'red');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (5, '4', 'red');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (6, '2', 'blue');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (7, '4', 'red');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (8, '3', 'green');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (9, '1', 'blue');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (10, '1', 'blue');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (11, '5', 'green');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (12, '5', 'black');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (13, '6', 'red');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (14, '6', 'red');
INSERT INTO `test` (`id`, `shelf`, `color`) VALUES (15, '6', 'white');
In the case above, it should return only shelf number 4.
Try this query –