-- Create a database named mydb
CREATE DATABASE mydb;
-- Output:
-- Query OK, 1 row affected (0.01 sec)
-- Use the database mydb
USE mydb;
-- Output:
-- Database changed
-- Create the STOCK table with sample data
CREATE TABLE STOCK (
ITEMNO CHAR(3),
ITEMNAME VARCHAR(20),
BRAND VARCHAR(20),
QTY INT,
UNITPRICE DECIMAL(5, 2),
STOCKDATE DATE
);
-- Insert data into the STOCK table
INSERT INTO STOCK (ITEMNO, ITEMNAME, BRAND, QTY, UNITPRICE, STOCKDATE)
VALUES
('S05', 'Ball Pen 0.5', 'Luxor', 100, 16.00, '2022-03-31'),
('S03', 'Ball Pen 0.25', 'Reynolds', 150, 20.50, '2023-01-01'),
('S02', 'Gel Pen Premium', 'Luxor', 125, 14.42, '2023-02-14'),
('S06', 'Gel Pen Classic', 'Pentel', 200, 22.75, '2022-01-09'),
('S01', 'Eraser Small', NULL, 210, NULL, NULL),
('S04', 'Eraser Big', 'Apsara', 60, 10.00, '2020-12-12'),
('S09', 'Sharpener Classic', 'Apsara', 160, 8.00, '2021-01-23');
-- Output:
-- Query OK, 7 rows affected (0.01 sec)
-- Ques 1: Write SQL command for (i) to (xv) based on the table STOCK shown below:
-- (i) Display entire contents of the table
SELECT * FROM STOCK;
-- Output: (All rows will be displayed)
-- | ITEMNO | ITEMNAME | BRAND | QTY | UNITPRICE | STOCKDATE |
-- |--------|----------------|----------|-----|-----------|------------|
-- | S05 | Ball Pen 0.5 | Luxor | 100 | 16.00 | 2022-03-31 |
-- | S03 | Ball Pen 0.25 | Reynolds | 150 | 20.50 | 2023-01-01 |
-- | S02 | Gel Pen Premium| Luxor | 125 | 14.42 | 2023-02-14 |
-- | S06 | Gel Pen Classic| Pentel | 200 | 22.75 | 2022-01-09 |
-- | S01 | Eraser Small | NULL | 210 | NULL | NULL |
-- | S04 | Eraser Big | Apsara | 60 | 10.00 | 2020-12-12 |
-- | S09 | Sharpener Classic | Apsara | 160 | 8.00 | 2021-01-23 |
-- (ii) Display details of all the Items in ascending order of StockDate
SELECT * FROM STOCK
ORDER BY STOCKDATE ASC;
-- Output:
-- Items will be displayed sorted by STOCKDATE in ascending order
-- (iii) Display details of Items whose quantity (Qty) is greater than 100
SELECT * FROM STOCK
WHERE QTY > 100;
-- Output:
-- | ITEMNO | ITEMNAME | BRAND | QTY | UNITPRICE | STOCKDATE |
-- |--------|----------------|----------|-----|-----------|------------|
-- | S03 | Ball Pen 0.25 | Reynolds | 150 | 20.50 | 2023-01-01 |
-- | S02 | Gel Pen Premium| Luxor | 125 | 14.42 | 2023-02-14 |
-- | S06 | Gel Pen Classic| Pentel | 200 | 22.75 | 2022-01-09 |
-- | S01 | Eraser Small | NULL | 210 | NULL | NULL |
-- | S09 | Sharpener Classic | Apsara | 160 | 8.00 | 2021-01-23 |
-- (iv) Display item number, name and New Unit price as UnitPrice + 5 with proper heading
SELECT ITEMNO, ITEMNAME, (UNITPRICE + 5) AS "New Unit Price"
FROM STOCK;
-- Output:
-- | ITEMNO | ITEMNAME | New Unit Price |
-- |--------|----------------|----------------|
-- | S05 | Ball Pen 0.5 | 21.00 |
-- | S03 | Ball Pen 0.25 | 25.50 |
-- | S02 | Gel Pen Premium| 19.42 |
-- | S06 | Gel Pen Classic| 27.75 |
-- | S01 | Eraser Small | NULL |
-- | S04 | Eraser Big | 15.00 |
-- | S09 | Sharpener Classic | 13.00 |
-- (v) Display item name and “Total Value” as Qty * UnitPrice of all items
SELECT ITEMNAME, (QTY * UNITPRICE) AS "Total Value"
FROM STOCK;
-- Output:
-- | ITEMNAME | Total Value |
-- |------------------|-------------|
-- | Ball Pen 0.5 | 1600.00 |
-- | Ball Pen 0.25 | 3075.00 |
-- | Gel Pen Premium | 1802.50 |
-- | Gel Pen Classic | 4550.00 |
-- | Eraser Small | NULL |
-- | Eraser Big | 600.00 |
-- | Sharpener Classic| 1280.00 |
-- (vi) Display item number, name, and quantity of ‘Pen’ items
SELECT ITEMNO, ITEMNAME, QTY
FROM STOCK
WHERE ITEMNAME LIKE '%Pen%';
-- Output:
-- | ITEMNO | ITEMNAME | QTY |
-- |--------|-----------------|-----|
-- | S05 | Ball Pen 0.5 | 100 |
-- | S03 | Ball Pen 0.25 | 150 |
-- | S02 | Gel Pen Premium | 125 |
-- | S06 | Gel Pen Classic | 200 |
-- (vii) Display details of Items whose price is in the range Rs.15/- to Rs.20/-
SELECT * FROM STOCK
WHERE UNITPRICE BETWEEN 15 AND 20;
-- Output:
-- | ITEMNO | ITEMNAME | BRAND | QTY | UNITPRICE | STOCKDATE |
-- |--------|---------------|---------|-----|-----------|------------|
-- | S05 | Ball Pen 0.5 | Luxor | 100 | 16.00 | 2022-03-31 |
-- | S03 | Ball Pen 0.25 | Reynolds| 150 | 20.50 | 2023-01-01 |
-- (viii) Display item number and name of items with ItemNo S01 and S03
SELECT ITEMNO, ITEMNAME
FROM STOCK
WHERE ITEMNO IN ('S01', 'S03');
-- Output:
-- | ITEMNO | ITEMNAME |
-- |--------|----------------|
-- | S01 | Eraser Small |
-- | S03 | Ball Pen 0.25 |
-- (ix) Display details of items whose STOCKDATE is unknown (missing)
SELECT * FROM STOCK
WHERE STOCKDATE IS NULL;
-- Output:
-- | ITEMNO | ITEMNAME | BRAND | QTY | UNITPRICE | STOCKDATE |
-- |--------|--------------|-------|-----|-----------|-----------|
-- | S01 | Eraser Small | NULL | 210 | NULL | NULL |
-- (x) Display details of items stocked in the year 2022
SELECT * FROM STOCK
WHERE YEAR(STOCKDATE) = 2022;
-- Output:
-- | ITEMNO | ITEMNAME | BRAND | QTY | UNITPRICE | STOCKDATE |
-- |--------|----------------|----------|-----|-----------|------------|
-- | S05 | Ball Pen 0.5 | Luxor | 100 | 16.00 | 2022-03-31 |
-- | S06 | Gel Pen Classic| Pentel | 200 | 22.75 | 2022-01-09 |
-- (xi) Display different brand names without repetition
SELECT DISTINCT BRAND
FROM STOCK;
-- Output:
-- | BRAND |
-- |----------|
-- | Luxor |
-- | Reynolds |
-- | Pentel |
-- | NULL |
-- | Apsara |
-- (xii) Display item details sorted on unitprice
SELECT * FROM STOCK
ORDER BY UNITPRICE ASC;
-- Output:
-- Items will be displayed sorted by UNITPRICE in ascending order
-- (xiii) Display items stocked before 2023
SELECT * FROM STOCK
WHERE STOCKDATE < '2023-01-01';
-- Output:
-- | ITEMNO | ITEMNAME | BRAND | QTY | UNITPRICE | STOCKDATE |
-- |--------|----------------|----------|-----|-----------|------------|
-- | S05 | Ball Pen 0.5 | Luxor | 100 | 16.00 | 2022-03-31 |
-- | S06 | Gel Pen Classic| Pentel | 200 | 22.75 | 2022-01-09 |
-- | S04 | Eraser Big | Apsara | 60 | 10.00 | 2020-12-12 |
-- | S09 | Sharpener Classic| Apsara | 160 | 8.00 | 2021-01-23 |
-- (xiv) Display brand, name, and price of items costlier than Rs.20/-
SELECT BRAND, ITEMNAME, UNITPRICE
FROM STOCK
WHERE UNITPRICE > 20;
-- Output:
-- | BRAND | ITEMNAME | UNITPRICE |
-- |----------|-----------------|-----------|
-- | Reynolds | Ball Pen 0.25 | 20.50 |
-- | Pentel | Gel Pen Classic | 22.75 |
-- (xv) Display items whose quantity is below 50 or above 200
SELECT * FROM STOCK
WHERE QTY < 50 OR QTY > 200;
-- Output:
-- | ITEMNO | ITEMNAME | BRAND | QTY | UNITPRICE | STOCKDATE |
-- |--------|---------------|-------|-----|-----------|-----------|
-- | S01 | Eraser Small | NULL | 210 | NULL | NULL |
-- | S04 | Eraser Big | Apsara| 60 | 10.00 | 2020-12-12 |