0% found this document useful (0 votes)
53 views94 pages

Dbms Record

1. The document describes how to create tables in SQL to store salesman, product, client and sales order information using various data types and constraints. 2. It provides the SQL commands to create the tables with the required columns, primary keys, foreign keys, data types, sizes and other attributes. 3. The document also shows sample insert statements and queries to retrieve, update, delete and alter records in the created tables.

Uploaded by

Chris Sosa Jacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views94 pages

Dbms Record

1. The document describes how to create tables in SQL to store salesman, product, client and sales order information using various data types and constraints. 2. It provides the SQL commands to create the tables with the required columns, primary keys, foreign keys, data types, sizes and other attributes. 3. The document also shows sample insert statements and queries to retrieve, update, delete and alter records in the created tables.

Uploaded by

Chris Sosa Jacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 94

SQL COMMANDS

1
Department of Computer Application Kristu Jyoti College of Management &Technology
1)

a) Create the relation to show salesman information called as salesman_master with the
following attributes:

Attributes DataType Size Attribute Description

Primary key, first letter


Salesmanno Varchar 6
must start with ‘S’

Salesmanname Varchar 10 Should not be null

Address1 Varchar 10 Should not be null

City Varchar 10

Pincode Number 8

State Varchar 10

NOT NULL,cannot be
Salamt Number 8
0

Salesamt Number 10 NOT NULL

Remarks Varchar 20 NOT NULL

b) Create the relation product_master tostorethe product information

Attributes DataType Size Attribute Description

productno Varchar 6 Primary key, first letter must


start with P.

Description Varchar 10

Salesmanno Varchar 6 Foreignkey, use on delete


cascade option

Profitpercent Number 4

Unitmeasure Varchar 10

Qtyinhand Number 8 Should not be null

Reorderlvl Number 8 Should not be null

Sellprice Number 8 Should not be null, cannot be


0

2
Department of Computer Application Kristu Jyoti College of Management &Technology
Costprice Number 8 Should not be null, cannot be
0

Show the result of the following questions:

Insert a minimum of 4 records.

1 Find the names of salesmen who have a salary of 2000 to 5000 both inclusive. Order
the result
in the descending order of the names.
2 Change the city of the salesman “S003” to Mumbai.
3 Change the size of the Salesmanname column size to varchar2(25 )
4 What is Martin Jane’s address and sales amount? ( If Martin Jane’s record is not
present insert/update the record)
5 List the names of all salesmen having ‘n’ as the third letter in their names.
6 Change the size of the Salesmanname column size to varchar2(25 )
7 List all the salesmen who do not stay in “Maharashtra” or “Tamil Nadu”. ( If
Maharashtra” or “Tamil Nadu record is not present update state )
8 Find the salesmenno of those salesmen who do not sell a product.
9 Delete the details of those salesmen whose remarks is “very poor”.
10 Change the size of the selling price column in product_master relation to 10,3.

Answers
a) CREATE table salesman_master

Salesmanno VARCHAR(6) PRIMARY KEY CHECK(Salesmanno LIKE 'S%'),

Salesmanname VARCHAR(10) NOT NULL,

Address1 VARCHAR(10 )NOT NULL,

City VARCHAR(10),

Pincode NUMBER(8),

State VARCHAR(10),

Salamt NUMBER(8) NOT NULL,

Salesamt NUMBER(8) NOT NULL,

Remarks VARCHAR(10) NOT NULL

3
Department of Computer Application Kristu Jyoti College of Management &Technology
);

Table created.

b) CREATE table product_master

productno VARCHAR(6) PRIMARY KEY CHECK(productno LIKE 'P%'),

Description VARCHAR(10),

Salesmanno VARCHAR(6) REFERENCES salesman_master ON DELETE CASCADE,

Profitpercent NUMBER(4),

Unitmeasure VARCHAR(10),

Qtyinhand NUMBER(8) NOT NULL,

Reorderlvl NUMBER(8) NOT NULL,

Sellprice NUMBER(8) NOT NULL CHECK(Sellprice!=0),

Costprice NUMBER(8) NOT NULL CHECK(Costprice!=0)

);

Table created.

INSERT INTO salesman_master


VALUES('S001','Martin','JaneHouse','Mahtra',685643,'Mhta',2000,5000,'very poor');

1 row created.

INSERT INTO salesman_master


VALUES('S002','Sam','SamHouse','Tndu',685643,'TN',5000,8000,'average');

1 row created.

INSERT INTO salesman_master


VALUES('S003','Dani','DaniHouse','Ekm',685643,'Kerala',3000,7000,'excellent');

4
Department of Computer Application Kristu Jyoti College of Management &Technology
1 row created.

INSERT INTO salesman_master VALUES('S004','John','JohnHouse','Delhi',685643,'New


Delhi',1000,3000,'poor');

1 row created.

INSERT INTO product_master VALUES('P001','Laptop','S001',12,8,10,5,25000,24000);

1 row created.

INSERT INTO product_master VALUES('P002','Harddisk','S001',8,6,20,10,5000,4500);

1 row created.

INSERT INTO product_master VALUES('P003','Tablet','S002',16,10,25,15,10000,9000);

1 row created.

INSERT INTO product_master VALUES('P004','Desktop','S004',35,15,15,3,15000,13000);

1 row created.

1. SELECT Salesmanname FROM salesman_master WHERE Salamt>=2000 AND


Salamt<= 5000 ORDER BY Salesmanname DESC;

SALESMANNAME
-----------------------
Sam
Martin
John

2. UPDATE salesman_master SET City='Mumbai' WHERE Salesmanno='S003';

1 row Updated.

3. ALTER Table salesman_master MODIFY (SALESMANNAME VARCHAR2(25) );

Table Altered

5
Department of Computer Application Kristu Jyoti College of Management &Technology
4. SELECT Address1, Salesamt FROM salesman_master WHERE
Salesmanname='Martin Jane';

ADDRESS1 SALESAMT
---------------- -------------------
JaneHouse 5000

5. SELECT Salesmanname FROM salesman_master WHERE Salesmanname LIKE


'__n%';

SALESMANNAME
-----------------------
Dani

6. ALTER Table salesman_master MODIFY ( State VARCHAR2(25) );

Table altered

7. SELECT Salesmanname FROM salesman_master WHERE State NOT


IN('Maharashtra','Tamil Nadu');

SALESMANNAME
-----------------------
Sam
Dani
John

8. SELECT s.Salesmanno FROM salesman_master s, product_master p WHERE


s.Salesmanno=p.Salesmanno AND p.Unitmeasure=p.QtyinhAND;

SALESMANNO
-------------------
S004

9. DELETE salesman_master WHERE Remarks='very poor';

1 row deleted.

10. ALTER Table product_master MODIFY ( Sellprice NUMBER(10,3) );

Table Altered

6
Department of Computer Application Kristu Jyoti College of Management &Technology
2)

a) Create the relation tostoreclient information called client_master.

Attributes DataType Size Attribute Description

clientno Varchar 6 Primary key, first letter


must start with ‘C’

name Varchar 10 Should not be null

address1 Varchar 10

address2 Varchar 10

city Varchar 10

pincode number 8

state Varchar 10

balance_due number 10

b) Create the relation sales_order tostoreclient’s order details:

Attributes DataType Size Attribute


Description

Orderno varchar 6 Primary key, first


letter must start with
‘O’

clientno varchar 6 Foreign key from the


client_master Table.

Orderdate date Should not be null.

Dest_address varchar 15

salesmanname varchar 10

deliverydate date

billpaid char 1

orderstatus varchar 10 values should be


inprocess, fulfilled,
returned, cancelled.

7
Department of Computer Application Kristu Jyoti College of Management &Technology
Insert a minimum of 4 records to the relations.

1 Retrieve the entire contents of the client_master relation in the alphabetical order of
their names.
2 List all the clients who are not located in “Maharashtra”.
3 Change the balance_due of clientno “C00001” to Rs.1000
4 Add a column called Telephone of data type number to client_master relation.
5 List all information from the sales_order Table for orders placed in the month of June,
2008.
6 List the names of all clients having ‘a’ as the second letter in their names.
7 List the clientno’s whose order delivery has been made in the month of June, 2009 but
whose bills are not yet paid.
8 Delete the client details located in Tamil Nadu.

Answers
a) CREATE Table client_master (

clientno VARCHAR(6) PRIMARY KEY CHECK(clientno LIKE 'C%'),

name VARCHAR(10) NOT NULL,

address1 VARCHAR(10),

address2 VARCHAR(10),

city VARCHAR(10),

pincode NUMBER(8),

state VARCHAR(10),

balance_due VARCHAR(10)

);

Table created.

b) CREATE Table sales_order (

Orderno VARCHAR(6) PRIMARY KEY CHECK(Orderno LIKE 'O%'),

clientno VARCHAR(6) REFERENCES client_master,

8
Department of Computer Application Kristu Jyoti College of Management &Technology
Orderdate DATE NOT NULL,

Dest_address VARCHAR(15),

salesmanname VARCHAR(10),

deliverydate DATE,

billpaid CHAR(1),

orderstatus VARCHAR(10) CHECK(orderstatus IN('inprocess', 'fulfilled', 'returned',


'cancelled'))

);

Table created.

