0% found this document useful (0 votes)
19 views

Solve

ghh

Uploaded by

tasniaifa961
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Solve

ghh

Uploaded by

tasniaifa961
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

EAST WEST UNIVERSITY

Course Title: Database Systems


COURSE CODE: CSE302(1)

LAB GROUP ASSIGNMENT - 1


SQL for DDL and DML statements

Group (2) Members


2018-2-60-027 Md. Mehedi Hasan Miju
2 018-3-60-014 Md. Mahmud Alam
2018-3-60-018 Khaledur Rahman

SPRING 2021
1. Screenshot of all Tables
2.Section A: Single-Table Queries
A.1: Find those orderId where the orderId ends with an even number.
SQL: select * from order_group2
where mod(to_number(substr(orderid,3),999),2)=0;

A.2: Find the item name where item name must be distinct.
SQL: select distinct itemtype
from order_group2;
A.3: Find Receipent name, phone number and adress from Bangladesh where
delivery due after 2020.
SQL: select receipentname, phnno, receipentaddress
from delivery_group2
where phnno like '+880%' and duedate > '31-DEC-2020';
A.4: Courier service company have made a mistake with billing amount and face
big losses. They mistyped '0' instead of '9' in their billing amount. Now show
previous amount, correct amount and difference between them as losses amount.

SQL: select billingId, amount as previous_amount, replace(amount,'0','9') as


correct_amount,
replace(amount,'0','9')-amount as losses_amount
from billing_group2;

3.Section B: Multi-Table Queries


B.1: Find a male customer who order before 2021 and send product toronoto or
Mumbai with billing amount at least 3000.
SQL: select customername, customeraddress,receipentname,receipentaddress
from customer_group2 c, order_group2 o, billing_group2 b, delivery_group2 d
where c.customerId=o.customerId and o.orderId=b.orderId and o.orderid=d.orderid
and orderDate <'01-jan-2021' and amount >= 3000 and customergender='M'
and(receipentaddress like '%Toronto%' or receipentaddress like '%Mumbai%');

B.2: Find the customer info who order a book more than 15 years ago.
SQL: select customerId, customername, customeraddress,
(extract(year from sysdate) - extract(year from orderdate)) as previous_order_year
from customer_group2 natural join order_group2
where itemtype = 'Books' and (extract(year from sysdate) - extract(year from
orderdate))>15;

B.3: Rewrite the same query using Join... Using operatorFind the customer info
who order a book more than 15 years ago.
SQL: select customerId, customername, customeraddress,
(extract(year from sysdate) - extract(year from orderdate)) as previous_order_year
from customer_group2 join order_group2 using(customerId)
where itemtype = 'Books' and (extract(year from sysdate) - extract(year from
orderdate))>15;

B.4: In corona situation, Courier service company increase their


foreign(Outside of Bangladesh only) delivery cost up to
35% whose billing amount is less or equal to 500. Produce a report with customer
id, name, order id, previous amount, new amount. Display them with appropriate
column header name.
SQL: select c.customerId, customerName, o.orderId, amount as previous_amount,
amount*1.35 as new_amount
from customer_group2 c, order_group2 o, billing_group2 b, delivery_group2 d
where c.customerId=o.customerId and o.orderId=b.orderId and o.orderid=d.orderid
and phnNo not like '+880%' and amount<=500;

4. Section C: Set Operations


C.1: Customer who are male or registered after 2020
SQL: select customerid,customername from customer_group2
where registrationdate>'31-dec-2020'
union
select customerid,customername from customer_group2
where customergender ='M';

C.2: Find Billing Id whose item is Bonsai-Tree or Rocking-Chair and amount must
be more than 2000.
SQL: select orderID from billing_group2 natural join order_group2
where amount>2000
intersect
select orderID from billing_group2 natural join order_group2 where itemtype in
('Bonsai-Tree','Roking-Chair');

