You are on page 1of 3

SQL Questions related to DDL

Create a table item_master with following structures Item_id int Item_type char(1) Item_name varchar(50) Stock_in_hand Integer(3) Unit_price float(3,2) a) b) c) d) Make item_id as primary key Make item_type and item_name as unique key Stock in hand should not have null value Create separate .sql file for this table

Create another table called customer_master Customer_id int Customer_name varchar(50) a) Make customer_id as primary key b) Make customer name unique c) Create .sql file for this table. Create another table called sales_log Sales_log_id int Item_id int Customer_id int Sold_qty Integer(3) Sold_date date a) Make Sales_log_id unique b) Make item_id not nullable and foreign key referencing to item_id of item_master table. c) Make customer_id not nullable and foreign key referencing to customer_id of customer_master table. d) Sold_qty should be not be nullable and cannot have value 0(Zero). e) Sold date should not be null be greater than equal to today s date. #1 Create 4 meaningful records into item_master Item_id Item_type Item_name 1 N Note Book 2 P Pen 3 B Box 4 C Crayons Customer_id 1 2 3 4

Stock_in_hand 250 300 230 400

Unit_price 30.00 30.00 50.00 30.00

Customer_name AEIT NIIT APTEC CASRT

#3 Create 10 records with these data Sales_log_id Item_id 1 1 2 1 3 1 4 2 5 3 6 2 7 4 8 3 9 4 10 3 11 2 12 4 13 2 14 3 15 1 16 2 17 3 18 2 19 4 20 2

Customer_id 1 2 3 1 4 4 2 3 4 3 3 3 2 4 4 2 2 3 3 2

Sold_qty 20 30 30 10 20 10 20 10 20 10 20 10 20 20 10 20 21 20 30 20

Sold_date 2011-09-12 2011-09-16 2011-09-12 2011-08-09 2011-08-12 2011-07-09 2011-07-09 2011-06-09 2011-08-09 2011-06-11 2011-09-11 2011-08-11 2011-06-11 2011-07-28 2011-08-20 2011-04-20 2011-05-05 2011-08-08 2011-09-09 2011-08-08

#1 Write a SQL statement to print the total number of items sold by item types to each customer customer_id ---------------1 2 3 4 1 2 3 4 customer_name ---------------------AEIT NIIT APTEC CASRT AEIT NIIT APTEC CASRT item_type ------------C C C C N N N N item_name ---------------Crayon Crayon Crayon Crayon Note Book Note Book Note Book Note Book total_items_sold ---------------------30 40 100 20 30 40 100 20

#2 Write a SQL statement that would print items in hand per item_type Item_id Item_name stock_in_hand ------------------------------------------1 Note Book 20 2 Pen 40 3 Crayon 100 4 Box 20 #3 Write a SQL statement to find out total number of items sold in ascending order. Item_id ---------1 2 3 4 Item_name --------------Note Book Pen Crayon Box stock_in_hand -------------------100 80 60 40

#4 Write a SQL statement to find out item that has been sold maximum Item_id Item_name stock_in_hand ------------------------------------------1 Note Book 100 #5 Write a SQL statement to find out customer who has bough maximum number of items by item customer_id customer_name item_name qty_sold -------------------------------------------------------------------1 AEIT Pen 100 2 NIIT Note Book 80 3 APTEC BOX 40 4 CASRT Crayon 30 #6 Write a SQL to print sales_log by customer_id, item_id, item_sold and date in ascending ordr. #7 Write a SQL to find out in which month most crayons were sold. #8 Write a SQL statement to find out avg sales per item per customer. #9 Write a SQL to find out max item sold in a day. #10 Write a SQL to find out in which day an item was not sold.

#11 Example of outer join SELECT cm.customer_name, im.item_name, sum(sl.sold_qty) FROM sales_log sl LEFT JOIN item_master as im ON (sl.item_id = im.item_id) INNER JOIN customer_master as cm ON (sl.customer_id = cm.customer_id) GROUP BY cm.customer_id, im.item_id ORDER BY cm.customer_id, im.item_id; #12 Also, please practice all the SQL functions from this website. This is very important for your tables. http://dev.mysql.com/doc/refman/5.0/en/functions.html #13 Also practice the SQL syntaxes related "minus", "union", distinct".

You might also like