INSERT INTO client_master VALUES('C00001','Lazz','Lazz Home','Home


Town','Mhta',656869,'Mhta',2000);

1 row created.

INSERT INTO client_master VALUES('C00002','Kate','Kate House','Home


Town','Tndu',656845,'Tamil Nadu',1000);

1 row created.

INSERT INTO client_master VALUES('C00003','Susan','Susan Home','Home


Town','Ekm',656887,'Kerala',8000);

1 row created.

INSERT INTO client_master VALUES('C00004','Rock','Rock House','Home


Town','Delhi',656898,'New Delhi',3000);

1 row created.

9
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO sales_order VALUES('O00001','C00001','22-jun-
2009','LazzHome,Mhta','Dani','25-jun-2009','0','inprocess');

1 row created.

INSERT INTO sales_order VALUES('O00002','C00002','15-jun-


2008','KateHouse,Tndu','John','26-jun-2009','1','fulfilled');

1 row created.

INSERT INTO sales_order VALUES('O00003','C00003','26-jun-


2008','SusanHome,Ekm','John','26-aug-2008','1','returned');

1 row created.

INSERT INTO sales_order VALUES('O00004','C00002','11-jun-


2008','KateHouse,Tndu','John','21-jun-2010','0','cancelled');

1 row created.

1. SELECT * FROM client_master ORDER BY name;

CLIENT NAME ADDRESS1 ADDRESS2 CITY PINCODE STATE


BALANCE_DU
--------- --------- ------------------ ---------------- -------- ------------ --------------- ----------------
--
C00002 Kate Kate House Home Town Tndu 656845 Tamil Nadu 1000
C00001 Lazz Lazz Home Home Town Mhta 656869 Mhta 2000
C00004 Rock Rock House Home Town Delhi 656898 New Delhi 3000
C00003 Susan Susan Home Home Town Ekm 656887 Kerala 8000

2. SELECT clientno, name FROM client_master WHERE State='Maharashtra';

CLIENT NAME
---------- --------
C00001 Lazz

3. UPDATE client_master SET balance_due='1000' WHERE clientno='C00001';

1 row updated.

4. ALTER Table client_master ADD ( Telephone NUMBER(10) );

10
Department of Computer Application Kristu Jyoti College of Management &Technology
Table altered

5. SELECT * FROM sales_Order WHERE Orderdate BETWEEN ‘01-jun-2008' AND


'30-jun-2008';

ORDER CLIENT ORDERDATE DEST_ADDRESS SALESMANNA DELIVERYD B


ORDERSTATU
----------- ---------- ----------------- ------------------------ -------------------- --------------- -- -----
-------------
O00002 C00002 15-JUN-08 KateHouse,Tndu John 26-JUN-09 1
fulfilled
O00003 C00003 26-JUN-08 SusanHome,Ekm John 26-AUG-08 1
returned
O00004 C00002 11-JUN-08 KateHouse,Tndu John 21-JUN-10 0
cancelled

6. SELECT name FROM client_master WHERE name LIKE '_a%';

NAME
--------
Kate
Lazz

7. SELECT clientno FROM sales_Order WHERE deliveryDATE>='01-jun-2009' AND


deliveryDATE<='30-jun-2009' AND billpaid='0';

CLIENT
---------
C00001

8. DELETE client_master WHERE state='Tamil Nadu';

1 rows deleted.

11
Department of Computer Application Kristu Jyoti College of Management &Technology
3. Create the following Tables:

employee

Attributes DataType Size Attribute


Description

empno Varchar 4 Primarykey

ename Varchar 10 Should not be null

job char 10

Hiredate date

basic_sal number (9,2)

commission number (7,2)

dept_no varchar 8 Foreignkey from


dept Table

dept

Attributes DataType Size Attribute


Description

dept_no varchar 8 Should start with


letter ‘D’

dname varchar 10

location varchar 10

Answer the following questions:

Insert a minimum of 4 records.

1 List the names of the employees who have a salary less than Rs.3000 from the
employee relation.
2 List all the employees whose name starts with the letter ‘K’.
3 List the departmentname which is located in Noida and Brooklyn from dept relation.
( If Noida and Brooklyn record is not present update location )
4 Display all the employee names in the ascending order of their date of joining and
ordered next by the employee names from employee relation.

12
Department of Computer Application Kristu Jyoti College of Management &Technology
5 List all employees who were hired during 1999.
6 Change the basic salary tors.3000 where the job is Clerk.
7 Add two columns “HOD” and “Strength” of datatype varchar and size 10 to the dept
relation.
8 Define the deptname as NOT NULL constraint in the dept relation.

Answers
CREATE Table dept

dept_no VARCHAR(8) UNIQUE CHECK (dept_no LIKE 'D%'),

dname VARCHAR(10),

location VARCHAR(10)

);

Table created.

CREATE Table employee

empno VARCHAR(4) PRIMARY KEY,

ename VARCHAR(10) NOT NULL,

job CHAR(10),

hireDATE DATE,

basic_sal NUMBER(9,2),

commission NUMBER(7,2),

dept_no VARCHAR(8),

FOREIGN KEY (dept_no) REFERENCES dept(dept_no)

);

Table created.

13
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO dept VALUES('D001','H R','Noida');

1 row created.

INSERT INTO dept VALUES('D002','MANAGER','Brooklyn');

1 row created.

INSERT INTO dept VALUES('D003','BPO','Delhi');

1 row created.

INSERT INTO dept VALUES('D004','OFFICE','Mumbai');

1 row created.

INSERT INTO employee VALUES('E001','Kevin','Clerk','23-jul-1999',2500,100,'D004');

1 row created.

INSERT INTO employee VALUES('E002','Kester','PUNE','13-aug-1999',2800,200,'D004');

1 row created.

INSERT INTO employee VALUES('E003','Alvin','Manager','23-jul-1999',9500,3000,'D002');

1 row created.

INSERT INTO employee VALUES('E004','aby','Manager','4-may-1998',8000,2800,'D001');

1 row created.

1. SELECT ename FROM employee WHERE basic_sal < 3000;

ENAME
----------
Kester

2. SELECT ename FROM employee WHERE ename LIKE 'K%';

ENAME
----------
Kevin
Kester

14
Department of Computer Application Kristu Jyoti College of Management &Technology
3. SELECT dname FROM dept WHERE location IN('Noida', 'Brooklyn');

DNAME
--------------
HR
MANAGER

4. SELECT ename FROM employee ORDER BY hireDATE,ename;

ENAME
----------
Aby
Alvin
Kevin
Kester

5. SELECT ename FROM employee WHERE EXTRACT(YEAR FROM hireDATE)


=1999;

ENAME
----------
Kevin
Kester
Alvin

6. UPDATE employee SET basic_sal=3000 WHERE job='Clerk';

1 row updated.

7. ALTER Table dept ADD ( HOD VARCHAR(10), Strength VARCHAR(10) );

Table altered

8. ALTER Table dept MODIFY (dname NOT NULL) ;

Table altered

15
Department of Computer Application Kristu Jyoti College of Management &Technology
4. Create the following relations:

salesman_mast

Attributes DataType Size Attribute


Description

salesman_no varchar 4 Primarykey

Name varchar 10 Should not be null

Address varchar 10

City varchar 10

Pincode number 6

State varchar 10

Sal_amount number 8,2 Cannot be 0

target number 5,2

product_mast

Attributes DataType Size Attribute


Description

product_no varchar 6 Primarykey, first


two letters should be
‘PR’

salesman_no varchar 4 Foreign key from


salesman_mast
relation

description varchar 10

profit_percent number 5,2

units varchar 10

Qty_available number 6

Sell_price number 7,2

Cost_price Number 7,2

16
Department of Computer Application Kristu Jyoti College of Management &Technology
Answer the following questions:

Insert a minimum of 4 records.

1 List the various products available from the product_mast relation in the descending
alphabetical order.
2 Retrieve the list of names, city and the state of the salesmen in the alphabetical Order
of city and then by names.
3 List all the salesmen who are not staying in the state Florida.
( If Florida record is not present update state )
4 Display the details of salesmen S003 AND S005.
5 List all the salesmen whose name start with ‘A’ and end with ‘D’.
6 Change the salary amount to Rs.3000 of all salesmen whose salary is less than 2500.
7 Increase the size of description field from 10 to 12 in the product_mast relation.
8 Delete the details of salesmen who live in the states Florida and New Town.
( If Florida” or “New Town record is not present update state )

Answers
CREATE Table salesman_mast

salesman_no VARCHAR(4) PRIMARY KEY,

Name VARCHAR(10),

Address VARCHAR(10),

City VARCHAR(10),

Pincode NUMBER(6),

State VARCHAR(10),

Sal_amount NUMBER(8,2),

target NUMBER(5,2)

);

Table created.

CREATE Table product_mast

17
Department of Computer Application Kristu Jyoti College of Management &Technology
(

product_no VARCHAR(6) PRIMARY KEY CHECK (product_no LIKE 'PR%'),

salesman_no VARCHAR(4) REFERENCES salesman_mast,

description VARCHAR(10),

profit_percent NUMBER(5,2),

units VARCHAR(10),

Qty_available VARCHAR(6),

Sell_price NUMBER(7,2),

Cost_price NUMBER(7,2)

);

Table created.

INSERT INTO salesman_mast VALUES('S001','Arnold','Arnd


Home','Flda',685565,'Florida',2500,500);

1 row created.

INSERT INTO salesman_mast VALUES('S002','Alfread','Alfd


Home','NewTwn',685575,'New Town',3500,800);

1 row created.

INSERT INTO salesman_mast VALUES('S003','Kate','Kate Home','NewDhi',685585,'New


Delhi',8500,700);

1 row created.

INSERT INTO salesman_mast VALUES('S005','Sam','Sam Home','NewTwn',685555,'New


Town',2000,300);

1 row created.

18
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO product_mast VALUES('PR001','S001','Pistol',12,30,25,50000,45000);

1 row created.

INSERT INTO product_mast VALUES('PR002','S002','L M G',19,20,18,99999,90000);

1 row created.

INSERT INTO product_mast VALUES('PR003','S005','S M G',17,10,5,90000,75000);

1 row created.

INSERT INTO product_mast VALUES('PR004','S002','Ammo',9,100,85,8000,5000);

1 row created.

1. SELECT product_no, description FROM product_mast ORDER BY description


DESC;

PRODUCT DESCRIPTION
----------- ----------------
PR003 SMG
PR001 Pistol
PR002 LMG
PR004 Ammo

2. SELECT Name, City, State FROM salesman_mast ORDER BY City, Name;

NAME CITY STATE


----------- ------------ --------------
Arnold Flda Florida
Kate NewDhi New Delhi
Alfread NewTwn New Town
Sam NewTwn New Town

3. SELECT Name FROM salesman_mast WHERE State !='Florida';

NAME
---------
Alfread
Kate
Sam

19
Department of Computer Application Kristu Jyoti College of Management &Technology
4. SELECT * FROM salesman_mast WHERE salesman_no IN('S003', 'S005');

SALE NAME ADDRESS CITY PINCODE STATE SAL_AMOUNT


TARGET
------- --------- --------------- -------------- ------------- --------------- ------------------- --------
---
S005 Sam Sam Home NewTwn 685555 New Town 2000 300
S003 Kate Kate Home New Delhi 685585 New Delhi 8500 700

5. SELECT Name FROM salesman_mast WHERE Name LIKE 'A%d';

NAME
----------
Arnold
Alfread

6. UPDATE salesman_mast SET Sal_amount=3000 WHERE Sal_amount<2500;

1 row Updated.

7. ALTER Table product_mast MODIFY ( description VARCHAR(12) );

Table Altered

8. DELETE FROM salesman_mast WHERE State IN('Florida', 'New Town');

3 rows deleted.

20
Department of Computer Application Kristu Jyoti College of Management &Technology
5.

a) Create the relation to show Employee infORmation called as Employee _Master with
the following attributes:

Attributes DataType Size Attribute Description

Emp_ID Varchar 6 Primarykey, first letter must start with ‘E’

Emp_Name Varchar 20 Should not be null


Address1 Varchar 20 Should not be null
Address2 Varchar 20
City Varchar 10
Pincode Number 8 Cannot be 0
State Varchar 10 Must be kerala, Tamil nadu, Karnataka,
Goa
Dept_ID Varchar 6 Foreign Key Department_Master
Salary_ID Varchar 6 Foreign Key Salary_Master
Remarks Varchar 20 NOT NULL

b) Create the relation Department_Master to store the Department information

Attributes DataType Size Attribute Description


Dept_ID Varchar 6 Primarykey, first letter must start with D.
Dept_Name Varchar 10 Should not be null
Location Varchar 10

c) Create the relation Salary_Master tostorethe Salary information

Attributes DataType Size Attribute Description


Salary_ID Varchar 6 Primarykey, first letter must start with S.
Basic Number 8,2 Cannot be 0
Allowances Number 8,2
Deductions Number 8,2

21
Department of Computer Application Kristu Jyoti College of Management &Technology
Show the result of the following questions:

1. Insert a minimum of 5 records.


2. Create a View Named All_Employee_View which contains three columns Emp_name,
Dept_Name, Net_Salary . (Net salary should be calculated as basic+allowances-deductions).

3.

CREATE Table Department_Master

Dept_ID VARCHAR(6) PRIMARY KEY CHECK(Dept_ID LIKE 'D%'),

Dept_Name VARCHAR(10) NOT NULL,

Location VARCHAR(10)

);

Table created.

CREATE Table Salary_Master

Salary_ID VARCHAR(6) PRIMARY KEY CHECK(Salary_ID LIKE 'S%'),

Basic NUMBER(8,2) CHECK(Basic != 0),

Allowances NUMBER(8,2),

Deductions NUMBER(8,2)

);

Table created.

CREATE Table Employee_Master

Emp_ID VARCHAR(6) PRIMARY KEY CHECK(Emp_ID LIKE 'E%'),

22
Department of Computer Application Kristu Jyoti College of Management &Technology
Emp_Name VARCHAR(20) NOT NULL,

Address1 VARCHAR(20) NOT NULL,

Address2 VARCHAR(20),

City VARCHAR(10) ,

Pincode NUMBER(8) CHECK(Pincode != 0),

State VARCHAR(10) CHECK(State IN('Kerala', 'Tamil nadu', 'Karnataka', 'Goa')),

Dept_ID VARCHAR(6) REFERENCES Department_Master,

Salary_ID VARCHAR(6) REFERENCES Salary_Master,

Remarks VARCHAR(20) NOT NULL

);

Table created.

1.

INSERT INTO Department_Master VALUES('D001','H R','Karnataka');

1 row created.

INSERT INTO Department_Master VALUES('D002','OFFICE','Kerala');

1 row created.

INSERT INTO Department_Master VALUES('D003','RECEPTION','Kerala');

1 row created.

INSERT INTO Department_Master VALUES('D004','MANAGER','Tamil nadu');

1 row created.

INSERT INTO Department_Master VALUES('D005','B P O','Goa');

1 row created.

23
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO Salary_Master VALUES('S001',5000,1000,500);

1 row created.

INSERT INTO Salary_Master VALUES('S002',6000,3500,2000);

1 row created.

INSERT INTO Salary_Master VALUES('S003',2000,1500,100);

1 row created.

INSERT INTO Salary_Master VALUES('S004',9000,3000,2500);

1 row created.

INSERT INTO Salary_Master VALUES('S005',6000,1500,300);

1 row created.

INSERT INTO Employee_Master

VALUES('E001','Arnold','Arn Villai','Arn
Street','Knta',564768,'Karnataka','D005','S003','Average');

1 row created.

INSERT INTO Employee_Master

VALUES('E002','Sam','Sam Villai','Sam Street','Goa',564568,'Goa','D001','S001','PoOR');

1 row created.

INSERT INTO Employee_Master

VALUES('E003','Kate','Kate Villai','Kate
Street','Kla',565768,'Kerala','D002','S002','Excellent');

1 row created.

INSERT INTO Employee_Master

VALUES('E004','Susan','Susan Villai','Susan Street','T


N',545768,'Tamilnadu','D004','S004','Average');

1 row created.

INSERT INTO Employee_Master

24
Department of Computer Application Kristu Jyoti College of Management &Technology
VALUES('E005','Dani','Dani Villai','Dani Street','Kla',569868,'Kerala','D003','S005','Very
PoOR');

1 row created.

2.

CREATE VIEW All_Employee_View

AS

SELECT E.Emp_Name, D.Dept_Name, (S.Basic+S.Allowances-S.Deductions) "Net_Salary"

FROM Employee_Master E, Department_Master D, Salary_Master S

WHERE E.Dept_ID=D.Dept_ID AND E.Salary_ID=S.Salary_ID;

View created.

6:

a) Create the relation to show STUDENT information called as STUDENT _Master


with the following attributes:

Attributes DataType Size Attribute Description

Stud_ID Varchar 6 Primarykey, first letter must start with ‘S’

Stud_Name Varchar 20 Should not be null


Address1 Varchar 20 Should not be null
Address2 Varchar 20
City Varchar 10
Pincode Number 8 Cannot be 0
State Varchar 10 Must be kerala, Tamil nadu, Karnataka, Goa
Course_ID Varchar 6 Foreign Key Course_Master
Remarks Varchar 20 NOT NULL

25
Department of Computer Application Kristu Jyoti College of Management &Technology
b) Create the relation Course_Master tostorethe Course information

Attributes DataType Size Attribute Description


Course_ID Varchar 6 Primarykey, first letter must start with C.
Course_Name Varchar 10 Should not be null
Location Varchar 10

c) Create the relation Marks_Details tostorethe Marks information

Attributes DataType Size Attribute Description


Stud_ID Varchar 6 Foreign Key Student_Master
Mark1 Number 6 Cannot be negative
Mark2 Number 6 Cannot be negative
Mark3 Number 6 Cannot be negative

Show the result of the following questions:

1. Insert a minimum of 5 records.


2. Create a View Named All_STUDENT_View which contains three columns
Stud_name, Course_Name, Total_Marks (total marks should be calculated as
mark1+mark2+mark3).

Answers
CREATE Table Course_Master

Course_ID VARCHAR(6) PRIMARY KEY CHECK(Course_ID LIKE 'C%'),

Course_Name VARCHAR(10) NOT NULL,

Location VARCHAR(10)

);

26
Department of Computer Application Kristu Jyoti College of Management &Technology
Table created.

CREATE Table STUDENT_Master

Stud_ID VARCHAR(6) PRIMARY KEY CHECK(Stud_ID LIKE 'S%'),

Stud_Name VARCHAR(20) NOT NULL,

Address1 VARCHAR(20) NOT NULL,

Address2 VARCHAR(20) NOT NULL,

City VARCHAR(10),

Pincode NUMBER(8) CHECK(Pincode != 0),

State VARCHAR(10) CHECK(State IN('Kerala', 'Tamil nadu', 'Karnataka', 'Goa')),

Course_ID REFERENCES Course_Master ,

Remarks VARCHAR(20) NOT NULL

);

Table created.

CREATE Table Marks_Details

Stud_ID VARCHAR(6) REFERENCES STUDENT_Master,

Mark1 NUMBER(6) CHECK(Mark1>=0),

Mark2 NUMBER(6) CHECK(Mark2>=0),

Mark3 NUMBER(6) CHECK(Mark3>=0)

);

Table created.

27
Department of Computer Application Kristu Jyoti College of Management &Technology
1.

INSERT INTO Course_Master VALUES('C001','M C A','Kerala');

1 row created.

INSERT INTO Course_Master VALUES('C002','B C A','Kerala');

1 row created.

INSERT INTO Course_Master VALUES('C003','B B A','Karnataka');

1 row created.

INSERT INTO Course_Master VALUES('C004','MCom','Tamil nadu');

1 row created.

INSERT INTO Course_Master VALUES('C005','BCom','Goa');

1 row created.

INSERT INTO STUDENT_Master VALUES('S001','Sam','Sam Villa','Sam


Street','Ekm',564735,'Kerala','C001','Excellent');

1 row created.

INSERT INTO STUDENT_Master VALUES('S002','Aby','Aby Villa','Aby


Street','Krnta',564445,'Karnataka','C002','Very Poor');

1 row created.

INSERT INTO STUDENT_Master VALUES('S003','John','John Villa','John


Street','Goa',566735,'Goa','C003','Average');

1 row created.

INSERT INTO STUDENT_Master VALUES('S004','Lucy','Lucy Villa','Lucy Street','T


N',577735, 'Tamil nadu','C005','PoOR');

1 row created.

INSERT INTO STUDENT_Master VALUES('S005','Bill','Bill Villa','Bill


Street','Ekm',564788,'Kerala','C004','Excellent');

28
Department of Computer Application Kristu Jyoti College of Management &Technology
1 row created.

INSERT INTO Marks_Details VALUES('S001',55,52,62);

1 row created.

INSERT INTO Marks_Details VALUES('S002',42,46,42);

1 row created.

INSERT INTO Marks_Details VALUES('S003',64,62,39);

1 row created.

INSERT INTO Marks_Details VALUES('S004',56,52,53);

1 row created.

INSERT INTO Marks_Details VALUES('S005',46,54,38);

1 row created.

2,

CREATE VIEW All_STUDENT_View

AS

SELECT S.Stud_Name, C.Course_Name, (M.Mark1+M.Mark2+M.Mark3) "Total_Marks"

FROM STUDENT_Master S, Course_Master C, Marks_Details M

WHERE S.Course_ID=C.Course_ID AND S.Stud_ID=M.Stud_ID;

View created.

29
Department of Computer Application Kristu Jyoti College of Management &Technology
7. Create the following Tables:

Table : STUDENT

COLUMN NAME DATA TYPE CONSTRAINTS


Roll_no Number(5) PRIMARY KEY
Name Varchar(20) NOT NULL
Dept_id Number(5) Foreign Key
Gender Varchar(2)
Contact_no Number(10)

Table : DEPARTMENT

COLUMN NAME DATA TYPE CONSTRAINTS


Dept_id Number(5) PRIMARY KEY
Dept_name Varchar(10) NOT NULL

Write SQL queries for the following

1. a. Insert 5 records in each Table


b. UPDATE the contact number of STUDENT with Roll_no 005
2.
a. Display the data of female STUDENTs in alphabetical Order.
b. Find the number of STUDENTs in Electrical Department

3. Create a view FROM STUDENT Table

ANSWERS

CREATE TABLE STUDENT

Roll_no number(5) PRIMARY KEY,

Name varchar(20) NOT NULL,

Dept_id number(5) REFERENCES department(Dept_id),

Gender varchar(2),

Contact_no number(10)

);

30
Department of Computer Application Kristu Jyoti College of Management &Technology
Table created.

CREATE TABLE DEPARTMENT

Dept_id number(5) PRIMARY KEY,

Dept_name varchar(10) NOT NULL);

Table created.

1.

a) INSERT INTO STUDENT VALUES (001,’Anu’,1001,’F’,7896543210);

1 row created

INSERT INTO STUDENT VALUES (002,’Anju’,1002,’F’,9886533267);

1 row created

INSERT INTO STUDENT VALUES (003,’Arun’,1001,’M’,8766543459);

1 row created

INSERT INTO STUDENT VALUES (004,’Jithin’,1003,’M’,8764323450);

1 row created

INSERT INTO STUDENT VALUES (005,’Tom’,1002,’M’,6754826798);

1 row created

INSERT INTO DEPARTMENT VALUES(1001,’Mechanical’);

1 row created

INSERT INTO DEPARTMENT VALUES(1002,’Electrical’);

1 row created

INSERT INTO DEPARTMENT VALUES(1003,’Civil’);

31
Department of Computer Application Kristu Jyoti College of Management &Technology
1 row created

b) UPDATE STUDENT SET Contact_no=7779934521 WHERE Roll_no=005;

1 row Updated

2.

a) SELECT * FROM STUDENT WHERE gender=’F’ ORDER BY name;

Roll_no Name Dept_id Gender Contact_no


---------- --------- ------------- ------------ -----------------
002 Anju 1002 F 9886533267
001 Anu 1001 F 7896543210

b) SELECT COUNT(Dept_name) FROM DEPARTMENT WHERE Dept_name=’Electrical’;

