0% found this document useful (0 votes)
30 views29 pages

Practical 8

The document outlines various SQL commands for creating and manipulating database tables related to accounts, loans, installments, and transactions. It includes practical exercises for creating tables, retrieving data, updating records, and applying data constraints. Additionally, it covers character functions, group functions, set operators, and join operations in SQL.

Uploaded by

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

Practical 8

The document outlines various SQL commands for creating and manipulating database tables related to accounts, loans, installments, and transactions. It includes practical exercises for creating tables, retrieving data, updating records, and applying data constraints. Additionally, it covers character functions, group functions, set operators, and join operations in SQL.

Uploaded by

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

Enrollment No:-23DIT0008 Database Management System

Practical 3
Aim: - Design the below given schemas using SQL Command
-”Create”. Decide the appropriate data type for each column.

(a)Create a table ACCOUNT with column account number, name,


city, balance, loan taken.
Query: - CREATE TABLE account(
ACCOUNT_number int(20),
name varchar(20),
city varchar(20),
balance int(20),
loan_taken int(20)
);

(b)Create a LOAN table with column loan number, account number,


loan amount, interest rate, loan date, remaining loan.
Query: - CREATE TABLE loan(
loan_num int(20),
ac_num int(20),
loan_amt int(20),
interest_rate int(20),
loan_date date,
remaining_loan int(20)
);

1
Enrollment No:-23DIT0008 Database Management System

(c)Create table INSTALLMENT with column loan number, installment


number, installment date and amount.
Query: - CREATE TABLE installment(
loan_num int(20),
inst_num int(20),
inst_dat date,
inst_amt decimal(15,2)
);

(d)Create table TRANSACTION with column account number,


transaction date, amount, type of transaction, mode of payment.
Query: - CREATE TABLE transaction(
ac_num int(20),
tran_date date,
tran_amt int(20),
tran_type varchar(20),
payment_mode varchar(20)
);

(e) Show the structure of above tables using “Describe” command.


Query: - describe account;

2
Enrollment No:-23DIT0008 Database Management System

Query: - describe loan;

Query: - DESCRIBE installment;

Query: - DESCRIBE TRANSACTION;

3
Enrollment No:-23DIT0008 Database Management System

Practical 4
Aim: Retrieve data from tables in Practical 2 using Data SQL
command- “Select”

(a)Display all rows and all columns of table Transaction.


Query: SELECT * FROM TRANSACTION;

(b)Display all rows and selected columns of table Installment.


Query: SELECT inst_num,loan_num from installment;

.
(c)Display selected rows and selected columns of table Account.
Query: SELECT account_number,name,city from account where
city='surat';

(d)Display selected rows and all columns of table loan.


Query: SELECT * from loan where interest_rate=6.30;

4
Enrollment No:-23DIT0008 Database Management System

(e)Display the branch wise balance from account table.


Query: SELECT name,balance from account where branch='valsad'
and name='abcdf ';
SELECT name,balance from account where branch='valasad' and
name='megha';

(f)Display list of those branches that have balance greater than 1


Lakh rupees.
Query: SELECT branch FROM account where balance>100000

(g)Display the list of customers in descending order of their name


from account table.
Query:

5
Enrollment No:-23DIT0008 Database Management System

(h) Display those records where mode of payment is “cheque”.


Query: SELECT * from transaction WHERE
payment_mode='[Cheque]';

6
Enrollment No:-23DIT0008 Database Management System

Practical 5
Aim: Write SQL queries to use Update, alter, rename, delete,
truncate and distinct.
Table: ACCOUNT.
(a)Change the name ‘pateljigar’ to ‘patelhiren’.
Query: UPDATE `account` SET `name`='[patelhiren]' WHERE
name='[pateljigar]';

(b)Change the name and city where account number is A005. (new
name = ‘kotharinehal’and new city = ‘patan’).
Query: - UPDATE `account` SET `name`='[‘kotharinehal’]',
`city`='[patan]' WHERE ACCOUNT_number=A005;

(c)Display only those records where loan taken status is ‘YES’.


