03 Data Base SQL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Page 1 of 11

Data Base SQL


Q. a) i) key. ii) iii) iv) v) vi) vii) brief. Define the following terms alternate key, primary key, candidate What are the different types of SQL statements? What are the advantages of DBMS? What is the difference between VIEW and TABLE? Differentiate between DELETE and DROP command. Differentiate between DDL and DML statements. What are the different types of constraints? Explain each one in

b) i) Study the following tables DOCTOR and SALARY and write SQL commands for the questions (i) to (iv) and give outputs for SQL queries (v) to (iv): Table: DOCTOR ID 101 104 107 114 109 105 117 111 130 NAMER John Smith Rahul Lara Mehul Jonson Lucy Becon Morphy DEPT ENT ORTHOPEDIC CARDIOLOGY SKIN MEDICINE ORGHOPEIC ENT Medicine ORTHOPEDIC SEX M M M F F M F F M EXPERIENCE 12 5 10 9 9 10 3 10 15

TABLE :SALARY ID 101 104 107 114 109 105 130 BASIC 12000 23000 32000 12000 42000 18900 21700 ALLOWANCE 1000 2300 4000 5200 1700 1690 2600 CONSULTATION 300 500 500 100 200 300 300

(a) Display NAME of all doctors who are in MEDICINE having more than 10 years experience from table DOCTOR. (b) Display the average salary of all doctors working in ENT department using the tables DOCTOR and SALARY. Salary=BASIC+ALLOWANCE. (c) Display the minimum ALLOWANCE of female doctors. (d) Display the highest consolation fee among all male doctors. (e) SELECT count(*) from DOCTOR where SEX=F; (f) SELECT NAME, DEPT, BASIC from DOCTOR, SALARY where

Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 2 of 11 DEPT=ENT and DOCTOR.ID=SALARY.ID ii) Write SQL commands for (a) to (f) and write the output for (g) on the basis of tables INTERIORS and NEWONES. Question for High Achievers TABLE: INTERIORS No 1 2 3 4 5 6 7 8 9 10 ItemnName Red Rose Soft Touch Jerry Home Rough Wood Comfort Zone Jerry Look Lion King Royal King Dine Paradise Dine Paradise Type Double Bed Baby cot Baby cot Office Table Double Bed Baby cot Office Table Sofa Sofa Dining Table DateOfStock 23/02/02 20/01/02 19/02/02 20/02/02 12/01/02 24/02/02 20/02/02 13/12/02 13/12/01 19/02/02 Price 32000 9000 8500 20000 15000 7000 16000 30000 9000 11000 Discount 15 10 10 20 20 19 20 25 15 15

NO 11 12 13

ITEMNAME White Wood James007 Tom look

TABLE: DIS TYPE Double Bed Sofa Baby cot

DATEOFSTOCK 23/03/03 20/02/03 21/02/02

PRICE 20000 15000 7000

DISCOUNT 20 15 10

(a) To show all information about the Sofas from the INTERIORS table. (b) To list the ITEMNAME which are price at more than 10000 from the INTERIORS table? (c) To list ITEMNAME and TYPE those items, in which DATEOFSTOCK is before 22/01/02 from the INTERIORS table in descending order of ITEMNAME. (d) To display ITEMNAME and DATEOFSOCK of those items, in which the discount percentage is more than 15 from INTERIORS table. (e) To count the number of items, whose type is Double Bed from INTERIORS table? (f) Give the output of following SQL statement : (i) Select COUNT (Distinct TYPE) FROM INTERIORS. (ii) Select AVG(DISCOUNT) from INTERIORS where TYPE=Baby cot; (iii) Select SUM(Price) from INTERIORS where DATEOFSTOCK<{12/02/02} ;

Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 3 of 11 (iii) Book_i d C0001 F0001 T0001 T0002 F0002 Book_Name Fast Cook The Tears My first C++ C++ Brainworks Thunderbolt s Table: BOOKS Author_Nam Publisher e s Lata Kapoor EPB William Hopkins Brain Brooke A.W. Rossaine Anna Roberts First Publ & BPB TDH First Publ Pric e 355 650 350 350 750 Type Cooker y Fiction Text Text Fiction Quantit y 5 20 10 15 50