COUNT(Dept_name)

3. CREATE VIEW STUD AS SELECT Roll_no, Name, Dept_id, Gender, Contact_no


FROM STUDENT;

View created

32
Department of Computer Application Kristu Jyoti College of Management &Technology
8. Create the following Tables:

CUSTOMER

Column name Data type Size Constraints

Cust_id Varchar 10 PRIMARY KEY

Fname Varchar 25 NOT NULL

Lname Varchar 25

Area Varchar 10 Kottayam ,Kollam, Kochi

Phone_number Numeric 15

INVOICE

Column Name Data type Size Constraints

Inv_no Varchar 20 PRIMARY KEY

Cust_id Varchar 10 Foreign Key

Issue_DATE Date

Write SQL queries for the following questions:

1.

a) Insert 5 records in each Table.

b) Change the issue date to 24/7/08 of cust_id ‘101’.

2.

a) Find the issue date for the customer ‘JOHNS’.

b) List the customer name and invoice number of all customers.

3.

a) Create a view on CUSTOMER Table.

33
Department of Computer Application Kristu Jyoti College of Management &Technology
Answers

CREATE Table CUSTOMER (

Cust_id VARCHAR(10) PRIMARY KEY,

Fname VARCHAR(25) NOT NULL, Lname VARCHAR(25),Area VARCHAR(10),


Phone_no

Number(15));

Table created

CREATE Table INVOICE (

Inv_no VARCHAR(20) PRIMARY KEY,

Cust_id VARCHAR(10) REFERENCES CUSTOMER(cust_id), issue_DATE DATE

);

Table created

1.

a) INSERT INTO CUSTOMER VALUES(‘101’,’john’,’scaria’,’kottayam’,5678890987);

1 row created.

INSERT INTO CUSTOMER VALUES(‘102’,’arjun’,’mohan’,’kollam’,7687654567);

1 row created.

INSERT INTO CUSTOMER VALUES(‘103’,’mahesh’,’kumar’,’kochi’,8907865434);

1 row created.

INSERT INTO CUSTOMER VALUES(‘104’,’libin’,’scaria’,’wayanad’,5678900987);

1 row created.

INSERT INTO CUSTOMER VALUES(‘105’,’manu’,’martin’,’kasargod’,9876543456);

1 row created.

34
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO INVOICE VALUES(‘i1’,’101’,’24 july 2008’);