Query:- SELECT * FROM `account` WHERE loan_taken='Yes';

7
Enrollment No:-23DIT0008 Database Management System

(d)Add the new column (address varchar2 (20)) into table ACCOUNT.
Query: - ALTER TABLE account add address varchar(20);

(e)Create another table ACCOUNT_TEMP (acc_no, name, balance)


from table ACCOUNT.
Query: - CREATE TABLE ACCOUNT_TEMP AS SELECT
account_number AS acc_no, name, balance FROM ACCOUNT;

(f)Rename the table ACCOUNT to ACCOUNT_MASTER.


Query:- ALTER TABLE ACCOUNT RENAME TO ACCOUNT_MASTER;

(g)Update the column balance for all the account holders. (Multiply
the balance by 2 foreach account holders)
Query:- UPDATE ACCOUNT_MASTER SET balance = balance * 2;

8
Enrollment No:-23DIT0008 Database Management System

(h)Delete the records whose account no is A004


Query:- DELETE FROM ACCOUNT_MASTER WHERE account_number =
'A004';

Table: LOAN.
(a)For each loan holders Add 100000 Rs. Amount into the column
loan_amt.
Query:- UPDATE LOAN
SET loan_amt = loan_amt + 100000;

(b)For each loan holders Increase the interest rate 2%.


Query:- UPDATE LOAN
SET interest_rate = interest_rate + 2;

9
Enrollment No:-23DIT0008 Database Management System

(c)Display only those records where loan holder taken a loan in


month of January.
Query:- SELECT * FROM LOAN
WHERE EXTRACT(MONTH FROM loan_date) = 1;

(d)Modify the structure of table LOAN by adding one column credit_no


varchar2 (4).
Query:-ALTER TABLE LOAN
ADD credit_no VARCHAR(4);

(e)Display the Loan amount*2 of table LOAN.


Query:- SELECT loan_num, loan_amt * 2 AS doubled_loan_amount
FROM LOAN;

10
Enrollment No:-23DIT0008 Database Management System

(f)Display the records of table LOAN by date wise in ascending order.


Query:- SELECT * FROM LOAN
ORDER BY loan_date ASC;

(g)Display the records of table LOAN by account number wise in


descending Order.
Query:- SELECT * FROM LOAN ORDER BY ac_num DESC;

(h)Increase the size 5 to 7 of column acc_no.


Query:- ALTER TABLE ACCOUNT_TEMP
MODIFY acc_no VARCHAR(7);

11
Enrollment No:-23DIT0008 Database Management System

Table: INSTALLMENT.
(a)Change the Inst_Date ‘2-Feb-04’ to ’3-Mar-04’.
Query- UPDATE INSTALLMENT
SET inst_dat = '2004-03-03'
WHERE inst_dat = '2004-02-02';

(b)Reduce 5000 amount from all Installment holders.


Query:- UPDATE INSTALLMENT
SET inst_amt = inst_amt - 5000;

(c)Add the amount 5000 where loan no is ‘L003’ and ‘L002’.


Query:- UPDATE INSTALLMENT
SET inst_amt = inst_amt + 5000
WHERE loan_num IN ('L003', 'L002');

12
Enrollment No:-23DIT0008 Database Management System

(d)Change the column size of 5 to 7 where column name is Loan_no.


Query:-ALTER TABLE INSTALLMENT
MODIFY loan_number VARCHAR(7);

(e)Delete row where inst_no is ‘I001’.


Query:DELETE FROM INSTALLMENT
WHERE installment_number = 'I001';

(f)Only create a structure of table installment1 from table installment.


Query:CREATE TABLE INSTALLMENT1 AS
SELECT * FROM INSTALLMENT WHERE 1 = 0;

Table: TRANSACTION.
(a)Insert any duplicate value and display all the records without any
duplicate rows.
1. Insert a duplicate value

13
Enrollment No:-23DIT0008 Database Management System

Query INSERT INTO TRANSACTION (ac_num, tran_date,


tran_amt, tran_type, payment_mode)
VALUES (123456, '2024-08-10', 5000.00, 'Deposit', 'Online');

