CodeEraFree roadmap

SQL Practice / Set operators

SQL Set Operators: practice questions with answers

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

Set operators combine the results of two SELECT queries. UNION stacks them and removes duplicate rows, UNION ALL stacks them and keeps duplicates, INTERSECT keeps only the rows that appear in both, and EXCEPT keeps rows from the first query that are not in the second. Both queries must return the same number of columns with matching types.

INTERSECT and EXCEPT need MySQL 8.0.31 or newer, or MariaDB 10.3 or newer. Oracle calls EXCEPT by the name MINUS.

SELECT ... FROM t1
UNION        -- or UNION ALL, INTERSECT, EXCEPT
SELECT ... FROM t2;
Set up the practice tables

Copy this script and run it once.

CREATE TABLE product_jan (
  prod_id     INT NOT NULL,
  prod_name   VARCHAR(45) NOT NULL,
  price       DECIMAL(10,0) NOT NULL,
  quantity    INT NOT NULL,
  expiry_date DATE NOT NULL,
  PRIMARY KEY (prod_id)
);

CREATE TABLE product_feb (
  prod_id     INT NOT NULL,
  prod_name   VARCHAR(45) NOT NULL,
  price       DECIMAL(10,0) NOT NULL,
  quantity    INT NOT NULL,
  expiry_date DATE NOT NULL,
  PRIMARY KEY (prod_id)
);

INSERT INTO product_jan VALUES
(1, 'Chocolate', 10, 15, '2024-05-10'),
  (2, 'Biscuits',  20, 10, '2025-10-30'),
  (3, 'Noodles',   60, 5,  '2025-11-01'),
  (4, 'Peanuts',   15, 20, '2024-06-06'),
  (5, 'Lays',      25, 30, '2025-05-17'),
  (6, 'Oats',      40, 25, '2025-02-14'),
  (7, 'Almonds',   150, 6, '2026-01-18'),
  (8, 'Pickle',    80, 3,  '2024-12-25');

INSERT INTO product_feb VALUES
(1, 'Cold Drink', 90, 10, '2024-08-21'),
  (2, 'Milk',       30, 50, '2023-06-20'),
  (3, 'Noodles',    60, 5,  '2025-11-01'),
  (4, 'Oil',        65, 18, '2024-02-28'),
  (5, 'Lays',       25, 30, '2025-05-17'),
  (6, 'Oats',       40, 25, '2025-02-14');

Table: product_jan

prod_idprod_namepricequantityexpiry_date
1Chocolate10152024-05-10
2Biscuits20102025-10-30
3Noodles6052025-11-01
4Peanuts15202024-06-06
5Lays25302025-05-17
6Oats40252025-02-14
7Almonds15062026-01-18
8Pickle8032024-12-25

Table: product_feb

prod_idprod_namepricequantityexpiry_date
1Cold Drink90102024-08-21
2Milk30502023-06-20
3Noodles6052025-11-01
4Oil65182024-02-28
5Lays25302025-05-17
6Oats40252025-02-14

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 1Retrieve all the products from both tables where the quantity sold is greater than 5 in January and February.

Show the answer and the output
SELECT * FROM product_jan WHERE quantity > 5
UNION
SELECT * FROM product_feb WHERE quantity > 5;

Output

prod_idprod_namepricequantityexpiry_date
1Chocolate10152024-05-10
2Biscuits20102025-10-30
4Peanuts15202024-06-06
5Lays25302025-05-17
6Oats40252025-02-14
7Almonds15062026-01-18
1Cold Drink90102024-08-21
2Milk30502023-06-20
4Oil65182024-02-28

UNION removes rows that are identical in both tables, such as Noodles, Lays and Oats.

Question 2Retrieve the common records from the two tables, i.e. the products sold in both January and February.

Show the answer and the output
SELECT * FROM product_jan
INTERSECT
SELECT * FROM product_feb;

Output

prod_idprod_namepricequantityexpiry_date
3Noodles6052025-11-01
5Lays25302025-05-17
6Oats40252025-02-14

INTERSECT compares whole rows, so a product only counts as common if every column matches.

Question 3Find the common product names sold in both months, along with their quantities, prices and total sales.

Show the answer and the output
SELECT prod_name, quantity, price, quantity * price AS total FROM product_jan
INTERSECT
SELECT prod_name, quantity, price, quantity * price AS total FROM product_feb;

Output

prod_namequantitypricetotal
Noodles560300
Lays3025750
Oats25401000

Question 4Retrieve the products sold in January for a price less than 30 that were not sold in February.

Show the answer and the output
SELECT * FROM product_jan WHERE price < 30
EXCEPT
SELECT * FROM product_feb;

Output

prod_idprod_namepricequantityexpiry_date
1Chocolate10152024-05-10
2Biscuits20102025-10-30
4Peanuts15202024-06-06

Question 5Retrieve the details of products sold in either January or February with a price between 10 and 50. Return all columns of the matching rows of both tables.

Show the answer and the output
SELECT * FROM product_jan WHERE price BETWEEN 10 AND 50
UNION
SELECT * FROM product_feb WHERE price BETWEEN 10 AND 50;

Output

prod_idprod_namepricequantityexpiry_date
1Chocolate10152024-05-10
2Biscuits20102025-10-30
4Peanuts15202024-06-06
5Lays25302025-05-17
6Oats40252025-02-14
2Milk30502023-06-20

Question 6Retrieve the total sales for each month as a union of two rows: the first row for January and the second for February. The columns are "Month" and "total_sales" (the sum of price times quantity).

Show the answer and the output
SELECT 'January' AS Month, SUM(price * quantity) AS total_sales FROM product_jan
UNION ALL
SELECT 'February' AS Month, SUM(price * quantity) AS total_sales FROM product_feb;

Output

Monthtotal_sales
January3840
February5620

Different from the PDF: UNION ALL is used instead of UNION. If both months had the same month name or total, UNION would silently drop a row.

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