1 row created.

INSERT INTO INVOICE VALUES(‘i2’,’102’,’17 feb 1999’);

1 row created.

INSERT INTO INVOICE VALUES(‘i3’,’103’,’1 jan 2000’);

1 row created

INSERT INTO INVOICE VALUES(‘i4’,’104’,’28 aug 1999’);

1 row created.

INSERT INTO INVOICE VALUES(‘i5’,’105’,’24 march 2004’);

1 row created.

b)

UPDATE INVOICE set issue_DATE=’15 nov 2022’ WHERE cust_id in(SELECT c.cust_id
FROM CUSTOMER c INNER JOIN INVOICE i on c.cust_id=i.cust_id WHERE
c.cust_id=’101’);

1 row Updated.

2.

a) SELECT i.issue_DATE FROM CUSTOMER c INNER JOIN INVOICE i on c.cust_id


WHERE c.Fname=’john’;

Issue_Date

15 nov 2022

b) . SELECT CUSTOMER.Fname, CUSTOMER.Lname, INVOICE.invo_no FROM


CUSTOMER INNER JOIN INVOICE on INVOICE.cust_id=CUSTOMER.cust_id;

Fname Lname inv_no


John scaria i1

35
Department of Computer Application Kristu Jyoti College of Management &Technology
Arjun mohan i2
Mahesh kumar i3
Libin scaria i4
Manu martin i5

3. CREATE VIEW CUSTOMER as SELECT cust_id,fname,lname,area,phone_no FROM


CUSTOMER;

View is created.

9. Create the following Tables:

CUSTOMER

Column Name Data Type Constraint

Cno Varchar(6) PRIMARY KEY

Cname Varchar(20) NOT NULL

Address Varchar(20)

City Varchar(20)

SALES

Column name Data type Constraint

Orderno Varchar(6) PRIMARY KEY

Orderdate Date __

cno Varchar(6) Foreign key

36
Department of Computer Application Kristu Jyoti College of Management &Technology
Write SQL queries for the following

1.

a) Insert 5 records in each Table

b) Update the address of CUSTOMER “John”

2.

a) Count the Orders placed after 03-march-2012

b) Find the Order number of CUSTOMER ‘Jay’

3. Create a view on CUSTOMER Table

ANSWER

CREATE Table CUSTOMER

Cno VARCHAR(6) PRIMARY KEY,

Cname VARCHAR(6) NOT NULL,

Address VARCHAR(20),

City VARCHAR(20) );

Table created.

CREATE Table SALES

Orderno VARCHAR(6) PRIMARY KEY,

Orderdate DATE ,

Cno VARCHAR(6) REFERENCES CUSTOMER(Cno)

);

37
Department of Computer Application Kristu Jyoti College of Management &Technology
Table Created

a). INSERT INTO CUSTOMER VALUES(‘1’,’John’,’Bogan villa’,’Kottayam’);

1 row created

INSERT INTO CUSTOMER VALUES(‘2’,’Jay’,’Rajangiri villa’,’Kolkata’);

1 row created

INSERT INTO CUSTOMER VALUES(‘3’,’Ram’,’Ione nagar’,’Mumbai’);

1 row created

INSERT INTO CUSTOMER VALUES(‘4’,’Mary’,’MC Street’,’Kolkata’);

1 row created

INSERT INTO CUSTOMER VALUES(‘5’,’Joseph’,’JH Street’,’Kochi’);

1 row created

INSERT INTO SALES VALUES(‘1’,’03-Feb-2025’,’1’);

1 row created

INSERT INTO SALES VALUES(‘2’,’03-Mar-2012’,’2’);

1 row created

INSERT INTO SALES VALUES(‘3’,’03-Aug-2021’,’3’);

1 row created

INSERT INTO SALES VALUES(‘4’,’06-Jan-2020’,’4’);

1 row created

INSERT INTO SALES VALUES(‘5’,’08-Sep-2021’,’5’);

1 row created

38
Department of Computer Application Kristu Jyoti College of Management &Technology
b). UPDATE CUSTOMER SET Address=’Rose Villa’ WHERE Cname=’John’;

1 row Updated

2.

a) SELECT COUNT(Orderno) FROM SALES WHERE Orderdate BETWEEN ’04-


Mar-

2012’ AND ’03-Feb-2025’;

COUNT(ORDERNO)

---------------------------

b. SELECT SALES.Cname FROM SALES INNER JOIN CUSTOMER ON

CUSTOMER.Cno=SALES.Cno WHERE CUSTOMER.Cname=’Jay’;

ORDERNO

---------------

3. CREATE VIEW NEW AS SELECT * FROM CUSTOMER;

View created

39
Department of Computer Application Kristu Jyoti College of Management &Technology
10. Create the following Tables:

EMPLOYEE

COLUMNNAME DATATYPE CONSTRAINT

eno Varchar(5) PRIMARY KEY

name Varchar(30) NOT NULL

designation Varchar(30) Trainee,Team Member,


Team Head,Manager

__
salary Number(7,2)

PROJECT

COLUMN NAME DATATYPE CONSTRAINT

PID Varchar(5) PRIMARY KEY

Proj_Name Varchar(25)

eno Varchar(5) Foreign key

Write SQL queries for the following

1.

a) Insert 5 record in each Tables.

b) UPDATE the designation of the employee named “Albert”.

2.

a) Display the details of the employees in alphabetic Order of the name.

b) List the employee name along with the name of the project assigned to him/her.

3. Create a view on the employee Table.

40
Department of Computer Application Kristu Jyoti College of Management &Technology
CREATE Table employee

eno varchar(5) PRIMARY KEY,

name varchar(30) NOT NULL,

designation varchar(30) check(designation in(‘trainee’,’team member’,’team


head’,’manager’)),

salary number(7,2)

);

INSERT INTO employee VALUES(‘e01’,’anu’,’trainee’,20000);

INSERT INTO employee VALUES(‘e02’,’amal’,’team head’,10000);

INSERT INTO employee VALUES(‘e03’,’ banty’,’ manager’,30000);

INSERT INTO employee VALUES(‘e04’,’priya’,’ team member’,10000);

INSERT INTO employee VALUES(‘e05’,’vimal’,’trainee’,30000);

SELECT * FROM employee;

ENO NAME DESIGNATION SALARY

----- ------------------------------ ------------------------------ ----------

e01 anu trainee 20000

e02 amal team head 10000

e03 banty manager 30000

e04 priya team member 10000

e05 albert trainee 30000

41
Department of Computer Application Kristu Jyoti College of Management &Technology
CREATE Table project

(pid varchar(5) PRIMARY KEY,

projname varchar(25) NOT NULL,

eno varchar(5) references employee(eno));

INSERT INTO project VALUES('p01','abc mngnt system','e01');

INSERT INTO project VALUES('p02','pqr','e02');

INSERT INTO project VALUES('p03','xyz','e03');

INSERT INTO project VALUES('p04','mno','e04');

INSERT INTO project VALUES('p05','qwe','e05');

SELECT * FROM project;

PID PROJNAME ENO

----- ------------------------- ---

p01 abc mngnt system e01

p02 pqr e02

p03 xyz e03

p04 mno e04

p05 qwe e05

42
Department of Computer Application Kristu Jyoti College of Management &Technology
b). UPDATE employee set designation='manager' WHERE name='albert';

ENO NAME DESIGNATION SALARY

----- ------------------------------ ------------------------------ ----------

e01 anu trainee 20000

e02 amal team head 10000

e03 banty manager 30000

e04 priya team member 10000

e05 albert manager 30000

a). SELECT * FROM employee ORDER BY name;

ENO NAME DESIGNATION SALARY


----- ------------------------------ ------------------------------ ----------
e05 albert manager 30000
e02 amal team head 10000
e01 anu trainee 20000
e03 banty manager 30000
e04 priya team member 10000

b). SELECT employee.name,project.projname


FROM employee INNER JOIN
project ON employee.eno=project. eno;

NAME PROJNAME
------------------------------ -------------------------
anu abc mngnt system
amal pqr
banty xyz
priya mno
albert qwe

43
Department of Computer Application Kristu Jyoti College of Management &Technology
3.
CREATE VIEW

employeeview as

SELECT eno,name,designation,salary

FROM employee;

11. Create the following Tables:

EMPLOYEE

COLUMN NAME DATA TYPE CONSTRAINTS

Ecode Varchar(5) PRIMARY KEY

Ename Varchar(30) NOT NULL

Bcode Varchar(10) Foreign key to Branch Table

Grade Number(2) Only value FROM 1 to 10

Dt_jn Date -

salary Number(7,2) -

BRANCH

COLUMN NAME DATA TYPE CONDITION

Bcode Varchar(10) PRIMARY KEY

Bname Varchar(10) NOT NULL

Create SQL Queries for the following:

1.

a) Insert 5 records in each Table

b) UPDATE the grade of the employee with ecode=”E201” to 5

44
Department of Computer Application Kristu Jyoti College of Management &Technology
2.

a) Display all employees whose salary is greater than average salary.

b) Display the ename,bname,grade of all employess

3. Create a view on the EMP Table.

CREATE Table Branch12

(Bcode varchar(10)PRIMARY KEY,

Bname varchar(10)NOT NULL);

Table created.

CREATE Table Emply77

(Ecode varchar(5)PRIMARY KEY,

Ename varchar(30)NOT NULL,

Bcode varchar(10)REFERENCES Branch12(Bcode),

Grade number(2)check((Grade>0)AND(Grade<11)),

Dt_in DATE,salary number(7,2));

Table created.

1. a) INSERT INTO Branch12 VALUES('b201','KOTTAYAM');

INSERT INTO Branch12 VALUES('b202','ERANAKULAM');

INSERT INTO Branch12 VALUES('b203','PALAKKAD');

INSERT INTO Branch12 VALUES('b204','THRISSUR');

INSERT INTO Branch12 VALUES('b205','CALICUT');

INSERT INTO Emply77 VALUES('E201','Naran','b201',10,'22-jan-2016',50000);

45
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO Emply77 VALUES('E202','siddharth','b202',9,'21-nov-2019',45000);

INSERT INTO Emply77 VALUES('E203','aayush','b203',8,'11-feb-2017',30000);

INSERT INTO Emply77 VALUES('E204','vamikha','b204',7,'17-jun-2020',55000);

INSERT INTO Emply77 VALUES('E205','kripha','b205',10,'25-dec-2021',65000);

b. UPDATE Emply77 set Grade=5 WHERE Ecode='E201';

2.a. SELECT * FROM Emply77 WHERE Salary>(SELECT avg(Salary) FROM Emply77);

ECODE ENAME BCODE GRADE DT_IN SALARY

----- ------------------------------ ---------- ---------- --------- ----------

E201 Naran b201 5 22-JAN-16 50000

E204 vamikha b204 7 17-JUN-20 55000

E205 kripha b205 10 25-DEC-21 65000

b. SELECT e.Ename,b.Bname,e.Grade

FROM Emply77 e INNER JOIN

Branch12 b on e.Bcode=b.Bcode;

ENAME BNAME GRADE

------------------------------ ---------- ----------

Naran KOTTAYAM 5

siddharth ERANAKULAM 9

aayush PALAKKAD 8

vamikha THRISSUR 7

kripha CALICUT 10

46
Department of Computer Application Kristu Jyoti College of Management &Technology
3.CREATE VIEW emply_details AS

SELECT Ecode, Ename,Grade,Dt_in,Salary FROM Emply77;

View created

12.Create the following Tables:

Table Name: MOVIE

COLUMN NAME DATA TYPE CONSTRAINTS

Mvno Number(2) PRIMARY KEY

Title Varchar(25) NOT NULL, UNIQUE

Type Varchar(10) Only


comedy,action,hORrOR

Star Varchar(25) __

Price Number(10,2) __

Table name: INVOICE

COLUMN NAME DATATYPE CONDITION

Invno Number(2) PRIMARY KEY

Mvno Number(2) Foreign key