TABLE: ISSUED Book_Id Too1 C001 F0001 Quantity_Issued 4 5 2

(a) To show Book Name, Author Name and Price of the books of EPB Publisher. (b) To list the name from books of text type (c) To display the names and price form books in ascending order of their price (d) To increase the price of all books of EPB Publishers by 50 (e) To display the Book_Id, Book_Name and Quantity_Issued for all books which have been issued? (f) Give the output of the follwing queries based on the above table : (i) SELECT COUNT(*) FROM Book; (ii) SELECT MAX(Price) from Book where Quantity>=15; (iii) SELECT COUNT(DISTINCE Publisher=First Publ; (iv) SELECT book_name from book ,Author_Name books were price>=400;

Answer: Q1) Primary key is key which give unique records from database. Primary key cant be null. Key that can be serve as primary key is called candidate key. A candidate key that is not primary key is called alternate key. 2: Sql statements are DDL used to define the objects in relational database. DML Used for query, addition, deletion, and updation of data stored in the database DCL (Data control language) used for controlling data stored in the databases.

Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 4 of 11 3: Advantage: Data redundancy is reduced, sharing of data, inconsistency is controlled, data integrity. 4: View is derived from another table, which is referred to as the base table of the view 5: Delete command can delete the record form table. While drop command delete table from database. 6: Theory of DDL and DML. 7. Different type of constraints are : Not Null, Default, Primary, Check, Unique b. Ans (i) (i) SELECT NAME FROM DOCTOR WHERE DEPT=MEDICINE AND EXPERIENCE >10; (ii) SELECT AVG (E.Salary=E.BASIC+E.ALLOWANCE) FROM DOCTOR D SALARY E WHERE D.DEPT=ENT AND D.ID=E.ID; (iii) SELECT MIN (E.ALLOWANCE) FROM DOCTOR D, SA;ARU E WJERE D.SEX=F AND D.ID=E.ID; (Iv) SELECT MAX (E.CONSULTANTION) FROM DOCTOR D, SALARY E WHERE D.SEX=M AND D.ID=E.ID; (v) (vi) 4 Name John

Dept Basic Ent 12000

Ans : (ii) (a) SELECT * FROM INTERIOR WHERE TYPE=SOFA; (b) SELECT ITEMNAME FROM INTERIORS WHERE PRICE>10000 ( c ) SELECT ITEMNAME, TYPE FROM INTERIORS WHERE DATEOFSTOCK<(22/01/02) IN DESC; ( d )SELECT I.ITEMNAME,I. DATEOFSTOCK FROM INTERIORS I DIS D WHERE (D.DISCOUNT>10) AND (D.NO=I.NO); ( e) SELECT COUNT(Double Bed) FROM INTERIORS;

( f) (i)Type Double bed Baby Cot Office table Sofa Dining Table (ii) 6066.66 (iii) 24000

Ans No: III

Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 5 of 11 (a) SELECT book_name, author_name, price FROM BOOKSnWHERE PUBLISHER=EPB (b) SELECT book_name, FROM BOOKS WHERE Type=Text ; (c) SELECT book_name, price FROM BOOKS ORDER BY book_name asc; (d) UPDATE books setprice=price+50; whe4re publisher=First publ; (e) SELECT a.book_id, abook.name, b.quantity_issued from book a issued b where a.book_id=b.bookid; (f) (i) 5 (ii) 750 (iii) The tears, Thunderboalts (iv) Book Name Authors The Tears William Hopkins Thunder Bolt Anna Robers