2. Display all records without any duplicate rows


Query:SELECT DISTINCT * FROM TRANSACTION;

b)Select all the records in descending order(account number wise).


SELECT *
FROM TRANSACTION
ORDER BY ac_num DESC;

14
Enrollment No:-23DIT0008 Database Management System

(c)Display amt, date, and type of transaction by date wise.


Query: SELECT tran_amt, tran_date, tran_type
FROM TRANSACTION
ORDER BY tran_date;

(d)Delete a table TRANSACTION_TEMP.


Query:DROP TABLE TRANSACTION_TEMP;

(e)Display account number where type of transaction is ‘D


Query: SELECT ac_num
FROM TRANSACTION
WHERE tran_type = 'D';

15
Enrollment No:-23DIT0008 Database Management System

16
Enrollment No:-23DIT0008 Database Management System

Practical 8
Aim:-Write SQL queries to use various character functions.

(a)Find out length of string “hello world”.


Query: select length ('hello world') from dual;

b) Change the case of ‘HELLO WORLD’ string to lower case.


Query: select lower ('HELLO WORLD') from dual;

(c)Change the case of ‘hello world’ to upper case.


Query: select upper ('hello world') from dual;

17
Enrollment No:-23DIT0008 Database Management System

(d)Display each word initial letter as capital letter: government


polytechnic for girls.
Query: select initcap ('government polytechnic for girls') from dual;

(e)Find ‘put’ from string ‘computer’.


Query: select instr('computer','put')from dual;

(f)Add 10 star on left side of India string.


Query: select lpad('india',length('india')+10,'*')from dual;

(g)Add 10 star on right side of India string.


Query: select rpad('india',length('india')+10,'*')from dual;

(h)Trim ‘ion’ from ‘information’.


Query: select rtrim('information','ion') from dual;

18
Enrollment No:-23DIT0008 Database Management System

(i)Trim ‘info’ from ‘information’.


Query: select ltrim('information','info') from dual;

Practical-10
Aim: Write SQL queries to use various group function and
operators using tables created in Practical

(a)Retrieve specified information for the account holder who are not
in ‘Ahmedabad’.
Query: - SELECT * FROM account WHERE city! ='ahemdabad';

19
Enrollment No:-23DIT0008 Database Management System

(b)Retrieve specified information for the account holder who are not
in ‘Ahmedabad ‘or ‘Vadodara’.
Query:- SELECT * FROM `account` WHERE city not
in('ahemdabad','vadodara');

(c)Retrieve those records of Account holder whose balance between


is 50000 and 100000.
Query:- SELECT * FROM `account` WHERE balance BETWEEN 50000
AND 100000;

(d)Retrieve those records of Account holder whose balance not


between is 50000 and 100000.
Query:- SELECT * FROM `account` WHERE balance NOT BETWEEN
50000 AND 100000;

(e)Display only those records whose amount is 5000, 25000, 30000.

20
Enrollment No:-23DIT0008 Database Management System

Query:- SELECT * FROM `account` WHERE balance


in('5000','25000','30000');

(f)Display only those records whose amount not in 5000, 25000,


30000.
Query:- SELECT * FROM `account` WHERE balance not
in('5000','25000','30000');

(g)Find the total transaction amount of account holder from


transaction table.
Query:- SELECT sum(balance) FROM account;

(h)Find minimum amount of transaction.


Query:- SELECT min(tran_amt) FROM transaction;

(i)Find maximum amount of transaction.


Query:- SELECT max(tran_amt) FROM transaction;

21
Enrollment No:-23DIT0008 Database Management System

(j)Count the total account holders.


Query:- SELECT count(*) from account;

(k)Count only that record that’s made of payment is ‘cash’.


Query:- SELECT COUNT(payment_mode) FROM TRANSACTION
WHERE payment_mode='cash';

(l)Count only those records whose transaction made in the month of


‘MAY’.

Query:- SELECT COUNT(*) AS may_transactions FROM


TRANSACTION WHERE EXTRACT(MONTH FROM tran_date) =
5;