Issue _DATE DATE -

Write SQL queries for the following.

1.a. Insert 5 records in each Table

b. Increase the price of all movies by 10

2.a. List the movies in sorted Order of their title

b. List the title and title for all the movie that are issued

47
Department of Computer Application Kristu Jyoti College of Management &Technology
3.Create a view on the MOVIE Table

CREATE Table movie1

(mvno number(2) PRIMARY KEY,

title varchar(25) NOT NULL UNIQUE,

type varchar(10),check(type in('comedy','action','horror')),

star varchar(25),price number(10,2));

Table created

CREATE Table INVOICE23

(invno number(2) PRIMARY KEY,

mvno number(2) REFERENCES movie1(mvno),

issueDATE DATE);

Table created.

1.a. INSERT INTO movie1 VALUES(11,'aram','horror','dulquer',100);

INSERT INTO movie1 VALUES(12,'niram','comedy','boban',130);

INSERT INTO movie1 VALUES(13,'haram','action','vinay',120);

INSERT INTO movie1 VALUES(14,'kadal','action','mammoty',100);

INSERT INTO movie1 VALUES(15,'kazcha','action','mammoty',200);

INSERT INTO INVOICE23 VALUES(21,11,'23-jan-2004');

48
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO INVOICE23 VALUES(22,12,'2-jan-2005');

INSERT INTO INVOICE23 VALUES(23,13,'12-jun-2010');

INSERT INTO INVOICE23 VALUES(24,14,'11-mar-2020');

INSERT INTO INVOICE23 VALUES(25,15,'10-mar-2007');

b.UPDATE movie1 set price=price+10;

5 rows Updated.

2.a.SELECT * FROM movie1 ORDER BY title;

MVNO TITLE TYPE STAR PRICE


11 aram hORrOR dulquer 160
13 haram action vinay 130
14 kadal action mammoty 140
15 kazhcha action mammoty 210
12 niram comedy comedy 140

b. SELECT movie1.title FROM movie1 join INVOICE23 on


movie1.mvno=INVOICE23.mvno WHERE issueDATE<='23-dec-21';

TITLE

-------------------------

Aram

Haram

Kadal

Kazhcha

Niram

3.CREATE VIEW movie11 as SELECT mvno,title,type,star,price FROM movie1;

49
Department of Computer Application Kristu Jyoti College of Management &Technology
13.Create the following Tables

Table: PERSONS

COLUMN NAME DATA TYPE CONSTRAINT

PID Numeric(5) PRIMARY KEY

Fname Varchar(15) NOT NULL

Lname Varchar(15)

Address Varchar(20)

City Varchar(10)

Table :ORDER

Column name Data type Constraints

OID Numeric(5) PRIMARY KEY

ORDERQTY Numeric(5) NOT NULL

ORDERDATE DATE

PID Numeric(5) Foreign key

Write SQL queries for the following:

1.a.Insert 5 records in each Table

b.Change the Order quantity of the Order No=578

2.a. Display the Order details placed befORe 09-june-2013

b. List the Order details of the CUSTOMER ‘Johnson’

3.Create a view on ORDER Table

CREATE Table person5

(pid number(5) PRIMARY KEY,

50
Department of Computer Application Kristu Jyoti College of Management &Technology
fname varchar(15) NOT NULL,

lname varchar(25),address varchar(25),

city varchar(10));

Table created.

CREATE Table Order5

(oid number(3) PRIMARY KEY,

Orderqty number(4),Orderdate DATE,

ordid number(5) REFERENCES person5(pid));

Table created.

1.a. INSERT INTO person5 VALUES(11,'achu','sugu','rosevilla','kochi');

INSERT INTO person5 VALUES(12,'sachu','surya','pinkvilla','kollam');

INSERT INTO person5 VALUES(13,'johnson','jithu','greenvilla','kannur');

INSERT INTO person5 VALUES(14,'varun','sasi','happyvilla','eroOR');

INSERT INTO person5 VALUES(15,'midhun','pillai','starvilla','kumily');

INSERT INTO Order5 VALUES (31,5,'24-feb-2001’,11);

INSERT INTO Order5 VALUES (32,7,'27-mar-2004',12);

INSERT INTO Order5 VALUES (33,6,'11-sep-2007',13);

INSERT INTO Order5 VALUES (34,10,'04-oct-2014',14);

INSERT INTO Order5 VALUES (578,10,'09-jun-2013',15);

b. UPDATE Order5 set Orderqty=200 WHERE oid=578;

2.a.SELECT * FROM Order5 WHERE Orderdate<='09-jun-2013';

51
Department of Computer Application Kristu Jyoti College of Management &Technology
OID ORDERQTY ORDERDATE ORDID

---------- ---------- --------- ----------

31 5 24-FEB-01 11

32 7 27-MAR-04 12

33 6 11-SEP-07 13

578 200 09-JUN-13 15

b. SELECT Order5.oid,Order5.Orderqty,Order5.Orderdate

FROM Order5 join person5 on order5.ORdid=person5.pid

WHERE person5.fname='johnson';

OID ORDERQTY ORDERDATE

---------- ---------- ---------

33 6 11-SEP-07

3.CREATE VIEW order11 as

SELECT odid,orderqty,orderdate,

ordid from orders5;

52
Department of Computer Application Kristu Jyoti College of Management &Technology
14.Create the following Tables

Table : CUSTOMER

COLUMN NAME DATA TYPE CONSTRAINTS

Cust_id Varchar(10) PRIMARY KEY

Cust_name Varchar(25) NOT NULL

Acc_No Number(10) Foreign Key

Address Varchar(25)

Phone_No Number(10)

Table : ACCOUNT

COLUMN NAME DATA TYPE CONSTRAINTS

Acc_No Number(10) PRIMARY KEY

Amount Number(10,2)

Write SQL queries for the following:

1.a.Insert 5 records in each Table

b. Change the address of CUSTOMER with Cust_id “C345”

2.a.Find the CUSTOMER details who have the highest balance in account

b. Sort the CUSTOMER Table on ascending Order of Acc_No

3.Create a view on CUSTOMER Table

CREATE Table account

(acc_no number(10) PRIMARY KEY,amount number(10,2));

Table created

CREATE Table CUSTOMER

53
Department of Computer Application Kristu Jyoti College of Management &Technology
(cust_id varchar(10) PRIMARY KEY,

cust_name varchar(25) NOT NULL,

acc_no number(10) REFERENCES account(acc_no),

address varchar(25),phone_no number(10));

Table created

INSERT INTO account VALUES(4598,20000);

INSERT INTO account VALUES(2894,5000);

INSERT INTO account VALUES(3482,550);

INSERT INTO account VALUES(1582,7800);

INSERT INTO account VALUES(8469,80000);

INSERT INTO CUSTOMER VALUES(‘c125’,’Ajal’,2894,’Mampra’,8914576310);

INSERT INTO CUSTOMER VALUES(‘c345’,’Amal’,1582,’Nilayam’,9249659348);

INSERT INTO CUSTOMER VALUES(‘c592’,’Diya’,5263,’Thayyil’,8429626583);

INSERT INTO CUSTOMER VALUES(‘c125’,’Riya’,8469,’Manakal’,9238462492);

INSERT INTO CUSTOMER VALUES(‘c248’,’Nila’,4598,’Chirayil’,8089424184);

b.UPDATE CUSTOMER SET address=’plamootil’ WHERE cust_id=’c345’;

1 row updated

2a. SELECT c.cust_id,c.cust_name,c.acc_no,

c.address,c.phone_no FROM CUSTOMER c

innerjoin account a on c.acc_no=a.acc_no WHERE a.acc_no in

(SELECT max(amount)FROM account);

54
Department of Computer Application Kristu Jyoti College of Management &Technology
Cust_id cust_name acc_no address phone_no

C463 Riya 8469 Manakal 9238462492

b.SELECT * FROM CUSTOMER ORDER BY acc-no;

Cust_id cust_name acc_no address phone_no

C345 Amal 1582 Nilayam 9249659348

C123 Ajal 2894 Mampra 8914576310

C248 Nila 4598 Chirayil 8089424184

C592 Diya 5263 Thayyil 8429626583

C463 Riya 8469 Manakal 9238462492

3.CREATE VIEW new

As SELECT * FROM CUSTOMER;

View created

55
Department of Computer Application Kristu Jyoti College of Management &Technology
15. CREATE Tables with appropriate structure to maintain infORmation about a set of books
in a library AND a possible set of CUSTOMERs.. then write needed queries fOR

1. Inserting new records into the Tables


2. Display books of type “novels” in different languages
3. Display all the details of books whose authOR name starts with “Adams”
4. List of CUSTOMERs in the decreasing Order of dues to be paid

CREATE Table library

(book_id varchar (5) PRIMARY KEY,

title varchar (25), authOR varchar (25),

type varchar (25), language varchar (25));

Table created

CREATE Table CUSTOMER_lib

(CUSTOMER_id varchar (25),

book_id varchar (5) REFERENCES library(book_id),

due_DATE DATE);

Table created

1.INSERT INTO library VALUES(‘b101’,’Smruthy’,’Perumpadam’,’Novel’,’Malayalam’);

INSERT INTO library VALUES (‘b102’,’3 Idiots’,’Chetan Bhagath’, ‘Novel’,’English’);

INSERT INTO library VALUES (‘b104’,’The Tears’,’Adams Robbert’,’Fiction’,’English’);

INSERT INTO library VALUES (‘b105’,’2 States’, ’Chetan Bhagath’,’Novel’,’English’);

INSERT INTO library VALUES (‘b106’,’Fast Cook’,’Adams James’,’Cookery’);

INSERT INTO CUSTOMER_lib VALUES (‘c101’,’b101’,’12-Dec-2021’);

INSERT INTO CUSTOMER_lib VALUES (‘c102’,’b102’,’10-Dec-2021’);

INSERT INTO CUSTOMER_lib VALUES (‘c105’,’b105’,’20-Dec-2021’);

INSERT INTO CUSTOMER_lib VALUES (‘c108’,’b104’,’7-Dec-2021’);

INSERT INTO CUSTOMER_lib VALUES (‘c112’,’b104’,’22-Dec-2021’);

2.SELECT * FROM library WHERE type=’Novel’;

56
Department of Computer Application Kristu Jyoti College of Management &Technology
BOOK_ID TITLE AUTHOR LANGUAGE
b101 Smruthy Perumpadam Malayalam
b102 3 Idiots Chetan Bhagath English
b105 2 States Chetan Bhagath English

3.SELECT * FROM library WHERE authOR LIKE ‘Adams%’;

BOOK_ID TITLE AUTHOR LANGUAGE


b104 The Tears Adams Robbert English
b106 Fast Cook Adams James English

4.SELECT * FROM CUSTOMER_lib ORDER BY due_DATE DESC;

CUSTOMER_ID BOOK_ID DUE_DATE


c112 b104 22-Dec-2021
c105 b105 20-Dec-2021
c101 b101 12-Dec-2021
c102 b102 10-Dec-2021
c108 b104 7-Dec-2021

57
Department of Computer Application Kristu Jyoti College of Management &Technology
16. CREATE Tables with appropriate structure to store information about a number of items
to be kept in stock in a provision store and the details about the sales on a given day. Then
write the needed queries to

1. Inserting meaningful data into Tables

2. Displaying the items in stock category wise

3. Details of sales made on a given day item wise

4. Display the sales bill with maximum amount on that day.

CREATE Table Stock

(Stock_ID varchar(6) PRIMARY KEY,Category varchar(25),

Item_Name varchar(20),Price Number(7,2));

Table created.

CREATE Table sales

(Sales_ID varchar(6) PRIMARY KEY,Item_Name varchar(20),

Qty Number(10),Stock_ID varchar(6) REFERENCES Stock,

SDATE DATE,Amount Number(7,2));

Table created.

1. INSERT INTO stock VALUES('S01','Stationary','Pen',10);

INSERT INTO stock VALUES('S02','Stationary','pencil',15);

INSERT INTO stock VALUES('S03','VegeTables','Tomato',20);

INSERT INTO stock VALUES('S04','VegeTables','ladiesfinger',25);

INSERT INTO stock VALUES('S05','Fruits','Pineapple',30);

SELECT * FROM Stock;

58
Department of Computer Application Kristu Jyoti College of Management &Technology
STOCK_ CATEGORY ITEM_NAME PRICE

------ ------------------------- -------------------- ----------

S01 Stationary Pen 10

S02 Stationary pencil 15

S03 VegeTables Tomato 20

S04 VegeTables ladiesfinger 25

S05 Fruits Pineapple 30

INSERT INTO sales VALUES('S001','PEN',10,'S01','10/feb/19',100);

INSERT INTO sales VALUES('S002','PENCIL',20,'S02','20/MAR/19',200);

INSERT INTO sales VALUES('S003','TOMATO',20,'S03','10/feb/19',50);

SELECT * FROM Sales;

SALES_ID ITEM_NAME QTY STOCK_ID SDATE AMOUNT

------ -------------------- ---------- ------ --------- ----------

S001 PEN 10 S01 10-FEB-19 100

S002 PENCIL 20 S02 20-MAR-19 200

S003 TOMATO 20 S03 10-FEB-19 50

2.SELECT Item_Name,Category FROM Stock;

ITEM_NAME CATEGORY

-------------------- -------------------------

Pen Stationary

pencil Stationary

Tomato VegeTables

59
Department of Computer Application Kristu Jyoti College of Management &Technology
ladiesfinger VegeTables

Pineapple Fruits

3.SELECT * FROM sales WHERE SDATE='10/feb/19' ORDER BY Item_Name ASC;

SALES_ID ITEM_NAME QTY STOCK_ID SDATE AMOUNT

------ --------------- ----- ------------- --------- ----------

S001 PEN 10 S01 10-FEB-19 100

S003 TOMATO 20 S03 10-FEB-19 504

4..SELECT * FROM sales WHERE

amount=(SELECT MAX(Amount) FROM

sales WHERE SDATE='10/feb/19');

SALES_ID ITEM_NAME QTY STOCK_ID SDATE AMOUNT

------------ -------------- ------ ---------- -------- ----------

S001 PEN 10 S01 10-FEB-19 100

60
Department of Computer Application Kristu Jyoti College of Management &Technology
17.Create the following Tables:

Table :BOOK

COLUMN NAME DATA TYPE CONSTRAINTS

BookID Varchar(6) PRIMARY KEY

BookName Varchar(15) NOT NULL

AuthOR_Name Varchar(15)

Publisher Varchar(20)

Unit_price Number(8,2) NOT NULL,cannot


be zero

Quantity Number(3)

Table: ISSUE

COLUMN NAME DATA TYPE CONSTRAINTS

Issue_ID Varchar(6) PRIMARY KEY

BookId Varchar(6) Foreign Key

Qty_Issued Number(3)

Write SQL queries fOR the following:

1.a.Insert 5 records in each Table

b.Increase the quantity of the book with name “Data Structures” by 10

2.a.Find the number of UNIQUE publishers

b.Display the BookId,Book Name AND Quantity issued fOR all books which are not issued

3.Create a View on the BOOK Table

CREATE Table Book(BookID varchar(6) PRIMARY KEY,

61
Department of Computer Application Kristu Jyoti College of Management &Technology
Book Name varchar(15) NOT NULL, AuthOR_Name varchar(15),

Publisher varchar(20),Unit _price Number(8,2) NOT NULL, Quantity Number (3));

Table created

INSERT INTO Book VALUES(101,’Cpgming’,’Balaguruswamy’,’Alex’,520,5);

INSERT INTO Book VALUES(102,’C++’,’Balaguruswamy’,’Gopal’,420,3);

INSERT INTO Book VALUES(103,’DataStructure’,’Mohan’,’Alex’,500,5);

INSERT INTO Book VALUES(104,’Software’,’Alex’,’Madhav’,300,8);

INSERT INTO Book VALUES(105,’Java’,’Jacob’,’Saji’,800,6);

SELECT * FROM Book;

BOOKID BOOKNAME AUTHOR_NAME PUBLISHER UNIT_PRICE QUANTITY

---------- ---------------- --------------- --------------- --------------- -----------

101 Cpgming Balaguruswamy Alex 520 5

102 C++ Balaguruswamy Gopal 420 3

103 DataStructure Mohan Alex 500 5

104 Software Alex Madhav 300 8

105 Java Jacob Saji 800 6

CREATE Table Issue(Issue_ID varchar(6) PRIMARY KEY,

Book Id varchar(6) REFERENCES Book(BookID),

Qty_issued Number(3)) ;

INSERT INTO Issue VALUES(1,101,3);

INSERT INTO Issue VALUES(2,102,4);

INSERT INTO Issue VALUES(3,101,5);

INSERT INTO Issue VALUES(4,102,2);

INSERT INTO Issue VALUES(5,101,1);

62
Department of Computer Application Kristu Jyoti College of Management &Technology
SELECT * FROM Issue;

ISSUE_ID BOOKID QTY_ISSUED

------ -------- ----------

1 101 3

2 102 4

3 101 5

4 102 2

5 101 1

1.b UPDATE Book set Quantity='10' WHERE BookName='DataStructure';

1 row Updated.

2.aSELECT DISTINCT Publisher FROM Book;

PUBLISHER

--------------------

Saji

Alex

Gopal

Madhav

2.b SELECT Book.BookID,Book.BookName

,Issue.Qty_Issued FROM Book left Join Issue

on Book.BookID=Iss

ue.BookID WHERE Book.BookID NOT IN

(SELECT BookID FROM Issue);

63
Department of Computer Application Kristu Jyoti College of Management &Technology
BOOKID BOOKNAME QTY_ISSUED

------ --------------- ----------

105 Java 3

103 DataStructure 6

104 Software 5

3).CREATE VIEW Bookpro As

