DBMS Concepts and SQL - Students Notes
DBMS Concepts and SQL - Students Notes
Relational Database
A relational database is a collection of data items organized as logically related tables.
Table/Relation
A group of rows and columns from a table. The horizontal subset of the Table is known as
a Row/Tuple. The vertical subset of the Table is known as a Column/an Attribute.
A relation in a database has the following characteristics:
Cardinality
No. of Rows of Table
Key
An Attribute/group of attributes in a table that identifies a tuple uniquely is known as a
key. A table may have more than one such attribute/group of identifies that identifies a
tuple uniquely, all such attributes(s) are known as Candidate Keys. Out of Candidate
keys, one is selected as Primary key, and others become Alternate Keys.
A Foreign Key is defined in a second table, but it refers to the primary key in the first
table.
Relational algebra
Relational algebra is a formal system for manipulating relations. Set of operations that can be
carried out on a relation:
Selection : To select a horizontal subset of a relation
Projection : To select vertical subset of a relation
Cartesian Product: It operates on two relations and is denoted by X. for example
Cartesian product of two relation R1 and R2 is represented by R=R1X R2. The degree
of R is equal to sum of degrees of R1 and R2. The cardinality of R is product of
cardinality of R1 and cardinality of R2
Example cartesian Product
The table R1
Empno Ename Dept
1 Bill A
2 Sarah C
3 John A
The table R2
Dno Dname
A Marketing
B Sales
C Legal
R1 X R2
Empno Ename Dept Dno Dname
1 Bill A A Marketing
1 Bill A B Sales
1 Bill A C Legal
2 Sarah C A Marketing
2 Sarah C B Sales
2 Sarah C C Legal
3 John A A Marketing
3 John A B Sales
3 John A C Legal
Alisha
John
Relational Operator
=, <, >, <=, >=, <>
Logical Operator
AND, OR, NOT
SELECT * FROM student WHERE fees < 7000;
rno name fees dob class
11 Peter 6700 1997-11-15 K12
13 John 6900 2000-12-13 K11
SELECT * FROM student WHERE fess > 7000 AND fees < 8000;
rno name fees dob class
10 Alex 7800 1998-10-03 K12
12 Alisha 7800 1999-07-03 K11
Alisha 7800
John 6900
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Example
UPDATE student
SET fees = '7900'
WHERE rno = 12 ;
SELECT * FROM student;
rno name fees dob class
10 Alex 7800 1998-10-03 K12
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Example
SELECT * FROM student ORDER BY name;
rno name fees dob class
10 Alex 7800 1998-10-03 K12
12 Alisha 7900 1999-07-03 K11
13 John 6900 2000-12-13 K11
11 Peter 6700 1997-11-15 K12
SELECT class, name, dob, fees FROM student ORDER BY class, name DESC;
class name dob fees
K11 John 2000-12-13 6900
K11 Alisha 1999-07-03 7900
K12 Peter 1997-11-15 6700
COUNT(rno)
SUM(fees)
29300
Deleting a table
Syntax:
DROP TABLE table_name
Example:
DROP TABLE student;
Consider the following tables Product and Client. Write SQL commands for the statement (i) to (iv) and give outputs for SQL
queries (v) to (viii)
Table: PRODUCT
Table: CLIENT
(ii) To display the details of Products whose Price is in the range of 50 to 100(Both values included).
Ans: Select all from product where Price between 50 and 100
(iii) To display the ClientName, City from table Client, and ProductName and Price from table Product, with their corresponding
matching P_ID.
Ans: ( The above question may consist DISTINCT City. If it is DISTINCT City, the following is the answer)
City
-----
Delhi
Mumbai
Bangalore
(vi) SELECT Manufacturer, MAX(Price), Min(Price), Count(*) FROM Product GROUP BY Manufacturer;
Ans:
Ans:
ClientName ManufacturerName
Cosmetic Shop ABC
Total Health ABC
Live Life XYZ
Pretty Woman XYZ
Dreams LAK
ProductName Price*4
Talcom Poweder 160
Face Wash 180
Bath Soap 220
Shampoo 480
Face Wash 380
5.b) Consider the following tables Item and Customer. Write SQL commands for the statement (i) to (iv) and give outputs for SQL
queries (v) to (viii)
Table: ITEM
Table: CUSTOMER
(i) To display the details of those Customers whose city is Delhi.Ans: Select all from Customer Where City=”Delhi”
(ii) To display the details of Item whose Price is in the range of 35000 to 55000 (Both values included).
Ans: Select all from Item Where Price>=35000 and Price <=55000
(iii) To display the CustomerName, City from table Customer, and ItemName and Price from table Item, with their corresponding
matching I_ID.
(iv) To increase the Price of all Items by 1000 in the table Item.
Ans: City
Delhi
Mumbai
Bangalore
Ans:
Ans:
(viii) SELECT ItemName, Price * 100 FROM Item WHERE Manufacturer = „ABC‟;
Ans:
ItemName Price*100
Personal Computer 3500000
Laptop 5500000
5.b) Consider the following tables Consignor and Consignee. Write SQL command for the statements(i)to(iv) And give outputs for
the SQL quries (v) to ( viii). 6
TABLE : CONSIGNOR
TABLE : CONSIGNEE
(ii) To display the cneeID, cnorName, cnorAddress, CneeName, CneeAddress for every Consignee.
Ans: Select CneeId, CnorName, CnorAddress, CneeName, CneeAddress from Consignor,Consignee where
Consignor.CnorId=Consignee.CnorId;
Ans:
CneeCity
Mumbai
New Delhi
Kolkata
(vi) SELECT A.CnorName A, B.CneeName B FROM Consignor A, Consignee B WHERE A.CnorID=B.CnorID AND
B.CneeCity=‟Mumbai‟;
(vii) SELECT CneeName,CneeAddress FROM Consignee WHERE CneeCity Not IN („Mumbai‟, „Kolkata‟);
Ans:
(viii) SELECT CneeID, CneeName FROM Consignee WHERE CnorID = „MU15‟ OR CnorID = „ND01‟;
senderCity
New Delhi
Mumbai
Ans: CneeID
CneeName
MU05
Rahul Kishore
KO19 A P Roy
Delhi 2007:
5.b) Consider the following tables. Write SQL command for the statements (i)to(iv)and give outputs for the SQL quries (v) to (viii). 6
TABLE : SENDER
TABLE :RECIPIENT
(ii) To display the recID, senderName, senderAddress, RecName, RecAddress for every recipt.
Ans: Select recID, SenderName, SenderAddress, RecName, RecAddress from Sender, Recipient where
Sender.Senderid=Recipient.RenderId;
CnorName
R singhal
Amit Kumar
CneeName
Rahul Kishore
S mittal
CneeName CneeAddress
Ans:
(vi) SELECT A.SenderName A, B.RecName FROM Sender A, Recipient B WHERE A.SenderID=B. SenderID AND
B.RecCity=‟Mumbai‟;
(viii) SELECT RecID, RecName FROM Recipient WHERE SenderID = „MU02‟ OR SenderID = „ND50‟;
5.b) Study the following tables FLIGHTS and FARES and write SQL commands for the questions (i) to (iv) and give outputs for
SQL quires (v) to(vi).
TABLE: FLIGHTS
NO_
FL_NO STARTING ENDING NO_ STOPS
FLGHTS
TABLE:FLIGHTS
(i) Display FL_NO and NO_FLIGHTS from “KANPUR” TO “BANGALORE” from the table FLIGHTS.
Ans: Select FL_NO, NO_FLIGHTS from FLIGHTS where Starting=”KANPUR” AND ENDING=”BANGALORE”
(ii) Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.
Ans: (Children, Try this as an assignment)
(iii) Display the FL_NO and fare to be paid for the flights from DELHI to MUMBAI using the tables FLIGHTS and FARES, where the
fare to paid = FARE+FARE+TAX%/100.
Ans: Select FL_NO, FARE+FARE+(TAX%/100) from FLIGHTS, FARES where Starting=”DELHI” AND Ending=”MUMBAI”
(iv) Display the minimum fare “Indian Airlines” is offering from the tables FARES.
v) Select FL_NO,NO_FLIGHTS,AIRLINES from FLIGHTS, FARES Where STARTING = “DELHI” AND FLIGHTS.FL_NO =
FARES.FL_NO
DELHI 2006:
5.b) 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 (vi) :
TABLE: DOCTOR
TABLE: SALARY
(i) Display NAME of all doctors who are in “MEDICINE” having more than 10 years experience from the Table DOCTOR.
(ii) Display the average salary of all doctors working in “ENT”department using the tables. DOCTORS and SALARY Salary
=BASIC+ALLOWANCE.
(iv) Display the highest consultation fee among all male doctors.
Ans: 4
(vi) SELECT NAME, DEPT , BASIC from DOCTOR, SALRY Where DEPT = “ENT” AND DOCTOR.ID = SALARY.ID
DELHI 2005:
(5) Consider the following tables EMPLOYEES and EMPSALARY. write SQL commands for the Statements (i) to (iv) and give
outputs for SQL quires (v) to (viii).
(i) To display Firstname, Lastname, Address and City of all employees living in Paris from the table EMPLOYEES.
(iii) To display the Firstname, Lastname, and Total Salary of all managers from the tables, where Total Salary is calculated as
Salary+Benifts.
(iv) To display the Maximum salary among Managers and Clerks from the table EMPSALARY.
Ans: 4
(vii) SELECT DESIGNATION , SUM(SALARY) FROM EMPSALARY GROUP BY DESIGNATION HAVING COUNT(*)>2;
Ans: 32000
5) Consider the following tables WORKERS and DESIG. Write SQL commands for the statements (i) to (iv) and give outputs for
SQL queries (v) to (viii). WORKERS
DESIG
DESIGINA
W_ID SALARY BENEFITS
TION
(i) To display W_ID Firstname, address andCity of all employees living in New York fromthe Table WORKERs
(iii) To display the FIRSTNAME, LASTNAME and Total Salary of all Clerks from the tables WORKERS And DESIG, where Total
salary is calculated as Salary + benifts.
Ans: Select firstname, lastname, salary+benefits where worker.w_id=desg.w_id and Designation=”Clerk”
(iv) To display the minimum salary among managers and Clerks from the tables DESIG.
(v) SELECT FIRSTNAME, SALARY FROM WORKERS, DESIG WHERE DESIGINATION = “MANAGER” AND WORKERS.W_ID =
DESIGN.W_ID
Ans: 4
(vii) SELECT DESIGNATION, SUM(SALARY) FROM DESIG GROUP BY DESIGNATION HAVING COUNT (*) < 3;
Ans: 15000
2004:
TABLE : BOOKS
TABLE:ISSUED
BOOK_ID QUANTITY_ISSUED
F0001 3
T0001 1
C0001 5
Write SQL queries from b to g.
(b)To show Book name, Author name and Price of books of EPB publisher.
(d) To display the names and prices of the books in descending order of their price.
Ans: update books set price= price+50 where publishers = “First Publ”
(f) To Display the Book_ID, Book_Name and Quantity Issued for all books Which have been issued.
(g) To insert a new row in the table Issued having the following data: “F0002”,4
(h) Give the output of the following queries on the above tables:
Ans: 3
Ans: Book_Name Author_Name My First C++ Brian & Brooks C++ Brainworks A.W.Rossaine Fast Cook Lata Kapoor.
Ans: 5
2003:
5.b Write SQL commands for (b) to (g) and write the outputs for (h) on the basis of tables TNTERIORS and NEWONES.
TABLE: INTERIORS
NO ITEM NAME TYPE DATEOFSTOCK PRICE DISCOUNT
TABLE:NEWONES
(b) To show all information about the sofas from the INTERIORS table.
(d) To list ITEMNAME and TYPE of those items, in which DATEOFSTOCK is before 22/01/02 from the INTERIORS table in
descending order of ITEMNAME.
(e) To display ITEMNAME and DATEOFSTOCK of those items in which the Discount percentage is more than 15 from
INTERIORS.
(g) To insert new row in the NEWONES table with the following data:14, “True Indian “, “Office Table “, {28/03/03},15000,20.
Ans: 5
(ii) Select AVG(DISCOUNT)from INTERIORS where TYPE =”Baby cot”;
Ans: 13
Ans: 53000
2002:
5. Given the following Teacher Relation. Write SQL Commands fro (b) to (g)
(d) To list all names of teachers with date of admission in ascending order.
(f)To count the number of items whose salary is less than 10000
(g) To insert a new record in the Teacher table with the following data: 8,”Mersha”,”Computer”, (1/1/2000),12000,”M”.Ans: Insert
into Teacher values ,”Mersha”, ”Computer”,{1/1/2000),12000,”M”);
2001:
5.b) Write the SQL commands for (i) to (vii) on the basis of the table SPORTS
TABLE: SPORTS
(i) Display the names of the students who have grade „C‟ in either Game1 or Game2 or both.
Ans: Select Count(*) from Sports Where (Game1=”Cricket” and Grade1=”A”) or (Game2=”Cricket” and Grade2=”A”)
(iii) Display the names of the students who have same game for both game1 and game2
(iv) Display the games taken up by the students, whose name starts with „A‟.
(vi) Assign a value 200 for marks for all those who are getting grade „B‟ or „A‟ in both Game1 and Game2.
2000 :
5. Write SQL commands for the (b) to (e) and write the outputs for (g) on thse basis of table CLUB.
TABLE: CLUB
(b) To show all information about the swimming coaches in the club.
(c) To list names of all coaches with their date of appointment (DATOFAPP) in descending order.
(d) To display a report, showing coachname, pay, age and bonus(15% of pay) for all coaches.
(e) To insert a new row in the CLUB table with following data: 11,”PRAKASH”,37,”SQUASH”, {25/02/98},2500,”M”
Ans: 4
Ans: 34
Ans: 1100
Ans: 7800
(G) Assuming that there is one more table COACHES in the database as shown below:
TABLE:COACHES
SPORTS PERSON SEX COACH_NO
AJAY 1 M 1
SEEMA F 2
VINOD M 1
TANEJA F 3
Ans:
AJAY 1 KUKREJA
SEEMA RAVINA
VINOD KUKREJA
TANEJA KARAN
1999:
5.) Given the following Teacher relation: Write SQL commands for questions (b) to (g). TEACHER
Ans: select name from teacher where sex=‟male‟ and department= ‟maths‟;
d) To list names of all teacher with their date of joining in ascending order.
(g) To insert a new row in the teacher table with the following data: 9, “raja‟, 26,“computer”, {13/5/95 }, 2300, “M”.