C.3: Find those order id, customer id, name, receipent name who do not receive
their delivery yet.
SQL: select o.orderId, c.customerId, customerName, receipentName
from customer_group2 c, order_group2 o, delivery_group2 d
where c.customerId=o.customerId and o.orderId=d.orderId
minus
select o.orderId, c.customerId, customerName, receipentName
from customer_group2 c, order_group2 o, delivery_group2 d
where c.customerId=o.customerId and o.orderId=d.orderId and dueDate<sysdate;
C.4: Find customer id, name, order id whose itemtype is 'Books' and duedate after
1990. Do not include those customers whose billing amount is greater than 400.
SQL: select c.customerId, customerName, o.orderId
from customer_group2 c, order_group2 o, billing_group2 b, delivery_group2 d
where c.customerId=o.customerId and o.orderId=b.orderId and o.orderid=d.orderid
and itemType = 'Books'
intersect
select c.customerId, customerName, o.orderId
from customer_group2 c, order_group2 o, billing_group2 b, delivery_group2 d
where c.customerId=o.customerId and o.orderId=b.orderId and o.orderid=d.orderid
and dueDate>'31-dec-1990'
minus
select c.customerId, customerName, o.orderId
from customer_group2 c, order_group2 o, billing_group2 b, delivery_group2 d
where c.customerId=o.customerId and o.orderId=b.orderId and o.orderid=d.orderid
and amount>400;

5.Section D: Aggregate Queries


D.1: Find average amount with two decimal point using truncate function.And also
find maximum amount, minimum amount, total amount, median amount, count
amount.
SQL: select trunc(avg(amount),2) as average_amount, max(amount) as
maximum_amount, min(amount) as minimum_amount,
sum(amount) as total_amount, median(amount) as median_amount, count(*) as
count_amount
from billing_group2;
D.2: Find the Customer gender Perccentage.
SQL: select customergender,Round((count(customergender)/(Select count(*) from
customer_group2))*100,2)||'%' as Gender_Percentage
from customer_group2
group by customergender;

D.3: Find average amount of billing for each itemtype with two decimal point.
SQL: select itemType, round(avg(amount),2) as average_amount
from order_group2 o, billing_group2 b
where o.orderId=b.orderId
group by itemType;
D.4: Find the maximum amount of billing without decimal point for each item type
and also Count the number of digits from the maximum amount without decimal
point. Do not include the item type in the result if it has less than 4 number of
digits in the Maximum amount. It must be sorted by the maximum amount in
descending order, if two amounts are similar then sort by item type in ascending
order.
SQL: select itemType, round(max(amount)) as maximum_amount,
length(round(max(amount))) as number_of_digits
from order_group2 o, billing_group2 b
where o.orderId=b.orderId
group by itemType
having length(round(max(amount)))>=4
order by max(amount) desc, itemType;
6.Section E: Nested Subqueries in WHERE Clause
E.1: Find those who listed as customer but do not have any order yet.
SQL: select c.customerId, customername
from customer_group2 c
where c.customerId not in (select c1.customerId
from customer_group2 c1,order_group2 o
where c1.customerid=o.customerid);
E.2: Find order id, receipent name, address and amount whose amount is greater
than all recipent who live in out of the Bangladesh It mus be sorted by the amount
in descending order.
SQL: select o.orderid, receipentName, receipentAddress, amount
from order_group2 o, billing_group2 b, delivery_group2 d
where o.orderid=b.orderid and o.orderid=d.orderid
and amount > all (select amount
from order_group2 o, billing_group2 b, delivery_group2 d
where o.orderid=b.orderid and o.orderid=d.orderid
and phnno not like '+880%')
order by amount desc;

E.3: Find customer id, name who are male and registration date, order date and
due date are same.
SQL: select customerid, customername
from customer_group2 c1
where customergender = 'M'
and exists (select * from customer_group2 c2, order_group2 o
where c1.customerid=c2.customerid and c2.customerid=o.customerid
and registrationdate=orderdate and exists (select *
from order_group2 o, delivery_group2 d
where c2.customerid=o.customerid and o.orderid=d.orderid
and orderdate=duedate));

E.4: Find the customer id, name, order id and amount whose billing amount is the
minimum.
SQL: select c.customerid, customername, o.orderid, amount
from customer_group2 c, order_group2 o, billing_group2 b
where c.customerid= o.customerid and o.orderid=b.orderid
and amount<=all (select amount from billing_group2);
7.Section F: Nested Subqueries in FROM Clause
F.1. Find those who listed as customer but do not have any order yet. (Solve the
same query in E.1. using FROM Clause)
SQL: select customerId, customername
from (select * from customer_group2 c
minus
select c.*
from customer_group2 c, order_group2 o
where c.customerId=o.customerId);
F.2: Find order id, receipent name, address and amount whose amount is greater
than all recipent who live in out of the Bangladesh It mus be sorted by the amount
in descending order.
(Solve the same query in E.2. using FROM Clause)
SQL: select o.orderid, receipentName, receipentAddress, amount
from order_group2 o, delivery_group2 d,
(select o1.orderId, amount
from order_group2 o1, billing_group2 b1 where o1.orderid=b1.orderid
and amount> all (select amount
from order_group2 o2, billing_group2 b2, delivery_group2 d2
where o2.orderid=b2.orderid and o2.orderid=d2.orderid and phnno not like
'+880%')) t
where o.orderid=d.orderid and o.orderid=t.orderid
order by amount desc;