SELECT Bookid,Bookname,AuthORname,

Publisher,Unit_Price, Quantity FROM Book

64
Department of Computer Application Kristu Jyoti College of Management &Technology
18. Create the following Tables:

Table NAME: CUSTOMER

Column Name Data Type Constraint

Cno Varchar(6) PRIMARY KEY

Cname Varchar(20) NOT NULL

Address Varchar(20)

City Varchar(20)

Table NAME :SALES

Column name Data type Constraint

Orderno Varchar(6) PRIMARY KEY

Orderdate DATE __

cno Varchar(6) Foreign key

Write SQL queries fOR the following

1.a. Insert 5 records in each Table

b.UPDATE the address of CUSTOMER with Cno=’C503’

2.a. Count the city wise number of CUSTOMERs

b.Find the CUSTOMER details with Order no=’O234’

3.Create a view on CUSTOMER Table

1.aCREATE Table CUSTOMER(cno varchar(6) PRIMARY KEY,

cname varchar(20) NOT NULL,address varchar(20) ,city varchar(20));

Table created.

65
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO CUSTOMER VALUES('c500','Ramkumar','Ram Nivas','kottayam');

INSERT INTO CUSTOMER VALUES('c501','Reshma','Reshmas villa','kottayam');

INSERT INTO CUSTOMER VALUES('c502','Abhinav','karanattu House','Eranakulam');

INSERT INTO CUSTOMER VALUES('c503','Achu','Sree ragam','mumbai');

INSERT INTO CUSTOMER VALUES('c504','Ashwin','chaithram house','Kottayam');

SELECT * FROM CUSTOMER;

CNO CNAME ADDRESS CITY

------ -------------------- -------------------- --------------------

c500 Ramkumar Ram Nivas kottayam

c501 Reshma Reshmas villa kottayam

c502 Abhinav karanattu House Eranakulam

c503 Achu Sree ragam mumbai

c504 Ashwin chaithram house Kottayam

CREATE Table sales(Order_no varchar(6) PRIMARY KEY,

Orderdate DATE,cno varchar(6) REFERENCES CUSTOMER(cno));

Table created.

INSERT INTO sales VALUES('o231','22-nov-2002','c500');

INSERT INTO sales VALUES('o232','13-march-2000','c501');

INSERT INTO sales VALUES('o233','28-october-2003','c502');

INSERT INTO sales VALUES('o234','14-april-2003','c503');

INSERT INTO sales VALUES('o235','20-october-1999','c504');

SELECT * FROM sales;

66
Department of Computer Application Kristu Jyoti College of Management &Technology
ORDER ORDERDATE CNO

------ --------- ------

o231 22-NOV-02 c500

o232 13-MAR-00 c501

o233 28-OCT-03 c502

o234 14-APR-03 c503

o235 20-OCT-99 c504

b. UPDATE CUSTOMER set address='pulimoottil' WHERE cno='c503';

1 row Updated.

SELECT * FROM CUSTOMER;

CNO CNAME ADDRESS CITY

------ -------------------- -------------------- --------------------

c500 Ramkumar Ram Nivas kottayam

c501 Reshma Reshmas villa kottayam

c502 Abhinav karanattu House Eranakulam

c503 Achu pulimoottil mumbai

c504 Ashwin chaithram house Kottayam

2.a SELECT count(city) FROM CUSTOMER GROUP BY 'city';

COUNT(CITY)

-----------

b.SELECT CUSTOMER.cno,CUSTOMER.cname,

CUSTOMER.address,CUSTOMER.city,sales.Order_no FROM

67
Department of Computer Application Kristu Jyoti College of Management &Technology
CUSTOMER left join sales on CUSTOMER.cno=sales.cno

WHERE sales.Order_no='o234';

CNO CNAME ADDRESS CITY ORDER_

------ -------------------- -------------------- -------------------- ------

c503 Achu pulimoottil mumbai o234

3. CREATE VIEW cust_sale AS SELECT cno,cname,address,city FROM CUSTOMER

19.Create the following Tables:

Table : STUDENT

COLUMN NAME DATA TYPE CONSTRAINTS

Roll_no Number(5) PRIMARY KEY

Name Varchar(20) NOT NULL

Dept_id Number(4) Foreign Key

Gender Varchar(2)

Contact_no Number(10)

Table : DEPARTMENT

COLUMN NAME DATA TYPE CONSTRAINTS

Dept_id Number(4) PRIMARY KEY

Dept_name Varchar(10) NOT NULL

Write SQL queries fOR the following

1.a. Insert 5 records in each Table

b. UPDATE the department name of deptid=103 to “ELECTRONICS”

68
Department of Computer Application Kristu Jyoti College of Management &Technology
2.a. Display the departments names in its alphabetical Order.

b. Display the details of STUDENTs in COMPUTER APPLICATION Department

3.Create a view FROM STUDENT Table

CREATE Table Department

(Dept_id number(4) PRIMARY KEY,

Dept_name varchar(10) NOT NULL);

Table created.

CREATE Table stud2(rollno number(5) PRIMARY KEY,

name varchar (20) NOT NULL, Dept_id number(4) REFERENCES

Department(Dept_id),gender varchar(2), contact_no number(10));

Table created.

1.a INSERT INTO Department VALUES(101,'comp sci');

INSERT INTO Department VALUES(102,'mcom');

INSERT INTO Department VALUES(103,'geology');

INSERT INTO Department VALUES(104,'comp app');

INSERT INTO Department VALUES(105,'commerce');

SELECT * FROM Department;

DEPT_ID DEPT_NAME

---------- ----------

101 comp sci

102 mcom

103 geology

104 comp app

105 commerce

69
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO stud2 VALUES(1,'anu',101,'f',9570602122) ;

INSERT INTO stud2 VALUES(2,'appu',102,'m',8021345633);

INSERT INTO stud2 VALUES(3,'linta',103,'f',8067543218);

INSERT INTO stud2 VALUES(4,'athul',104,'m',9765432190);

INSERT INTO stud2 VALUES(5,'athulya',105,'f',8212345678);

SELECT * FROM stud2;

ROLLNO NAME DEPT_ID GENDER CONTACT_NO