Q.1. What do you mean by data redundancy? Ans.: Duplication of data is known data redundancy. Q.2. What do you mean by data independence? Ans. The ability to modify a scheme definition in one level without changing the scheme of next level is called data independence. Q.3. Differentiate between Physical Data independence & logical data independence. Ans.: Physical data independence refers to the ability to modify the physical schema without affecting the conceptual schema. Logical data independence refers to the ability to modify the conceptual schema without affecting the external schema. Q.4 Define the following terms: (i) Degree (ii) Domain (iii) Cardinality (iv) Primary key (v) Candidate key (vi) Alternate key Ans.: (i) Degree: - The number of attribute in a relation is know as the degree of relation. (ii) Domain: - A collection of all possible value for an attribute is known as domain for that attribute. (iii) Cardinality: - The number of rows in a relation is known as cardinality of that relation. (iv) Primary key: - A primary key is a set of one or more attribute that can uniquely identify a tuples in a relation. (v) Candidate Key: - All attribute in a relation that can serve as a primary key are candidate key for that relation.

Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 6 of 11 (vi) Alternate key: - A candidate key that is not a primary key is called a candidate key.

Q.5. Define the following:(i) DDL (ii) DML Ans.:DDL: - Data definition Language (DDL) is a language which is use to define the data. DML: - Data Manipulation Language (DML) is a language which is uses to manipulate the data. Q.6. Differentiate between SQL Command DROP TABLE and DROP VIEW. Ans. DROP TABLE remove the given relation from the database while DROP VIEW remove a view from the data base. Q.7. Write SQL commands for (i) to (vii) on the basis of table STUDENT

Table: STUDENT StudentNo Class Name Game Grade1 10 7 Sameer Cricket B 11 8 Sujit Tennis A 12 7 Kamal Swimming B 13 7 Veena Tennis C 14 9 Archana Basket Ball A 15 10 Arpit Cricket A (i) (ii) (iii) (iv) (v) (vi) (vii) Ans.:

SUPW Photography Gardening Photography Cooking Literature Gardening

Grade2 A C B A A C

Display the name of the students who are getting a grade C in either GAME or SUPW. Display the number of students getting grade A in cricket. Display the different games offered in the school. Display the SUPW taken up by the students, whose name starts with A. Add a new column named Marks. Assign a value 200 for Marks for all those who are getting grade B or above in GAME. Arrange the whole table in the alphabetical order to SUPW. (i) (ii) (iii) (iv) (v) (vi) (vii) SELECT Name FROM Student WHERE Grade1=C OR Grade2 = C; SELECT Count(*) FROM Student WHERE Grade1 = A AND Game=Cricket; SELECT Distinct Game FROM Student; SELECT SUPW FROM Student WHERE Name LIKE A%; Alter Table Student ADD Marks Number(6); UPDATE Student SET Marks = 200 WHERE Grade1 <= B; SELECT * FROM Student ORDER BY SUPW;

Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 7 of 11

Q. 8. Write the out put of following query based on the table STU Table: STU StudentNo Class Name Marks 10 7 Sameer 100 11 8 Sujit 110 12 7 Kamal 90 13 7 Veena 120 14 9 Archana 80 15 10 Arpit 160 (i) SELECT AVG (Marks) FROM Stu ; (ii) SELECT SUM (Marks) FROM Stu WHERE class = 7; (iii) SELECT MIN (Marks) FROM Stu ; (iv) SELECT MAX (Marks) FROM Stu ; (v) SELECT COUNT (*) FROM Stu WHERE StudentNo >12; Ans.: (i) 110 (ii) 310 (iii) 90 (iv) 160 (v) 2 Q.1 What is database? And what are the various levels of data abstraction in a database system. Ans. A database is collection of inter-related data. A database is implemented through three levels: i.ii.iii.Internal level (or Physical level) Conceptual level (or Logical level) External level (or View level)

Q.2 Ans.

What is data independence? And what is its type. Data independence allows modification of a scheme definition without affecting other scheme definition. There are two levels of data independence:i.ii.Logical data independence. Physical data independence.

Q.3

What is data model? Give the name of data models.

Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 8 of 11 Ans. Data model is a logical approach to use for database management system. There are three types of data model: - Relational, Network, Hierarchical data model.

Q.4

Define the following terms in database:(a). Domain (d). Degree (b). (e). Tuple Cardinality (h). (c). (f). Attributes Views

(g). Data Redundancy Ans. (a).

Data Dictionary

A domain is a pool of values from which the actual values

appearing in a given column are drawn. (b). (c). (d). relation. (e). The number of tuples or rows in a relation is called the The rows of tables or relations are known as tuple. The column of tables or relations are known as attributes. The number of attributes in a relation determine the degree of a

