A join combines rows from two tables using a related column. INNER JOIN ... ON returns only the rows that have a match in both tables. NATURAL JOIN does the same automatically, using every column that has the same name in both tables. It is short, but risky: if someone later adds another column with a matching name, the join silently changes. Most teams prefer INNER JOIN ... ON.
The questions use two tables: vendors and products. Two products have no vendor (vend_id is NULL), so an inner join leaves them out.
SELECT columns
FROM table1
INNER JOIN table2 ON table1.key = table2.key;
SELECT columns FROM table1 NATURAL JOIN table2;
Set up the practice tables
Copy this script and run it once.
CREATE TABLE vendors (
vend_id CHAR(10) NOT NULL,
vend_name CHAR(50) NOT NULL,
vend_address CHAR(50),
vend_city CHAR(50),
vend_state CHAR(5),
vend_zip CHAR(10),
vend_country CHAR(50),
PRIMARY KEY (vend_id)
);
CREATE TABLE products (
prod_id CHAR(10) NOT NULL,
vend_id CHAR(10),
prod_name CHAR(255) NOT NULL,
prod_price DECIMAL(8,2) NOT NULL,
prod_desc TEXT,
PRIMARY KEY (prod_id),
FOREIGN KEY (vend_id) REFERENCES vendors (vend_id)
);
INSERT INTO vendors (vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country) VALUES
('BRS01', 'Bears R Us', '123 Main Street', 'Bear Town', 'MI', '44444', 'USA'),
('BRE02', 'Bear Emporium', '500 Park Street', 'Anytown', 'OH', '44333', 'USA'),
('DLL01', 'Doll House Inc.','555 High Street', 'Dollsville', 'CA', '99999', 'USA'),
('FRB01', 'Furball Inc.', '1000 5th Avenue', 'New York', 'NY', '11111', 'USA'),
('FNG01', 'Fun and Games', '42 Galaxy Road', 'London', NULL, 'N16 6PS', 'England'),
('JTS01', 'Jouets et ours', '1 Rue Amusement', 'Paris', NULL, '45678', 'France');
INSERT INTO products (prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES
('BR01', 'BRS01', '8 inch teddy bear', 5.99, '8 inch teddy bear, comes with cap and jacket'),
('BR02', NULL, '12 inch teddy bear', 8.99, '12 inch teddy bear, comes with cap and jacket'),
('BR03', 'BRS01', '18 inch teddy bear', 11.99, '18 inch teddy bear, comes with cap and jacket'),
('BNBG01', 'DLL01', 'Fish bean bag toy', 3.49, 'Fish bean bag toy, complete with bean bag worms with which to feed it'),
('BNBG02', 'DLL01', 'Bird bean bag toy', 3.49, 'Bird bean bag toy, eggs are not included'),
('BNBG03', NULL, 'Rabbit bean bag toy', 3.49, 'Rabbit bean bag toy, comes with bean bag carrots'),
('RGAN01', 'DLL01', 'Raggedy Ann', 4.99, '18 inch Raggedy Ann doll'),
('RYL01', 'FNG01', 'King doll', 9.49, '12 inch king doll with royal garments and crown'),
('RYL02', 'FNG01', 'Queen doll', 9.49, '12 inch queen doll with royal garments and crown');Table: vendors
| vend_id | vend_name | vend_address | vend_city | vend_state | vend_zip | vend_country |
|---|---|---|---|---|---|---|
| BRE02 | Bear Emporium | 500 Park Street | Anytown | OH | 44333 | USA |
| BRS01 | Bears R Us | 123 Main Street | Bear Town | MI | 44444 | USA |
| DLL01 | Doll House Inc. | 555 High Street | Dollsville | CA | 99999 | USA |
| FNG01 | Fun and Games | 42 Galaxy Road | London | NULL | N16 6PS | England |
| FRB01 | Furball Inc. | 1000 5th Avenue | New York | NY | 11111 | USA |
| JTS01 | Jouets et ours | 1 Rue Amusement | Paris | NULL | 45678 | France |
Table: products
| prod_id | vend_id | prod_name | prod_price | prod_desc |
|---|---|---|---|---|
| BNBG01 | DLL01 | Fish bean bag toy | 3.49 | Fish bean bag toy, complete with bean bag worms with which to feed it |
| BNBG02 | DLL01 | Bird bean bag toy | 3.49 | Bird bean bag toy, eggs are not included |
| BNBG03 | NULL | Rabbit bean bag toy | 3.49 | Rabbit bean bag toy, comes with bean bag carrots |
| BR01 | BRS01 | 8 inch teddy bear | 5.99 | 8 inch teddy bear, comes with cap and jacket |
| BR02 | NULL | 12 inch teddy bear | 8.99 | 12 inch teddy bear, comes with cap and jacket |
| BR03 | BRS01 | 18 inch teddy bear | 11.99 | 18 inch teddy bear, comes with cap and jacket |
| RGAN01 | DLL01 | Raggedy Ann | 4.99 | 18 inch Raggedy Ann doll |
| RYL01 | FNG01 | King doll | 9.49 | 12 inch king doll with royal garments and crown |
| RYL02 | FNG01 | Queen doll | 9.49 | 12 inch queen doll with royal garments and crown |
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 the product ID, product name and vendor name from the products and vendors tables, sorted by product ID.
Show the answer and the output
SELECT prod_id, prod_name, vend_name
FROM products
INNER JOIN vendors ON products.vend_id = vendors.vend_id
ORDER BY prod_id;Output
| prod_id | prod_name | vend_name |
|---|---|---|
| BNBG01 | Fish bean bag toy | Doll House Inc. |
| BNBG02 | Bird bean bag toy | Doll House Inc. |
| BR01 | 8 inch teddy bear | Bears R Us |
| BR03 | 18 inch teddy bear | Bears R Us |
| RGAN01 | Raggedy Ann | Doll House Inc. |
| RYL01 | King doll | Fun and Games |
| RYL02 | Queen doll | Fun and Games |
Question 2Retrieve the product ID, product name, vendor name and vendor country for products sold by vendors in the USA, sorted by product ID.
Show the answer and the output
SELECT prod_id, prod_name, vend_name, vend_country
FROM products
INNER JOIN vendors ON products.vend_id = vendors.vend_id
WHERE vend_country = 'USA'
ORDER BY prod_id;Output
| prod_id | prod_name | vend_name | vend_country |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | Doll House Inc. | USA |
| BNBG02 | Bird bean bag toy | Doll House Inc. | USA |
| BR01 | 8 inch teddy bear | Bears R Us | USA |
| BR03 | 18 inch teddy bear | Bears R Us | USA |
| RGAN01 | Raggedy Ann | Doll House Inc. | USA |
Question 3Retrieve the product ID, product name, vendor name and product price where the price is less than 5, sorted by product ID. Use a natural join.
Show the answer and the output
SELECT prod_id, prod_name, vend_name, prod_price
FROM products
NATURAL JOIN vendors
WHERE prod_price < 5
ORDER BY prod_id;Output
| prod_id | prod_name | vend_name | prod_price |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | Doll House Inc. | 3.49 |
| BNBG02 | Bird bean bag toy | Doll House Inc. | 3.49 |
| RGAN01 | Raggedy Ann | Doll House Inc. | 4.99 |
The only column name the two tables share is vend_id, so the natural join uses it.
Question 4Retrieve the product ID, vendor ID and vendor city for all products, sorted by vendor ID.
Show the answer and the output
SELECT prod_id, vendors.vend_id, vend_city
FROM products
INNER JOIN vendors ON products.vend_id = vendors.vend_id
ORDER BY vendors.vend_id;Output
| prod_id | vend_id | vend_city |
|---|---|---|
| BR01 | BRS01 | Bear Town |
| BR03 | BRS01 | Bear Town |
| BNBG01 | DLL01 | Dollsville |
| BNBG02 | DLL01 | Dollsville |
| RGAN01 | DLL01 | Dollsville |
| RYL01 | FNG01 | London |
| RYL02 | FNG01 | London |
vend_id is in both tables, so the query says which one to show with vendors.vend_id.
Question 5Retrieve the vendor name and the total amount of products sold by each vendor as "total_amount", sorted by the total amount.
Show the answer and the output
SELECT vend_name, SUM(prod_price) AS total_amount
FROM products
INNER JOIN vendors ON products.vend_id = vendors.vend_id
GROUP BY vend_name
ORDER BY total_amount;Output
| vend_name | total_amount |
|---|---|
| Doll House Inc. | 11.97 |
| Bears R Us | 17.98 |
| Fun and Games | 18.98 |
Question 6Retrieve the vendor name and the total amount including 5% GST for each vendor's products as "total_amount_GST_5", sorted by that amount.
Show the answer and the output
SELECT vend_name, SUM(prod_price * 1.05) AS total_amount_GST_5
FROM products
INNER JOIN vendors ON products.vend_id = vendors.vend_id
GROUP BY vend_name
ORDER BY total_amount_GST_5;Output
| vend_name | total_amount_GST_5 |
|---|---|
| Doll House Inc. | 12.5685 |
| Bears R Us | 18.8790 |
| Fun and Games | 19.9290 |
Question 7Retrieve the product ID, vendor ID, product name, vendor name and product price where the vendor ID ends with '01' and the product price is greater than 4.
Show the answer and the output
SELECT prod_id, vendors.vend_id, prod_name, vend_name, prod_price
FROM products
INNER JOIN vendors ON products.vend_id = vendors.vend_id
WHERE vendors.vend_id LIKE '%01' AND prod_price > 4;Output
| prod_id | vend_id | prod_name | vend_name | prod_price |
|---|---|---|---|---|
| BR01 | BRS01 | 8 inch teddy bear | Bears R Us | 5.99 |
| BR03 | BRS01 | 18 inch teddy bear | Bears R Us | 11.99 |
| RGAN01 | DLL01 | Raggedy Ann | Doll House Inc. | 4.99 |
| RYL01 | FNG01 | King doll | Fun and Games | 9.49 |
| RYL02 | FNG01 | Queen doll | Fun and Games | 9.49 |
Different from the PDF: The PDF said INNER JOIN vendor (a missing s), which fails because that table does not exist.
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