---------- --------- ----------- ---------- ------------

1 anu 101 f 9570602122

2 appu 102 m 8021345633

3 linta 103 f 8067543218

4 athul 104 m 9765432190

5 athulya 105 f 8212345678

b. UPDATE Department set Dept_name='electro' WHERE Dept_id=103;

1 row Updated.

2.a.SELECT Dept_name FROM Department ORDER BY Dept_name;

DEPT_NAME

----------

commerce

comp app

comp sci

electro

mcom

70
Department of Computer Application Kristu Jyoti College of Management &Technology
b. SELECT s.rollno, s.name, s.gender, s.contact_no,

d.Dept_id FROM stud2 s INNER JOIN Department d on

s.Dept_id=d.Dept_id WHERE d.Dept_name='comp app';

ROLLNO NAME GENDER CONTACT_NO DEPT_ID

---------- ---------- ---------- ---------- ----------

4 athul m 9765432190 104

3.CREATE VIEW STUDENT_view AS SELECT * FROM STUDENT;

20. Create the following Tables:

Table NAME: EMPLOYEE

COLUMNNAME DATATYPE
CONSTRAINT

eno Varchar(5) PRIMARY KEY

name Varchar(30) NOT NULL

designation Varchar(30) Trainee,Team


Member, Team
Head,Manager

salary Number(7,2) __

Table NAME: PROJECT

COLUMN NAME DATATYPE CONSTRAINT

PID Varchar(5) PRIMARY KEY

Proj_Name Varchar(25)

eno Varchar(5) Foreign key

71
Department of Computer Application Kristu Jyoti College of Management &Technology
Write SQL queries for the following

1. Insert 5 record in each Tables

b. Increase the salary of all employees by 1500

2. a. Display the details of employee whose second letter of name is “a”

b. List the employee names AND designation who is not assigned a project

3. Create a view on the EMPLOYEE Table.

CREATE Table employee (eno VARCHAR(5) PRIMARY KEY,

name VARCHAR(30) NOT NULL, designation VARCHAR(30), salary Number(7,2))

CREATE Table project (Pid VARCHAR(5) PRIMARY KEY,

Project_name VARCHAR(25),

Eno VARCHAR(5) REFERENCES employee(eno));

1.

INSERT INTO employee VALUES(‘e01’,’john’,’trainee’,5000);

1 row created.

INSERT INTO employee VALUES(‘e02’,’sam’,’team member’,5800);

1 row created.

INSERT INTO employee VALUES(‘e03’,’aby’,’team head’,8000);

1 row created.

INSERT INTO employee VALUES(‘e04’,’joji’,’manager’,10000);

1 row created.

INSERT INTO employee VALUES(‘e05’,’rose’,’assistant’,8500);

1 row created.

72
Department of Computer Application Kristu Jyoti College of Management &Technology
INSERT INTO project VALUES(‘p01’,’tourism management’,’e01’);

1 row created

INSERT INTO project VALUES(‘p02’,’hospital management’,’e02’);

1 row created.

INSERT INTO project VALUES(‘p03’,’null’,’e03’);

INSERT INTO project VALUES(‘p04’,’null’,’e04’);

1 row created.

INSERT INTO project VALUES(‘p05’,’null’,’e05’);

1 row created

4.(b)

UPDATE employee set salary=salary+1500;

5 rows Updated.

5.(a)

SELECT * FROM employee WHERE name LIKE’_a%’;

Eno name designation salary


E02 sam team member 7300

73
Department of Computer Application Kristu Jyoti College of Management &Technology
5.(b)

SELECT employee.name, employee.designation FROM employee INNER JOIN project on


project.eno=employee.eno WHERE project_name=’null’;

Name designation

Aby team head

Joji manager

Rose assistant

6.

CREATE VIEW employee as SELECT eno, name, designation, salary FROM employee;

View is created.

I. 21 Create the following Tables:

Table Name: MOVIE

COLUMN NAME DATA TYPE CONSTRAINTS

Mvno Number(2) PRIMARY KEY

Title Varchar(25) NOT NULL, UNIQUE

Type Varchar(10) Only


comedy,action,hORrOR

Star Varchar(25) __

Price Number(10,2) __

74
Department of Computer Application Kristu Jyoti College of Management &Technology
Table name: INVOICE

COLUMN NAME DATATYPE CONDITION

Invno Number(2) PRIMARY KEY

Mvno Number(2) Foreign key

Issue _DATE DATE -

Write SQL queries fOR the following.

1. a. Insert 5 records in each Table

b.Change the star details of the movie with movie_no =12

a. a. Calculate the average price of each movie type

b. Display the Movie name AND type of movies that are not issued

2. Create a view on the MOVIE Table

SQL> CREATE Table Movie77(Mvno number(2) PRIMARY KEY,Title varchar(25)NOT


NULL UNIQUE,Type varchar(10)check(type in('co

medy','action','hORrOR')),Star varchar(25),Price number(10,2));

Table created.

SQL> CREATE Table INVOICE67(Invno number(2)PRIMARY KEY,Mvno


number(2)REFERENCES Movie77(Mvno),Issue_DATE DATE);

Table created.

SQL> INSERT INTO Movie77 VALUES(10,'cid moosa','comedy','dileep',180);

75
Department of Computer Application Kristu Jyoti College of Management &Technology
1 row created.

SQL> INSERT INTO Movie77 VALUES(11,'cold case','hORrOR','prithviraj',200);

1 row created.

SQL> INSERT INTO Movie77 VALUES(12,'commissioner','action','suresh gopi',300);

1 row created.

SQL> INSERT INTO Movie77 VALUES(13,'manu uncle','comedy','mammooty',250);

1 row created.

SQL> INSERT INTO Movie77 VALUES(14,'maraykar','action','mohanlal',300);

1 row created.

SQL> INSERT INTO Movie77 VALUES(16,'vellinakshtram','hORrOR','prithvi',220);

1 row created.

SQL> INSERT INTO INVOICE67 VALUES(11,10,'22-jan-1998');

1 row created.

SQL> INSERT INTO INVOICE67 VALUES(12,11,'23-feb-2001');

76
Department of Computer Application Kristu Jyoti College of Management &Technology
1 row created.

SQL> INSERT INTO INVOICE67 VALUES(13,12,'12-mar-2010');

1 row created.

SQL> INSERT INTO INVOICE67 VALUES(14,13,'14-apr-2019');

1 row created.

SQL> INSERT INTO INVOICE67 VALUES(15,14,'26-may-2021');

1 row created.

b. SQL> INSERT INTO INVOICE67 VALUES(11,10,'22-jan-1998');

1 row created.

SQL> INSERT INTO INVOICE67 VALUES(12,11,'23-feb-2001');

1 row created.

SQL> INSERT INTO INVOICE67 VALUES(13,12,'12-mar-2010');

1 row created.

77
Department of Computer Application Kristu Jyoti College of Management &Technology
SQL> INSERT INTO INVOICE67 VALUES(14,13,'14-apr-2019');

1 row created.

SQL> INSERT INTO INVOICE67 VALUES(15,14,'26-may-2021');

1 row created.

b. SQL> UPDATE Movie77 set Star='prithvi' WHERE Mvno=12;

1 row Updated.

a.a SQL> SELECT avg(Price)FROM Movie77 GROUP BY Type;

AVG(PRICE)

----------

300

215

200

b. SQL> SELECT Movie77.Title,Movie77.Type FROM Movie77 left join INVOICE67 on


Movie77.Mvno=INVOICE67.Mvno WHERE Movie77.Mvn

o NOT IN(SELECT Mvno FROM INVOICE67);

TITLE TYPE

------------------------- ----------

vellinakshtram hORrOR

78
Department of Computer Application Kristu Jyoti College of Management &Technology
5.SQL>CREATE VIEW movie_details As SELECT Mvno,Title,Type,Star,Price FROM
Movie77;

View created

II. 22. Create the following Tables:

Table name: EMPLOYEE

COLUMN NAME DATA TYPE CONSTRAINTS

Ecode Varchar(5) PRIMARY KEY

Ename Varchar(30) NOT NULL

Bcode Varchar(10) Foreign key to Branch


Table

Grade Number(2) Only value FROM 1 to 10

Dt_jn DATE -

salary Number(7,2) -

Table name: BRANCH

COLUMN NAME DATA TYPE CONDITION

Bcode Varchar(10) PRIMARY KEY

Bname Varchar(10) NOT NULL

Create SQL Queries fOR the following:

4. a. Insert 5 records in each Table

b. UPDATE the grade of the employee with ecode=”e201” to 5

5. a.Display the maximum AND minimum salary of each grade of employees

b. Display the details of employees in the KOTTAYAM branch

6. Create a view on the EMP Table

79
Department of Computer Application Kristu Jyoti College of Management &Technology
Table: BRANCH

SQL> CREATE Table Branch(Bcode varchar(10) PRIMARY KEY,Bname varchar(10) NOT


NULL);

Table created.

4.a)

SQL> INSERT INTO Branch VALUES('B02','Kollam');

1 row created.

SQL> INSERT INTO Branch VALUES('B04','Alappuzha');

1 row created.

SQL> INSERT INTO Branch VALUES('B05','Kottayam');

1 row created.

SQL> INSERT INTO Branch VALUES('B06','Iduki');

row created.

SQL> INSERT INTO Branch VALUES('B13','Kannur');

1 row created.

SQL> SELECT * FROM Branch;

BCODE BNAME

---------- ----------

B02 Kollam

B04 Alappuzha

B05 Kottayam

B06 Iduki

B13 Kannur

80
Department of Computer Application Kristu Jyoti College of Management &Technology
Table: EMPLOYEE

SQL> CREATE Table Employee(Ecode varchar(5)PRIMARY KEY,Ename varchar(30)


NOT NULL, Bcode varchar(10) REFERENCES Branch,Grade number(2)
CHECK((Grade>0)AND(Grade<11)), Dt_jn DATE,Salary number(7,2));

Table created.

4.a)

SQL> INSERT INTO Employee VALUES('E100','Kiran','B02',5,'03-jan-2016',30000);

1 row created.

SQL> INSERT INTO Employee VALUES('E201','Vinu','B04',8,'25-FEB-2018',50000);

1 row created.

SQL> INSERT INTO Employee VALUES('E300','Maya','B13',7,'02-MAR-2021',40000);

1 row created.

SQL> INSERT INTO Employee VALUES('E304','Abhijith','B04',4,'17-JUNE-2014',65000);

1 row created.

SQL> INSERT INTO Employee VALUES('E402','SreeRaj','B05',9,'4-DEC-2020',35000);

1 row created.

SQL> SELECT * FROM Employee;

ECODE ENAME BCODE GRADE DT_JN SALARY

----- ------------------------------ ---------- ---------- --------- ----------

E100 Kiran B02 5 03-JAN-16 30000

E201 Vinu B04 8 25-FEB-18 50000

E300 Maya B13 7 02-MAR-21 40000

81
Department of Computer Application Kristu Jyoti College of Management &Technology
E304 Abhijith B04 4 17-JUN-14 65000

E402 SreeRaj B05 9 04-DEC-20 35000

4. b)

SQL> UPDATE Employee set Grade=5 WHERE Ecode='E201';

1 row Updated.

SQL> SELECT * FROM Employee;

ECODE ENAME BCODE GRADE DT_JN SALARY

----- ------------------------- ---------- ---------- --------- ------

E100 Kiran B02 5 03-JAN-16 30000

E201 Vinu B04 5 25-FEB-18 50000

E300 Maya B13 7 02-MAR-21 40000

E304 Abhijith B04 4 17-JUN-14 65000

E402 SreeRaj B05 9 04-DEC-20 35000

5.a)

SQL> SELECT MAX(Salary)FROM Employee GROUP BY Grade;

MAX(SALARY)

-----------

50000

65000

40000

35000

82
Department of Computer Application Kristu Jyoti College of Management &Technology
5.a)

SQL> SELECT MIN(Salary)FROM Employee GROUP BY Grade;

