CodeEraFree roadmap

SQL Practice / INNER and NATURAL JOIN

SQL INNER JOIN and NATURAL JOIN: practice questions with answers

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

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_idvend_namevend_addressvend_cityvend_statevend_zipvend_country
BRE02Bear Emporium500 Park StreetAnytownOH44333USA
BRS01Bears R Us123 Main StreetBear TownMI44444USA
DLL01Doll House Inc.555 High StreetDollsvilleCA99999USA
FNG01Fun and Games42 Galaxy RoadLondonNULLN16 6PSEngland
FRB01Furball Inc.1000 5th AvenueNew YorkNY11111USA
JTS01Jouets et ours1 Rue AmusementParisNULL45678France

Table: products

prod_idvend_idprod_nameprod_priceprod_desc
BNBG01DLL01Fish bean bag toy3.49Fish bean bag toy, complete with bean bag worms with which to feed it
BNBG02DLL01Bird bean bag toy3.49Bird bean bag toy, eggs are not included
BNBG03NULLRabbit bean bag toy3.49Rabbit bean bag toy, comes with bean bag carrots
BR01BRS018 inch teddy bear5.998 inch teddy bear, comes with cap and jacket
BR02NULL12 inch teddy bear8.9912 inch teddy bear, comes with cap and jacket
BR03BRS0118 inch teddy bear11.9918 inch teddy bear, comes with cap and jacket
RGAN01DLL01Raggedy Ann4.9918 inch Raggedy Ann doll
RYL01FNG01King doll9.4912 inch king doll with royal garments and crown
RYL02FNG01Queen doll9.4912 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_idprod_namevend_name
BNBG01Fish bean bag toyDoll House Inc.
BNBG02Bird bean bag toyDoll House Inc.
BR018 inch teddy bearBears R Us
BR0318 inch teddy bearBears R Us
RGAN01Raggedy AnnDoll House Inc.
RYL01King dollFun and Games
RYL02Queen dollFun 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_idprod_namevend_namevend_country
BNBG01Fish bean bag toyDoll House Inc.USA
BNBG02Bird bean bag toyDoll House Inc.USA
BR018 inch teddy bearBears R UsUSA
BR0318 inch teddy bearBears R UsUSA
RGAN01Raggedy AnnDoll 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_idprod_namevend_nameprod_price
BNBG01Fish bean bag toyDoll House Inc.3.49
BNBG02Bird bean bag toyDoll House Inc.3.49
RGAN01Raggedy AnnDoll 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_idvend_idvend_city
BR01BRS01Bear Town
BR03BRS01Bear Town
BNBG01DLL01Dollsville
BNBG02DLL01Dollsville
RGAN01DLL01Dollsville
RYL01FNG01London
RYL02FNG01London

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_nametotal_amount
Doll House Inc.11.97
Bears R Us17.98
Fun and Games18.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_nametotal_amount_GST_5
Doll House Inc.12.5685
Bears R Us18.8790
Fun and Games19.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_idvend_idprod_namevend_nameprod_price
BR01BRS018 inch teddy bearBears R Us5.99
BR03BRS0118 inch teddy bearBears R Us11.99
RGAN01DLL01Raggedy AnnDoll House Inc.4.99
RYL01FNG01King dollFun and Games9.49
RYL02FNG01Queen dollFun and Games9.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.