diff --git a/Assignment2_codes_revised.sql b/Assignment2_codes_revised.sql new file mode 100644 index 0000000000000000000000000000000000000000..cb3ba56c9624c4b997e8e229a3c5bbaf6e508636 --- /dev/null +++ b/Assignment2_codes_revised.sql @@ -0,0 +1,88 @@ +use omila405; +#Q1: +select * from jbemployee; +#Q2: +select name from jbdept order by name asc ; +#Q3: +SELECT * FROM jbitem where qoh=0; +#Q4: +select * from jbemployee where salary between 9000 and 10000; +#Q5: +select name,startyear-birthyear AS 'Age'from jbemployee; + +#Q6: +select * from jbemployee where name regexp 'son,'; +#Q7: +select * from jbitem where supplier = (select id from jbsupplier where name='Fisher-Price'); +#Q8: +select jbitem.*, jbsupplier.name as supplierName from jbitem +join jbsupplier on jbitem.supplier=jbsupplier.id +where jbsupplier.name="Fisher-Price"; +#Q9: +select city.id,city.name,sup.city from jbsupplier sup left join jbcity city on city.id=sup.city; +#Q10: +select name,color from jbparts where weight >(select weight from jbparts where name='card reader'); +#Q11: +select p1.name,p1.color,p1.weight from jbparts p1 join jbparts p2 on p2.name='card reader' where p1.weight>p2.weight; +#Q12: +select AVG(weight) from jbparts where color='black'; + +#13 update +select jbsupplier.name, jbcity.state ,jbparts.name as Parts_name, SUM(jbsupply.part) as total_parts, SUM(jbsupply.quan) as total_quantity,(SUM(jbsupply.part)*SUM(jbsupply.quan)) as Weight from jbsupplier +join jbcity on jbcity.id=jbsupplier.city +join jbsupply on jbsupply.supplier=jbsupplier.id +join jbparts on jbsupply.part=jbparts.id +where jbcity.state='Mass' +GROUP BY jbsupplier.id; + + + + + + +#Q14: +create table jb ( + id int primary key, + name varchar(20), + dept int , + price int, + qoh int, + supplier int, + CONSTRAINT FK_jb_supplier + FOREIGN KEY (supplier) REFERENCES jbsupplier(id), + CONSTRAINT FK_jb_dept + FOREIGN KEY (dept) REFERENCES jbdept(id)) + as select * from jbitem where price < (select AVG(price) from jbitem); + + +#Q15: +create view item_copy_view as + select * from jbitem where price < (select AVG(price) from jbitem) ; +#Q16:view is an virtial table and saves in views. It doesn't have rows(tuples) and columns(attributes) like tables. +#View depends on tables so it is dynamic, table is independant and static. +#Refrence:https://pediaa.com + +#Q17: +create view view2 as + select jbdebit.id,(jbsale.item*jbsale.quantity) as total_cost from jbdebit left join jbsale on jbdebit.id=jbsale.debit; + +#Q18: +select debit , (jbsale.quantity*jbitem.price) as total from jbsale join jbitem on jbsale.item=jbitem.id; +#Q19: +SET SQL_SAFE_UPDATES = 0; +SET FOREIGN_KEY_CHECKS=0; -- to disable them + DELETE jbsupplier +from jbsupplier +INNER JOIN jbcity on jbsupplier.city = jbcity.id +where jbcity.name="Los Angeles"; +SET FOREIGN_KEY_CHECKS=1; -- to re-enable them +SET SQL_SAFE_UPDATES = 1; + +#Q20: +CREATE VIEW jbsale_supply(supplier, item, quantity) AS +SELECT jbsupplier.name, jbitem.name, jbsale.quantity + FROM jbsupplier left join jbitem on jbsupplier.id=jbitem.supplier +join jbsale on jbitem.id=jbsale.item; +SELECT supplier, sum(quantity) AS sum FROM jbsale_supply +GROUP BY supplier; +