cardinality of the relation. (f). A View is a virtual table that does not really exist. It derived from

one or more base tables. (g). (h). about data. Duplication of data is known as Data Redundancy. A Data Dictionary is a file that contains metadata i.e., data

Q.5

What is Key? Define following keys: (a). Primary Key (b). Foreign Key

(c). Candidate Key (d). Alternate Key Ans. (a). A Primary Key is a set of one or more attributes that can

uniquely identify tuples in the relation. (b). A non-key attributes whose values are derived from the primary

key of some other table is known as Foreign Key. (c). All attribute combinations inside a relation that can serve as primary key are Candidate Keys as they are candidate for the primary key position. (d). A candidate key that is not the primary key is called an Alternate Key. Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 9 of 11

Q.6

What is relation algebra? And write the various operations of relation

algebra. Ans. The relational algebra is a collection of operation on relations. The various oerations of relational algebra are Select ( ), Project ( ), Cartesian product ( ), Union ( ), Set difference (-), Set intersection ( ), Join ( ) etc

Q.7 Ans.

What is SQL? And also define DDL, DML. SQL stands for Structure Query Language. It is a database language that enables to create and operate on relational databases. DDL: - Data Definition Language (DDL) provides commands for

defining relation schemas, deleting relations, creating indexes, and modifying relation schemas. DML: - Data Manipulation Language (DML) includes commands for manipulates tuples in databases. (Insert, delete, update commands etc)

Q.8 Ans.

What is Constraints? Write down different types of Constraints. A Constraint is a condition or check applicable on a field or set of fields. These constraints ensure database integrity, thus are sometimes called database integrity constraints. Some constraints are: i.iii.v.Unique constraint Default constraint Not Null constraint ii.iv.vi.Primary Key constraint Check constraint Foreign Key constraint

Q.9 Ans.

What is an aggregate function? Aggregate functions calculate the summary values from the data in a particular column. E.g. avg(), min(), max(), sum(), count().

Q.10 Write a query that selects all orders except those with zeros or NULLs in the amt field. Ans. SELECT * FROM Orders

Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 10 of 11 WHERE amt <> 0 AND (amt IS NOT NULL); OR SELECT * FROM Orders WHERE NOT (amt=0 OR amt IS NULL);

Q.11 Write a query that produces the Salesman table with the columns in the following order: city, salesman-name, salesman-code, commission. Ans. SELECT city, sales-man, salesman-code, commission FROM Salesman;

Q.12 Different between DROP TABLE and DROP VIEW SQL commands. Ans. DROP TABLE removes the given relation from the database and DROP VIEW removes a view from the database.

Q.13

Write the syntax for the following commands:CREATE TABLE, SELECT, INSERT, UPDATE, DELETE, ALTER TABLE, DROP TABLE, AND DROP VIEW.

Ans.

The syntax of CREATE TABLE command is: CREATE TABLE <table name> (<column-name1> <data type> [(<size>)], <column-name2> <data type> [(<size>) ]);

The syntax of SELECT statement is: SELECT * / <column name1> [, <column name2>, .] FROM <table name> [WHERE <condition> GROUP BY <column name(s)> HAVING <search condition> Order by <column name>]; The syntax of INSERT command is: INSERT INTO <table name> [<column1>, <column2>, .] VALUES (<value1>, <value2> .);

The syntax of DELETE command is: Prepared By Sumit Kumar Gupta, PGT Computer Science

Page 11 of 11 DELETE FROM <table name> [WHERE <condition>];

The syntax of UPDATE command is: UPDATE <table name> SET <column name> = <value> [WHERE <condition>];

The syntax of DELETE command is: DELETE FROM <table name> [WHERE <condition>];

The syntax of ALTER TABLE command is: ALTER TABLE <table name> ADD <column name> <data type> <size>;

The syntax of DROP TABLE command is: DROP TABLE <table name>;

The syntax of DROP VIEW command is: DROP VIEW <view name>;

Prepared By Sumit Kumar Gupta, PGT Computer Science

You might also like