03 Data Base SQL

11
Page 1 of 11 Prepared By Sumit Kumar Gupta, PGT Computer Science Data Base SQL Q. a) i) Define the following terms alternate key, primary key, candidate key. ii) What are the different types of SQL statements? iii) What are the advantages of DBMS? iv) What is the difference between VIEW and TABLE? v) Differentiate between DELETE and DROP command. vi) Differentiate between DDL and DML statements. vii) What are the different types of constraints? Explain each one in brief. 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 NAMER DEPT SEX EXPERIENCE 101 John ENT M 12 104 Smith ORTHOPEDIC M 5 107 Rahul CARDIOLOGY M 10 114 Lara SKIN F 9 109 Mehul MEDICINE F 9 105 Jonson ORGHOPEIC M 10 117 Lucy ENT F 3 111 Becon Medicine F 10 130 Morphy ORTHOPEDIC M 15 TABLE :SALARY ID BASIC ALLOWANCE CONSULTATION 101 12000 1000 300 104 23000 2300 500 107 32000 4000 500 114 12000 5200 100 109 42000 1700 200 105 18900 1690 300 130 21700 2600 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

Transcript of 03 Data Base SQL

Page 1: 03 Data Base SQL

Page 1 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

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

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 NAMER DEPT SEX EXPERIENCE 101 John ENT M 12 104 Smith ORTHOPEDIC M 5 107 Rahul CARDIOLOGY M 10 114 Lara SKIN F 9 109 Mehul MEDICINE F 9 105 Jonson ORGHOPEIC M 10 117 Lucy ENT F 3 111 Becon Medicine F 10 130 Morphy ORTHOPEDIC M 15

TABLE :SALARY

ID BASIC ALLOWANCE CONSULTATION 101 12000 1000 300 104 23000 2300 500 107 32000 4000 500 114 12000 5200 100 109 42000 1700 200 105 18900 1690 300 130 21700 2600 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

Page 2: 03 Data Base SQL

Page 2 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

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

TABLE: DIS NO ITEMNAME TYPE DATEOFSTOCK PRICE DISCOUNT 11 White Wood Double Bed 23/03/03 20000 20 12 James007 Sofa 20/02/03 15000 15 13 Tom look Baby cot 21/02/02 7000 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} ;

No ItemnName Type DateOfStock Price Discount 1 Red Rose Double Bed 23/02/02 32000 15 2 Soft Touch Baby cot 20/01/02 9000 10 3 Jerry Home Baby cot 19/02/02 8500 10 4 Rough Wood Office Table 20/02/02 20000 20 5 Comfort Zone Double Bed 12/01/02 15000 20 6 Jerry Look Baby cot 24/02/02 7000 19 7 Lion King Office Table 20/02/02 16000 20 8 Royal King Sofa 13/12/02 30000 25 9 Dine Paradise Sofa 13/12/01 9000 15 10 Dine Paradise Dining Table 19/02/02 11000 15

Page 3: 03 Data Base SQL

Page 3 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

(iii) Table: BOOKS

Book_id

Book_Name Author_Name

Publishers

Price

Type Quantity

C0001 Fast Cook Lata Kapoor EPB 355 Cookery

5

F0001 The Tears William Hopkins

First Publ 650 Fiction 20

T0001 My first C++ Brain & Brooke

BPB 350 Text 10

T0002 C++ Brainworks

A.W. Rossaine

TDH 350 Text 15

F0002 Thunderbolts

Anna Roberts

First Publ 750 Fiction 50

TABLE: ISSUED

(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 can’t 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.

Book_Id Quantity_Issued Too1 4 C001 5 F0001 2

Page 4: 03 Data Base SQL

Page 4 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

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) 4 (vi) Name Dept Basic

John 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

Page 5: 03 Data Base SQL

Page 5 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

(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.

Page 6: 03 Data Base SQL

Page 6 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

(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 SUPW Grade2

10 7 Sameer Cricket B Photography A 11 8 Sujit Tennis A Gardening C 12 7 Kamal Swimming B Photography B 13 7 Veena Tennis C Cooking A 14 9 Archana Basket Ball A Literature A 15 10 Arpit Cricket A Gardening C

(i) Display the name of the students who are getting a grade ‘C’ in

either GAME or SUPW. (ii) Display the number of students getting grade ‘A’ in cricket. (iii) Display the different games offered in the school. (iv) Display the SUPW taken up by the students, whose name starts

with ‘A’. (v) Add a new column named ‘Marks’. (vi) Assign a value 200 for Marks for all those who are getting grade ‘B’

or above in GAME. (vii) Arrange the whole table in the alphabetical order to SUPW.

Ans.: (i) SELECT Name FROM Student WHERE Grade1=”C” OR

Grade2 = “C”; (ii) SELECT Count(*) FROM Student WHERE Grade1 = “A”

AND Game=”Cricket”; (iii) SELECT Distinct Game FROM Student; (iv) SELECT SUPW FROM Student WHERE Name LIKE “A%”; (v) Alter Table Student ADD Marks Number(6); (vi) UPDATE Student SET Marks = 200 WHERE Grade1 <= “B”; (vii) SELECT * FROM Student ORDER BY SUPW;

Page 7: 03 Data Base SQL

Page 7 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

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.- Internal level (or Physical level)

ii.- Conceptual level (or Logical level)

iii.- External level (or View level)

Q.2 What is data independence? And what is its type.

Ans. Data independence allows modification of a scheme definition without

affecting other scheme definition. There are two levels of data

independence:-

i.- Logical data independence.

ii.- Physical data independence.

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

Page 8: 03 Data Base SQL

Page 8 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

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 (b). Tuple (c). Attributes

(d). Degree (e). Cardinality (f). Views

(g). Data Redundancy (h). Data Dictionary

Ans. (a). A domain is a pool of values from which the actual values

appearing in a given column are drawn.

(b). The rows of tables or relations are known as tuple.

(c). The column of tables or relations are known as attributes.

(d). The number of attributes in a relation determine the degree of a

relation.

(e). The number of tuples or rows in a relation is called the

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). Duplication of data is known as Data Redundancy.

(h). A Data Dictionary is a file that contains “metadata” i.e., data

about 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.

Page 9: 03 Data Base SQL

Page 9 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

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 What is SQL? And also define DDL, DML.

Ans. 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 What is Constraints? Write down different types of Constraints.

Ans. 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.- Unique constraint ii.- Primary Key constraint

iii.- Default constraint iv.- Check constraint

v.- Not Null constraint vi.- Foreign Key constraint

Q.9 What is an aggregate function?

Ans. 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

Page 10: 03 Data Base SQL

Page 10 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

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:

Page 11: 03 Data Base SQL

Page 11 of 11

Prepared By Sumit Kumar Gupta, PGT Computer Science

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>;