9 Gtu Query Algebra Solution

Embed Size (px)

Citation preview

  • 8/18/2019 9 Gtu Query Algebra Solution

    1/4

    Darshan Institute of Engineering & Technology

    130703‐Database Management System (DBMS)

    GTU Q. Papers - Query 

    Firoz Sherasiya Page 1 

    We have following relations: 

    Supplier(S#,sname,status,city) 

    Parts(P#,pname,color,weight,city) 

    SP(S#,P#,quantity) 

    Answer the following queries in SQL. 

    1)  Find name of  supplier for city = ‘Delhi’. 

    Select sname from supplier 

    Where city=’delhi’; 

    2) 

    Find suppliers whose name start with ‘AB’ 

    Select * from supplier 

    Where sname like ‘AB%’; 

    3) 

    Find all suppliers whose status is 10, 20 or 30. 

    Select * from supplier 

    Where status IN (10, 20, 30); 

    4) 

    Find total number of  city of  all suppliers. 

    Select count(city) from supplier; 

    5) 

    Find s# of  supplier who supplies ‘red’ part. 

    Select sp.s# from sp 

    Inner  join supplier s on s.s#=sp.s# 

    Inner  join parts p on p.p#=sp.p# 

    Where p.color=’red’; 

    6)  Count number of  supplier who supplies ‘red’ part. 

    Select count(*) from sp 

    Inner  join supplier s on s.s#=sp.s# 

    Inner  join parts on p.p#=sp.p# 

    Where p.color=’red’; 

    7)  Sort the supplier table by sname. 

    Select * from supplier 

    Order by sname; 

    8) 

    Delete records in supplier table whose status is 40. 

    Delete from supplier where status=40; 

    9)  Add one field in supplier table. 

    Alter table supplier 

    Add new_column varchar2(20); 

    10) Find name of  parts whose color is ‘red’ 

    Select pname from parts 

    Where color=’red’; 

    11) 

    Find parts name whose weight less than 10 kg. 

    Select pname from parts where weight

  • 8/18/2019 9 Gtu Query Algebra Solution

    2/4

    Darshan Institute of Engineering & Technology

    130703‐Database Management System (DBMS)

    GTU Q. Papers - Query 

    Firoz Sherasiya Page 2 

    15) Find name of  supplier who supply maximum parts. 

    Select sname, max(count(sp.p#)) from supplier 

    Inner  join sp on sp.s#=supplier.s#; 

    16) 

    Sort the parts table by pname. 

    Select * from parts 

    Order by pname; 17)

     

    Delete records in parts table whose color is ‘blue’. 

    Delete from parts 

    Where color=’blue’ 

    18) 

    Drop one field in parts table. 

    Alter table parts 

    Drop column city; 

    Consider following schema and write SQL for given statements. 

    student( rollno, name, branch) 

    exam(rollno, subject_code, obtained_marks , paper_code) papers(paper_code, paper_satter_name, university) 

    1) 

    Display name of  student who got first class in subject ‘130703’. 

    Select s.name from student s 

    Inner  join exam e on e.rollno=s.rollno 

    Where e.obtained_marks>=60 and 

    e.paper_code=’130703’; 

    2)  Display name of  all student with their total mark. 

    Select sum(e.obtained_marks), s.name from student s 

    Inner  join exam e on e.rollno=s.rollno 

    Group by s.name; 

    3) 

    Display list number of  student in each university. 

    Select count(s.rollno), p.university from student s 

    Inner  join exam e on e.rollno=s.rollno 

    Inner  join paper p on p.papercode=e.papercode 

    Group by p.university; 

    4) 

    Display list of  student who has not given any exam. 

    Select s.name from student s 

    Where s.rollno NOT IN 

    (select distinct e.rollno from exam e); 

    Consider following schema and represent given statements in relation algebra form. 

    Branch(branch_name,branch_city) 

    Account(branch_name, acc_no, balance) 

    Depositor(Customer_name, acc_no) 

    1) 

    Find out list of  customer who have account at ‘abc’ branch. 

    ∏ customer_name (σbranch_name=”abc” (depositor  account) ) 

  • 8/18/2019 9 Gtu Query Algebra Solution

    3/4

    Darshan Institute of Engineering & Technology

    130703‐Database Management System (DBMS)

    GTU Q. Papers - Query 

    Firoz Sherasiya Page 3 

    2)  Find out all customers who have account in ‘Ahmedabad’ city and balance is greater than 10,000. 

    ∏ customer_name (σbranch_city=”ahmedabad” ^ balance>10000 (depositor  account) 

    3)  find out list of  all branch name with their maximum balance. 

    ∏branch_name G max (balance) (branch  account) 

    Consider following schema and write SQL for given statements. 

    Student(Rollno, Name, Age,Sex,City)  Student_marks(Rollno,Sub1,Sub2,Sub3,Total,Average) 

    Write query to 

    1) 

    Calculate and store total and average marks from Sub1, Sub2 & Sub3. 

    Update student_marks 

    set total=sub1+sub2+sub3, 

    average=(sub1+sub2+sub3)/3; 

    2) 

    Display name of  students who got more than 60 marks in subject Sub1. 

    Select name from student where rollno IN (select rollno from student_marks where sub1>60); 

    3) 

    Display name of  students with their total and average marks. 

    Select a.name, b.total, b.average from student a, student_marks b 

    where a.rollno=b.rollno; 

    4)  Display name of  students who got equal marks in subject Sub2 

    Select a.name from student a where a.rollno IN 

    (select b.rollno from student_marks b where b.rollno IN 

    (select c.rollno from student_marks c where b.sub2=c.sub2)); 

    Write down the query for the following table where primary keys are underlined. 

    Person(ss#, name, address)  Car(license, year, model) Accident(date, driver, damage‐amount)  Owns(ss#, license)  Log(license, date, driver) 

    1)  Find the total number of  people whose cars were involved in accidents in 2009. 

    Select count(ss#) from person Where ss# IN 

    (select ss# from owns where licence IN 

    (select licence from log where driver IN 

    (select driver from accident 

    Where date > ’01‐ jan‐09’ and ’31‐dec‐09’))); 

    OR 

    Select count(ss#) from person 

    Inner  join owns on person.ss#=owns.ss# 

    Inner  join log on owns.licence=log.licence 

    Inner  join accident on accident. 

    Where accident.date > ’01‐ jan‐09’ and accident.date 

  • 8/18/2019 9 Gtu Query Algebra Solution

    4/4

    Darshan Institute of Engineering & Technology

    130703‐Database Management System (DBMS)

    GTU Q. Papers - Query 

    Firoz Sherasiya Page 4 

    OR 

    Select count(accident.date) from accident 

    Inner  join log on log.date=accident.date and log.driver=accident.driver 

    Inner  join owns on own.licence=log.licence 

    Inner  join person on person.ss#=own.ss# 

    where  person.name = ‘s.sudarshan’; 

    3)  Add a new customer to the database. 

    Insert into person (ss#, name, address) values (11, ‘ramesh’, ‘rajkot’); 

    Insert into owns (ss#, license) values (11, ‘L11’); 

    Insert into car (license, year, model) values (‘L11’, 2011, ‘esteem’); 

    Insert into log (license, date, driver) values (‘L11’, null, ‘mohan’); 

    4)  Add a new accident recorded for the Santro belonging to “KORTH” 

    Insert into accident (date, driver, damage‐amount) values (’01‐oct‐11’,’magan’,10000); 

    Insert into log (license, date, driver) values (‘L08’,’01‐oct‐11’,’magan’); 

    Insert into car (license, year, model) values (’L08’,’2007’,’alto’); 

    Consider the employee data. Give an expression in SQL for the following query : Employee(employee‐name, street, city)  Works(employee‐name, company‐name, salary) 

    Company(company‐name, city)  Manages(employee‐name, manager‐name) 

    1) 

    Find the name of  all employees who work for State Bank. 

    Select employee‐name from works 

    Where company‐name=’state bank’; 

    2) 

    Find the names and cities of  residence of  all employees who work for State Bank. 

    Select employee‐name, city from employee 

    Where employee‐name IN 

    (select employee‐name from works 

    Where company‐name=’state bank’); OR 

    Select e.employee‐name, e.city from employee e 

    Inner  join works w on w.employee‐name= e.employee‐name 

    Where w.company‐name=’state bank’; 

    3)  Find all employee in the database who do not work for State Bank. 

    Select employee‐name from work 

    Where company‐name != ‘state bank’; 

    4)  Find all employee in the database who earn more than every employee of  UCO Bank. 

    Select employee‐name from works 

    Where salary  > (select max (salary) from works 

    Where company‐name =’uco bank’);