Structure Query Language - .Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce

  • View
    364

  • Download
    1

Embed Size (px)

Text of Structure Query Language - .Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce

  • 1

    GAPS

    ASHOK VIHAR

    Structure Query LanguageDatabase: A database is an organised collection of data.

    DBMS:A collection of programs that enables you to store, modify and extract information from a database.

    Relational Database: A database in which the data is stored in the form of relations(also called tables) is called a Relational Database.

    RDBMS: A DBMS used to manage Relational Databases is called an RDBMS(Relational DabaBase Management System). Some popular RDBMS software available are Oracle, MySQL, Sybase, Ingress.

    Relation: A relation is two dimension table.

    Attributes. The coloumn of the table are called attributes

    Tuples: The rows of the table are called tuples.

    Degree: The number of attributes in a relation is called the degree of the relation.

    Cardinality: The number of tuples(rows) in the relation is called its cardinality.

    Primary Key: The group of one or more coloumn used to uniquely identify each row of a relation is called its Primary Key.

    Candidate Key: A coloumn or a group of columns which can be used as the primary key of relation is called a candidate key.

    Alternate key: A candidate key of table which is not made its primary key is called its Alternate key.

    Foreign Key: A foreign key is a column in a table where that column is a primary key of another table.

    Categories of SQL:

    (i) DDL(Data Definition Language): This is the category of SQL commands. All the commands which are used to create, destory or resturcture databases and tables come under this category. Examples of DDL commands are -CREATE, DROP, ALTER.

    (ii) DML(Data Manipulation Language): This is a category of SQL Command. All the commands which are used to manipulae data within tables comes under this category. Examples: Insert,Update,Delete.

    (iii)DCL(Data Control Language): All the commands which are used to control the access to databases and tables fall under this category. Examples -Grant, Revoke.

    Benefits of using a DBMS are: Redundancy can be controlled, Inconsistence can be avoided, Data can be shared, Security restrictions can be applied.

    Q1Write SQL commands for (a) to (f) and write the outputs for (g) on the basis of table HOSPITAL. [D 1998]

    Table : HOSPITAL

    No Name Age Department Charges Sex1 Arpit 62 21/01/98 300 M2 Zarina 22 12/12/97 250 F3 Kareem 32 19/02/98 200 M4 Arun 12 11/01/98 300 M

    SQL

  • 2

    GAPS

    ASHOK VIHAR

    5 Zubin 30 12/01/98 250 M6 Ketika 16 24/02/98 250 F7 Ankita 29 20/02/98 800 F8 Zareen 45 22/02/98 300 F9 Kush 19 13/01/98 800 M10 Shilpa 23 21/02/98 400 F

    (a) To select all the information of patients of cardiology department.(b) To list the names of female patients who are in ENT department.(c) To list names of all patients with their date of admission in ascending order.(d) To display patients name, charges, age for only female patients.(e) To count the number of patient with Age < 30.(f) To insert a new row in the HOSPITAL table with the following data:11, Aftab, 24, Surgery. {25/02/98}, 300, M(g) Give the output of following SQL statements:(i) SELECT COUNT(DISTINCT charges) FROM hospital(ii) SELECT MIN(age) FROM hospital WHERE sex = F(iii) SELECT SUM(charges) FROM hospital WHERE department = ENT:(iv) SELECT AVG(charges) FROM hospital WHERE dateofadm 40.c. To display list of all employees whose salary >= 30000 .d. To display the employee names in ascending order of age.e. To display all information of all employees those name starts with A.f. To display name and age of all male employees those salary is between 20000 and 25000.

    Q3 Write the SQL commands for the questions from (a) to (j) on the basis of table FAMILY.No Name FemalMe

    mbersMaleMembers Income Occupation

    1 Mishra 3 2 67000 Service2 Gupta 4 1 150000 Business3 Khan 6 3 48000 Mixed4 Chaddha 2 2 125000 Business5 Yadav 7 2 Mixed

    (a) To select income of family whose occupation is mixed.(b) To show all information where female members are more than 3.(c) To list the name of family with income in ascending order.(d) To display family name , male members and occupation those income greater than 50000.(e) To count the mumber of family whose income is less than 110000(f) To insert a new record in the family table with the following data

    SQL

  • 3

    GAPS

    ASHOK VIHAR

    9,D souza, 2,1,115000,Service(g)To show all information in descending order of name(h) To show all information whose name end with C(i) To list the name of family where male members are less than 2(j) To list the information whose income is null

    Q4 Write the SQL command for (a) to (j) Table : StudentNo Name Age Department Fees1 Pankaj 24 Computer 3002 Shalini 21 History 2003 Sanjay 22 Hindi 3004 Sudha 25 History 2505 Rakesh 20 Hindi 210

    (a) To Show all information about the student of History Department.(b) To list the names of students who are in Hindi department.(c) To list name of all students with their age in ascending order(d) To display students name, charges, age of all students.(e) To display all the information of students in descending order of fees.(f) Show all information those name ends with S.(g) To count the no of students those age

  • 4

    GAPS

    ASHOK VIHAR

    No. Name Price Supplier Stock11 Motherboard 7000 Intel 202 Keyboard 1000 TVSE 703 Mouse 500 Logitech 604 Soundcard 600 Samsung 505 Speaker 600 Samsung 256 Monitor 3000 Philips 227 CD ROM 2800 Creative 328 Printer 7900 HP 10

    (a) Display data for the entire item sorted by their name.(b) Display the name and price from the table item in descending order of their stock.(c) List all name and price wth price between 3000 and 7000.(d) Write the command to set the price field of all product to 1200 corresponding to Name=keyboard(e) Write the SQL command to delete rows with stock between 20 to 40.(f) To count the number of products with stock20;(iii)Select count(Distinct stock) from product.(iv) Select max(price) rom product.

    Q7 Write SQL Commands for the questions from (a) to (j) on the basis of table SUPPLIER.Table : SupplierSno Pname Sname Qty Price CityS1 Bread Britannia 150 8 DelhiS2 Cake Britanni 250 20 MumbaiS3 Coffee Nescafe 170 45 DelhiS4 Chocolate Amul 250 10 MumbaiS5 Sauce 300 36 Jaipur

    a. Display data for all products whose quantity is between 170 and 370.b. Display data for all products sorted by their quantity. c. Give Sname for that entire product whose name starts with C.d. To list Sname,Pname,Price for all the products whose quantity is

  • 5

    GAPS

    ASHOK VIHAR

    (i) To display the name of all activities with their Acodes in descending order.(ii) Show all the data of all activities those prizemoney is NULL (iii)To display the activity name and ACodes in descending order of ACode from the table Activity.(iv) To display the content of the Activity table whose ScheduleDate earliar than 01/01/2004 in ascending order of ParticipantNum.(v) To display all information of Activity whose Activity name starts with A.(vi)Show acode whose Participants num is 10(vii)Delete all records those Acode is less than 1003.(viii)Count the no of records of each participantsNum.(ix)Increae the prizemoney by 1000 of all activities(x)Show all information those Acode is 1001

    Q9Consider the following tables PREPAID. Write SQL commands for the statement (i) to (iv) and give out-puts for SQL queries (v) to (viii)[Compt 2005]

    Table : PREPAIDS.No. Cname Model Connection Activation_Date Validity

    (in days)Amount(paid in Rs)

    1 Sita Nokia Airtel 04/06/2004 365 33002 Ritesh Nokia Hutch 12/02/2005 60 30303 Reena Samsung Hutch 14/05/2003 365 30304 Meetali Nokia Indicom 24/09/2004 180 8005 Ramanju Samsung Idea 16/10/2004 180 9006 Anupam LG Hutch 18/01/2005 60 6007 Sai Ram Nokia Indicom 10/12/2004 180 8008 Deepa LG Reliance 08/03/2005 30 3009 Karan Sony Idea 30/01/2005 90 80010 Jayant Siemen Airtel 02/02/2005 60 560

    (i) To display Cname and Amout of customers having Samsung model and Hutch connection, arranged in descending order of Amount.(ii) To display different types of connection available.(iii) To display sum off validity for each type of connection grouping.(iv) to display cname,connection and date of expiry. Where date of expiry is date of activation (of connec-tion) + validity(number of days valid).(v) Select count(distinct Model) from PREPAID(vi) Select Max(Activation), Min(Activation) from PREPAID(vii)Select avg(Validity) from PREPAID where Model=LG(viii)Select Max(amount) from PREPAID where connection =IDEA

    Q10 Consider the following table: [compt 2004]Table:SchoolBus

    Rtno area_covered capacity noofstudents distance Transporter charges1 Vasant Kunj 100 120 10 Shivam Travel 1000002 Hauz Khas 80 80 10 Anand Travel 850003 Pitampura 60 55 30 Anand Travel 600004 Rohini 100 90 35 Shivam Travel 750005 Yamuna Vihar 50 60 45 Anand Travel 450006 Krishna Nagar 70 80 30 Yadav Co. 800007 Vasundhara 80 110 20 Yadav Co. 1000008 Paschim Vihar 100 40 20 Speed Travels 55000

    SQL

  • 6

    GAPS

    ASHOK VIHAR

    9 Saket 120 120 10 Speed Travels 10000010 Janak Puri 100 100 20 Kisan Tours 95000

    Write the SQL commands for the following:(i) To show all records students more than the capacity in order of RtNo.(ii)to show area_covered for buses covering more than 20 km but charges less than 80000.(iii) To show Transporter wise total no. of students traveling.(iv) To show Rtno, area_covered and average cost per student for all routes.(v) Add a new coloumn drivername in the school bus relation.(vi) Add a new record with the following data:(11,Moti Bagh,35,32,10,Kisan Tours,35000)(vii) Give the output(a) Select sum(distance) from schoolbus where transporter=Yadav Travels(b) Select min(noofstudents) from schoolbus(c) Select avg(charges) from schoolb