F.3: Find customer id, name who are male and registration date, order date and
due date are same.(Solve the same query in E.3. using FROM Clause)
SQL: select c.customerid, customerName
from customer_group2 c, order_group2 o,
(select c.customerId, o.orderId
from customer_group2 c, order_group2 o, delivery_group2 d
where c.customerid=o.customerid and o.orderid=d.orderid and
registrationdate=orderdate and orderdate=duedate and customergender='M') t
where c.customerid=o.customerid and c.customerId=t.customerId and
o.orderId=t.orderId;

F.4: Find the customer id, name, order id and amount whose billing amount is the
minimum (Solve the same query in E.4. using FROM Clause)
SQL: select c.customerid, customername, o.orderid, amount
from customer_group2 c, order_group2 o,
(select orderId, amount
from billing_group2
where amount <= all (select amount
from billing_group2)) t
where c.customerid= o.customerid and o.orderid=t.orderid;
8.G : Defining Views
G.1: Create an updatable view that contains order id and amount. The name of the
view must be v1.
SQL: create view v1 as select billingid,amount from billing_group2;

G.2: Show the view v1.


SQL: select * from V1;
=> Its select clause contains plain and simple.
=> No distinct keyword, aggregates or arithmetic operations.
=>It has single relation
=>No group by or having clause
=> this view may not contain some attributes of the underlying relation. Those
attributes not present in the views can be se to null.
all four condition satisfied, so this is updatable view.

G.3: Create a non-updatable view that contains average amount of billing for each
item type. Do not include the item type in the view if its average amount is greater
than 1000. It must be sorted by the Average amount in descending order.
SQL: create view v2 as
select itemtype,round(avg(amount),2) as average_amount
from billing_group2 natural join order_group2
group by itemtype
having round(avg(amount),2)> 1000
order by round(avg(amount),2) desc;

G.4: Show the view v2.

SQL: select * from V2;

This is a non-updatable view. because it is not simple view.


1. Its select clasue contain aggregate function
2. It has multiple relations (billing_group2, order_group2)
3. It has group by and having clause these condition proves that
this is not simple view.
Hence, this is an non-updatable view.

9. H. Authorization
H.1: Create three users named as user1, user2 and user3
and allow them appropriate system-level privileges.
SQL: create user user1 identified by miju;
grant resource, connect, create session, create table,
create view, create any trigger, create any procedure,
create sequence, create synonym, unlimited tablespace to user1;

create user user2 identified by mahmud;


grant resource, connect, create session, create table,
create view, create any trigger, create any procedure,
create sequence, create synonym, unlimited tablespace to user2;

create user user3 identified by khaledur;

grant resource, connect, create session, create table,


create view, create any trigger, create any procedure,
create sequence, create synonym, unlimited tablespace to user3;
H.2: Firstly from "DMA" account, grant select privilege on V1, V2 views to
"User1" with grant option privilege and grant select privilege on V1 view to
"User2" with grant option privilege. Secondly from "User1" account, grant select
privilege on V1 view to "User2" with grant option privilege and grant select
privilege on V2 view to "User2" without grant option privilege and grant select
privilege on V1, V2 views to "User3" without grant option privilege Lastly from
"User2" account, grant select privilege on V1 views to "User3" without grant
option privilege You must include the resulting authorization graph and the content
of user_tab_privs table in your report after executing these statements.
SQL: grant select on v1 to user1,user2 with grant option;
grant select on v2 to user1 with grant option;
select * from user_tab_privs;
In USER1 :
SQL: select * from dma.v1;

SQL: select * from dma.v2;


grant select on dma.v1 to user2 with grant option;
grant select on dma.v2 to user2;
grant select on dma.v1 to user3;
grant select on dma.v2 to user3;
select * from user_tab_privs;

In USER2:
SQL: Select * from dma.v1;
SQL: Select * from dma.v2;

grant select on dma.v1 to user3;

select * from user_tab_privs;


In USER3 :
SQL: Select * from dma.v1;

SQL: Select * from dma.v2;


SQL: select * from user_tab_privs;

Authorization graph:

