ByteBoard Notes Notes

SQL Set 6: join ITEM and SUPPLIER with GROUP BY and ORDER BY

Computer Science XII · Experiment 24 of 29 · SQL

12-CSSQL query sets
Expt. No.24
Page No.48

ITEM and SUPPLIER mixed set

Aim

To write board-style SQL queries on two tables, ITEM and SUPPLIER, that combine an equi-join with GROUP BY, ORDER BY and UPDATE.

Concepts used

Queries

USE shop;-- Item names with their supplier namesSELECT iname, sname FROM item, supplier WHERE item.scode = supplier.scode;-- Items supplied from MumbaiSELECT iname, price, sname FROM item, supplierWHERE item.scode = supplier.scode AND city = 'Mumbai';-- Number of items from each supplierSELECT sname, COUNT(*) FROM item, supplierWHERE item.scode = supplier.scode GROUP BY sname;-- Stock value (qty x price) for each supplier, highest firstSELECT sname, SUM(qty * price) AS stock_value FROM item, supplierWHERE item.scode = supplier.scode GROUP BY sname ORDER BY stock_value DESC;-- Raise the price of every item from S02 by 5UPDATE item SET price = price + 5 WHERE scode = 'S02';SELECT iname, price FROM item WHERE scode = 'S02';

A marked line continues the line above it. In your file, write it on the same line.

Tables used

Table: item
+-------+------------+-------+-----+-------+
| icode | iname      | scode | qty | price |
+-------+------------+-------+-----+-------+
|     1 | Notebook   | S01   | 120 |    40 |
|     2 | Pen        | S02   | 300 |    15 |
|     3 | Stapler    | S01   |  25 |   150 |
|     4 | Marker     | S02   |  80 |    35 |
|     5 | Calculator | S03   |  10 |   650 |
|     6 | Eraser     | S02   | 200 |     5 |
+-------+------------+-------+-----+-------+
Table: supplier
+-------+----------------+---------+
| scode | sname          | city    |
+-------+----------------+---------+
| S01   | Sharma Traders | Delhi   |
| S02   | Mehta Stores   | Mumbai  |
| S03   | Rao Agencies   | Chennai |
+-------+----------------+---------+

Output

mysql> USE shop; Database changed
mysql> SELECT iname, sname FROM item, supplier WHERE item.scode = supplier.scode; +------------+----------------+ | iname | sname | +------------+----------------+ | Notebook | Sharma Traders | | Pen | Mehta Stores | | Stapler | Sharma Traders | | Marker | Mehta Stores | | Calculator | Rao Agencies | | Eraser | Mehta Stores | +------------+----------------+ 6 rows in set
mysql> SELECT iname, price, sname FROM item, supplier -> WHERE item.scode = supplier.scode AND city = 'Mumbai'; +--------+-------+--------------+ | iname | price | sname | +--------+-------+--------------+ | Pen | 15 | Mehta Stores | | Marker | 35 | Mehta Stores | | Eraser | 5 | Mehta Stores | +--------+-------+--------------+ 3 rows in set
mysql> SELECT sname, COUNT(*) FROM item, supplier -> WHERE item.scode = supplier.scode GROUP BY sname; +----------------+----------+ | sname | COUNT(*) | +----------------+----------+ | Sharma Traders | 2 | | Mehta Stores | 3 | | Rao Agencies | 1 | +----------------+----------+ 3 rows in set
mysql> SELECT sname, SUM(qty * price) AS stock_value FROM item, supplier -> WHERE item.scode = supplier.scode GROUP BY sname ORDER BY stock_value DESC; +----------------+-------------+ | sname | stock_value | +----------------+-------------+ | Sharma Traders | 8550 | | Mehta Stores | 8300 | | Rao Agencies | 6500 | +----------------+-------------+ 3 rows in set
mysql> UPDATE item SET price = price + 5 WHERE scode = 'S02'; Query OK, 3 rows affected
mysql> SELECT iname, price FROM item WHERE scode = 'S02'; +--------+-------+ | iname | price | +--------+-------+ | Pen | 20 | | Marker | 40 | | Eraser | 10 | +--------+-------+ 3 rows in set

✓ Printed by running this query set, not typed by hand.

Result

The equi-join matched each item with its supplier, and Mehta Stores (Mumbai) supplies 3 items. Sharma Traders had the highest stock value (8550), then Mehta Stores (8300) and Rao Agencies (6500). UPDATE raised the price of the 3 items from S02 by 5.

Viva voce: what the examiner may ask

01Why is item.scode = supplier.scode needed?

It is the join condition; it keeps only the rows where the item's supplier code matches the supplier.

02Can an alias like stock_value be used in ORDER BY?

Yes. MySQL allows a column alias in ORDER BY.

03What does SUM(qty * price) add up?

The value qty × price for every row in the group.

Variations for the lab test

  1. Display suppliers who supply more than one item.
  2. Display items whose price is between 20 and 200, with the supplier's city.
  3. Display the costliest item of each supplier.
! Common mistake Writing WHERE scode = 'S02' in a join query that lists both tables gives an "ambiguous column" error; write item.scode.
Back to the 12-CS index