MIN(SALARY)

-----------

30000

65000

40000

35000

5.b)

SQL> SELECT Employee.Ecode, Employee.Ename, Employee. Bcode, Employee.Grade,


Employee.Dt_jn, Employee.Salary,Branch.Bname

FROM Employee LEFT JOIN Branch on Employee.Bcode=Branch.Bcode;

ECODE ENAME BCODE GRADE DT_JN SALARY BNAME

----- ---------------- ---------- ---------- --------- --------- ----------

E100 Kiran B02 5 3-JAN-16 30000 Kollam

E304 Abhijith B04 4 17-JUN-14 65000 Alappuzha

E201 Vinu B04 5 25-FEB-18 50000 Alappuzha

E402 SreeRaj B05 9 04-DEC-20 35000 Kottayam

E300 Maya B13 7 02-MAR-21 40000 Kannur

5 rows SELECTed.

6)

SQL> CREATE VIEW EMP_VIEW as SELECT * FROM Employee;

View created

83
Department of Computer Application Kristu Jyoti College of Management &Technology
23. Create the following Tables

Table : CUSTOMER

COLUMN NAME DATA TYPE CONSTRAINTS

Cust_id Varchar(10) PRIMARY KEY

Cust_name Varchar(25) NOT NULL

Acc_No Number(10) Foreign Key

Address Varchar(25)

Phone_No Number(10)

Table : ACCOUNT

COLUMN NAME DATA TYPE CONSTRAINTS

Acc_No Number(10) Foreign Key

Amount Number(10,2)

Write SQL queries fOR the following:

1. a.Insert 5 records in each Table

b. Change the phone number of CUSTOMER with name=”Anjali”

2. a.Display the acc_no, name, address of CUSTOMERs whose name starts with ‘L’

b.Find the CUSTOMER details who have the lowest balance in account

3. Create a view on CUSTOMER Table

4 a. Insert 5 recORd in each Table

84
Department of Computer Application Kristu Jyoti College of Management &Technology
● INSERT INTO CUSTOMER
VALUES(‘c101’,’Ramesh’,23456782,’kottayam’,9976453213);

1 row created

● INSERT INTO CUSTOMER


VALUES(‘c102’,’lalappan’,4567892,’alappuzha’,8045673212);

1 row created

● INSERT INTO CUSTOMER VALUES


(‘c103’,’anjali’,1234123,’thiruvalla’,9012345618);

1 row created

● INSERT INTO CUSTOMER


VALUES(‘c104’,’gopalan’,4554454,’ambalappuzha’,9020345618);

1 row created

● INSERT INTO CUSTOMER VALUES


(‘c105’,’kumaran’,2343125,’kollam’,8978654532);

1 row created

 SELECT * FROM the CUSTOMER;

Cust_id Cust_name Acc_no Address Phone_no


C101 Ramesh 23456782 Kottayam 9974532313
C102 Lalappan 4567892 Alappuzha 8045673212
C103 Anjali 1234123 Thiruvalla 7867543412
C104 Gopalan 4554454 Ambalapuzha 9020345618
C105 Kumaran 234125 Kollam 8978654532

85
Department of Computer Application Kristu Jyoti College of Management &Technology
b. Change the phone number of the CUSTOMER with the name=”Anjali”.

:UPDATE CUSTOMER SET phone_no=9675643213 WHERE


cust_name=’Anjali’;

1 row Updated

 SELECT * FROM CUSTOMER;

Cust_id Cust_name Acc_no Address Phone_no


C101 Ramesh 23456782 Kottayam 9976433213
C102 Lalappan 4567892 Alappuzha 8045673213
C103 Anjali 1234123 Thiruvalla 9675643213
C104 Gopalan 4554454 Ambalapuzha 9020345618
C105 Lumaran 234125 Kollam 8978654532

5a. Display the acc_no ,name ,address of CUSTOMER whose name starts with
‘L’.

SELECT acc_no, cust_name,address FROM CUSTOMER WHERE


cust_name LIKE ‘L’;

Acc_no Cust_name Address


4567892 Lalappan Alappuzha

b. find the CUSTOMER details who have the lowest balance in account.

SELECT * FROM CUSTOMER WHERE acc_no=( SELECT acc_no FROM


ACCOUNT WHERE amount =( SELECT MIN(amount) FROM ACCOUNT));

Cust_id Cust_name Acc_no Address Phone_no


C103 Anjali 1234123 Thiruvalla 787543412

86
Department of Computer Application Kristu Jyoti College of Management &Technology
6a. create a view on CUSTOMER Table

CREATE VIEW cust_name as SELECT cust_id, cust_name,acc_no,


address,phone_no.

View created.

24. 24. Create the following Tables:

Table : PRODUCT

COLUMN NAME DATA TYPE CONSTRAINTS


Product_no Varchar(6) PRIMARY KEY
Product_name Varchar(15) NOT NULL
Qty_in_hAND Number(8)
Unit_price Number(8,2) NOT NULL,cannot be zero

Table : SALES

COLUMN NAME DATA TYPE CONSTRAINTS


Order_no Varchar(6) PRIMARY KEY
Qty_Ordered Number(8)
Product_no Varchar(6) Foreign Key

CREATE Table product (Product_no varchar(6) PRIMARY KEY, Product_name varchar(15)


NOT NULL, Qty_in_hAND number(8), Unit_price number(8,2));

Table created.

CREATE Table sales (Order_no varchar(6) PRIMARY KEY, Qty_Ordered number(8),


Product_no varchar(6) REFERENCES product(Product_no));

Table created.

Write SQL queries fOR the following:

1. a. Insert 5 records in each Table


INSERT INTO product VALUES (‘P001’,’Pen’,100,10.00);

87
Department of Computer Application Kristu Jyoti College of Management &Technology
1 row inserted
INSERT INTO product VALUES (‘P002’,’Pencil’,150,8.50);
1 row inserted
INSERT INTO product VALUES (‘P003’,’Eraser’,50,5.00);
1 row inserted
INSERT INTO product VALUES (‘P004’,’Sharpener’,100,9.00);
1 row inserted
INSERT INTO product VALUES (‘P005’,’Ruler’,200,15.00);
1 row inserted

INSERT INTO sales VALUES (‘O510’,50,’P001’);


1 row inserted
INSERT INTO sales VALUES (‘O345’,70,’P002’);
1 row inserted
INSERT INTO sales VALUES (‘O967’,80,’P004’);
1 row inserted
INSERT INTO sales VALUES (‘O876’,90,’P003’);
1 row inserted
INSERT INTO sales VALUES (‘O970’,20,’P004’);
1 row inserted

b.Increase the quantity Ordered fOR Order_No = ‘O510’ to 100

UPDATE sales set qty_Ordered=100 WHERE Order_no=’O510’;

1 row Updated.

2. a.Display the name of products without duplication

SELECT distinct Product_name FROM product;

Product_name

Pen

Pencil

Eraser

Sharpener

Ruler

b.List the Product_no,name AND Qty_Ordered fOR the Order ‘O345’

SELECT product.Product_no, product.Product_name, sales.Qty_Ordered FROM product,


sales INNER JOIN product on product.Product_no = Sales.Product_no WHERE
sales.Order_no=’O345’;

88
Department of Computer Application Kristu Jyoti College of Management &Technology
Product_no Product_name Qty_Ordered
P002 Pencil 70

3. Create a view on PRODUCT Table

CREATE VIEW prod as SELECT Product_no, Product_name, Qty_in_hAND, Unit_price


FROM product;

View created.

89
Department of Computer Application Kristu Jyoti College of Management &Technology
NoSQL -MongoDB

90
Department of Computer Application Kristu Jyoti College of Management &Technology
Create a collection movie AND Insert 3 Documents

title : Fight Club writer : Chuck Palahniuk year : 1999 actORs : [ Brad Pitt

Edward NORton

] title : Pulp Fiction writer : Quentin Tarantino year : 1994 actORs : [

John Travolta

Uma Thurman

title : The Hobbit: An Unexpected Journey

writer : J.R.R. Tolkein year : 2012 franchise : The Hobbit title : The Hobbit: The Desolation
of Smaug

writer : J.R.R. Tolkein year : 2013 franchise : The Hobbit

Query / Find Documents

query the movies collection to

1. get all documents


2. get all documents with writer set to "Quentin Tarantino"
3. get all documents WHERE actORs include "Brad Pitt" 4. Get all the document in sORted
Order of writer

UPDATE Documents

1.add an actOR named "Samuel L. Jackson" to the movie "Pulp Fiction"

Delete Documents

1.delete the movie "Pulp Fiction"

Ans:

> use movie switched to db movie

> db.createCollection("movie")

Output: {“ok”: 1}

> db.movie.insert({"title":"fight club","writer":"chuck


palahniuk",year:'1999',"actORs":["brad pitt","edward NORton"]});

Output:

91
Department of Computer Application Kristu Jyoti College of Management &Technology
WriteResult({ "nInserted" : 1 })

> db.movie.insert({"title":"pulp Fiction","writer":"quentin


Tarantino",year:'1994',"actORs":["john Travolta","Uma thurman"]});

Output:

WriteResult({ "nInserted" : 1 })

> db.movie.insert({"title":"the hobbit:an unexpected


journey","writer":"j.r.r.Tolkein",year:'2012',"franchise":"the hobbit"});

Output:

WriteResult({ "nInserted" : 1 })

1. > db.movie.find().pretty();

Output:

"_id" : ObjectId("61a8dc81ee875b3b0a29ba18"),

"title" : "fight club",

"writer" : "chuck palahniuk",

"year" : "1999",

"actORs" : [

"brad pitt",

"edward NORton"

"_id" : ObjectId("61a8de3aee875b3b0a29ba19"),

"title" : "pulp Fiction",

"writer" : "quentin Tarantino",

"year" : "1994",

92
Department of Computer Application Kristu Jyoti College of Management &Technology
"actORs" : [

"john Travolta",

"Uma thurman"

}{

"_id" : ObjectId("61a8dfc2ee875b3b0a29ba1a"),

"title" : "the hobbit:an unexpected journey",

"writer" : "j.r.r.Tolkein",

"year" : "2012",

"franchise" : "the hobbit"

2. > db.movie.find({"writer":"quentin Tarantino"})

Output:

{ "_id" : ObjectId("61a8de3aee875b3b0a29ba19"), "title" : "pulp Fiction", "writer" : "quentin


Tarantino", "year" : "1994", "actORs" : [ "john Travolta", "Uma thurman" ] }

3. > db.movie.find({"actORs":"brad pitt"})

Output:

{ "_id" : ObjectId("61a8dc81ee875b3b0a29ba18"), "title" : "fight club", "writer" : "chuck


palahniuk", "year" : "1999", "actORs" : [ "brad pitt", "edward NORton" ] }

4. > db.movie.find().sORt({"writer":1})

Output:

{ "_id" : ObjectId("61a8dc81ee875b3b0a29ba18"), "title" : "fight club", "writer" : "chuck


palahniuk", "year" : "1999", "actORs" : [ "brad pitt", "edward NORton" ] }

{ "_id" : ObjectId("61a8dfc2ee875b3b0a29ba1a"), "title" : "the hobbit:an unexpected


journey", "writer" : "j.r.r.Tolkein", "year" : "2012", "franchise" : "the hobbit" }

{ "_id" : ObjectId("61a8de3aee875b3b0a29ba19"), "title" : "pulp Fiction", "writer" : "quentin


Tarantino", "year" : "1994", "actORs" : [ "john Travolta", "Uma thurman" ] }

93
Department of Computer Application Kristu Jyoti College of Management &Technology
UPDATE document

1.> db.movie.UPDATE({"title":"pulp Fiction"},{$set:{"actOR":"samuel.l.jackson"}})

Output:

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

DeleteDocument

1. > db.movie.remove({"title":"pulp Fiction"})

Output:

WriteResult({ "nRemoved" : 1 })

94
Department of Computer Application Kristu Jyoti College of Management &Technology

You might also like