(m)Find the average value of transaction.


Query:- SELECT AVG(tran_amt) FROM transaction;

(n)Display total balance for each branch from account table.


Query:- SELECT city AS branch,sum(balance) from account GROUP by
branch;

22
Enrollment No:-23DIT0008 Database Management System

(o)Display total balance for account in Ahmadabad city.


Query:- SELECT SUM(balance) FROM account WHERE
branch='Ahmedabad';

(p)Find total amount of mode of pay ‘cash’ from transaction table.


Query:- SELECT SUM(tran_amt) FROM transaction WHERE
payment_mode='cash';

23
Enrollment No:-23DIT0008 Database Management System

Practical-11
Aim: Write SQL query for set operators and join operations.
(Use tables of Practical 1)

(a)Display all customer account number who have account or taken


loan from bank.
Query:- SELECT ACCOUNT_number FROM account UNION SELECT
ac_num FROM loan;

(b)Display all customer account number who have account and also
taken loan from bank.
Query:- SELECT ACCOUNT_number FROM account INTERSECT SELECT
ac_num FROM loan;

24
Enrollment No:-23DIT0008 Database Management System

(c)Find the list of all account number of customer who have no loan
in bank.
Query:- SELECT ACCOUNT_number FROM account LEFT JOIN loan ON
account.loan_taken=loan.ac_num WHERE loan_taken='no';

(d)Display name of all customer whose remaining loan amount is


greater than 50000.
Query:- SELECT name FROM account LEFT JOIN loan ON
account.account_number=loan.ac_num WHERE
remaining_loan>50000;

(e)Display account number of customer who have given installment


of their loan on date 18-June-04.
Query:- SELECT account_number FROM account_master left JOIN
loan ON account_master.ACCOUNT_number=loan.ac_num left JOIN
installment on loan.ac_num=installment.loan_num WHERE
inst_dat=2006/6/18;

25
Enrollment No:-23DIT0008 Database Management System

(f)Display name of all customer who have selected mode of payment


as cash.
SELECT DISTINCT a.name FROM ACCOUNT_MASTER a JOIN
TRANSACTION t ON a.account_number = t.ac_num WHERE
t.payment_mode = 'cash';

26
Enrollment No:-23DIT0008 Database Management System

Practical-12
Aim: - Apply the concept of integrity/data constraints while
creating/altering a table.

(a)Create table SalesPeople where Snumvarchar2(4) P.K(first letter


should start with S) , Sname varchar2(20) NOT NULL,City
Varchar2(15),Mobile_No Number(10).
Query:- CREATE TABLE SalesPeople (
snum varchar(4) PRIMARY KEY CHECK(snum like 'S%'),
27
Enrollment No:-23DIT0008 Database Management System

Sname varchar(20) NOT NULL,


City Varchar(15),
Mobile_Number varchar(10)
);

(b)Create table Customer where cnum varchar2(4) P.K(first letter


should start with C), Cname varchar2(20) NOT NULL,city
varchar2(20),Rating number(3) DEFAULT 10,Snum number(4) F.K
(where snum refers salespeople table)
Query:- CREATE TABLE customer(
cnum varchar(4) PRIMARY KEY CHECK(cnum LIKE '%S'),
Cname varchar(20) NOT NULL,
city varchar(20),
rating int(10) DEFAULT 10,
snum varchar(4),
FOREIGN KEY(snum) REFERENCES salespeople(snum)
);

(c)Create table Order where Order_No number(4) P.K, Amount


number(5), odate varchar2(10), cnum varchar2(4) F.K,(where cnum
refers customer table), snum varchar2(4) F.K (where snum refers
Salespeople table)
Query:- CREATE TABLE Orders(
28
Enrollment No:-23DIT0008 Database Management System

Order_number int(4) PRIMARY KEY,


Amount int(5),
odate varchar(10),
cnum varchar(4),
FOREIGN KEY(cnum) REFERENCES customer(cnum),
snum varchar(4),
FOREIGN KEY(snum) REFERENCES salespeople(snum)
);

29

You might also like