H.3: Firstly from "DBA" account, revoke only V2 view from "User1" and check
from all other users account whether any changes happen or not.Lastly again from
"DBA" account, revoke V1 view from "User1"and check from all other users
account whether any changes happen or not.You must include the resulting
authorization graph and the content of user_tab_privs table in your report after
executing these statements. Spot the difference and explain.

After revoke v2 from user1. (DMA Account)


SQL: revoke select on v2 from user1;
select * from user_tab_privs;

In user1 account
SQL: Select * from dma.v1;
SLQ: Select * from dma.v2;

SQL: select * from user_tab_privs;


In user2 account:
SLQ: Select * from dma.v1;

SLQ: Select * from dma.v2;


SQL: select * from user_tab_privs;

In USER3 :
SLQ: Select * from dma.v1;

SLQ: Select * from dma.v2;


SQL: select * from user_tab_privs;

Authorization graph: After revoke v2 from user1.

After revoke v1 from user1:


SQL: revoke select on v1 from user1;
select * from user_tab_privs;
In USER1:
SQL: select * from dma.v1;

SQL: select * from dma.v2;

In user2:
SQL: select * from dma.v1;
SQL: select * from dma.v2;

SQL:select * from user_tab_privs;

In user3:
SQL: select * from dma.v1;

SQL: select * from dma.v2;

SQL:select * from user_tab_privs;


Authorization graph: After revoke v1 from user1. Final Graph of H3.

Graph Difference:

Graph(H2):
Graph(H3).

Explanation:
Firstly, we revoke V2 view from "USER1". This view got privileged with grant option
from "DMA".
After this revoke statement, we cannot access the V2 view from "USER1" anymore.
Moreover, this revoke statement automatically
revoke V2 view from "USER2" and "USER3" because both of the users got privilege
from "USER1".

Lastly, we revoke V1 view from "USER1". This view also got privileged with grant
option from "DMA".
After this revoke statement, we cannot access the V1 view from "USER1" anymore.
Moreover, this revoke statement automatically
revoke V1 view from "USER2" and "USER3" because both of the users got privilege
from "USER1".But we can access V1 view from "User2" and "User3". Because "User2"
got V1 view privilege from both "DMA" and "User1". Although "User2" lost its privilege
from "User1" but "User2" still have V1 view privilege by "DMA" with grant
option.That's why we can access V1 view from "User2".And hence, "User2" also grant
privilege to "User3", so "User3" can access V1 view as well.

Discussion:
We developed our knowledge not only from text books but also researched many
materials and even observed some works of Mgramin and Danhuss; SQL
developers from GitHub. Hence, we were able to overcome some difficult queries
which we made ourselves such as in query A.1 we transformed a varchar into a
number query (some more examples of difficult query). In A.4 if someone
mistakenly input a wrong digit without using update operation we find a solution,
If we want to talk about B.2 there we try to find some customer who are missing
for a long while.In C.4 we use multiple set operator in multiple relation. In D.2 we
find gender percentage where we use aggregate functions, arithmetic operation
concat operator as well as nested query too.
We solved all the questions in the group together and everyone discussed together
and chose the best solutions from there. Due to this group assignment, the problem
that we had in the concept has been solved in a very short time.
Therefore, we are satisfied with our work as our team work was crucial and very
synchronized that led us to complete our assignment with a feeling of
self-satisfaction.
Appendix
Contribution Matrix
Query No. Member Name who completed this Remarks
query and solved

A.1 Khaledur Rahman


A.2 Md. Mahmud Alam
A.3 Md. Mehedi Hasan Miju
A.4 Md. Mahmud Alam

B.1 Md. Mahmud Alam


B.2 Md. Mehedi Hasan Miju
B.3 Md. Mehedi Hasan Miju
B.4 Khaledur Rahman

C.1 Khaledur Rahman


C.2 Md. Mahmud Alam
C.3 Md. Mehedi Hasan Miju
C.4 Md. Mehedi Hasan Miju

D.1 Md. Mahmud Alam


D.2 Md. Mehedi Hasan Miju
D.3 Khaledur Rahman
D.4 Md. Mahmud Alam

E.1 Khaledur Rahman


E.2 Md. Mehedi Hasan Miju
E.3 Md. Mahmud Alam
E.4 Md. Mahmud Alam

F.1 Khaledur Rahman


F.2 Md. Mehedi Hasan Miju
F.3 Md. Mahmud Alam
F.4 Md. Mahmud Alam

G.1,G.2 Khaledur Rahman


G.2,G.3 Md. Mehedi Hasan Miju

H.1 Khaledur Rahman


H.2 Md. Mehedi Hasan Miju
H.3 Md. Mahmud Alam

You might also like