0% found this document useful (0 votes)
14 views7 pages

DBMS Test

The document outlines the creation of several SQL tables including 'customer', 'orders', 'items', and others, detailing their structure and relationships through foreign keys. It also includes examples of SQL queries demonstrating the use of various clauses such as ORDER BY, ALTER, and statistical functions like MAX, MIN, SUM, and AVG. Additionally, it covers the use of date-related functions and the creation of views and copied tables.

Uploaded by

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

DBMS Test

The document outlines the creation of several SQL tables including 'customer', 'orders', 'items', and others, detailing their structure and relationships through foreign keys. It also includes examples of SQL queries demonstrating the use of various clauses such as ORDER BY, ALTER, and statistical functions like MAX, MIN, SUM, and AVG. Additionally, it covers the use of date-related functions and the creation of views and copied tables.

Uploaded by

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

create table customer

(
customer_id int not null
Primary key ,
name varchar(100) not null,
age int not null,
address varchar(45),
area varchar(45),
is_senior_citizen NUMBER(1) DEFAULT 0 ,
phone int null
);

create table customer_phone


(
customer_id int not null,
phone varchar(30) null,
primary key(customer_id,phone),
foreign key(customer_id)
references customer
);

create table orders


(
order_id number NOT NULL
PRIMARY KEY,
order_date date NOT NULL,
total_amount int NOT NULL,
is_cancelled NUMBER(1) DEFAULT 0,
discount_applied NUMBER(1) DEFAULT 0 NULL,
customer_id int NOT NULL,
foreign key(customer_id)
references customer
);

create table items


(
item_id int not null
PRIMARY KEY,
name varchar(50) not null,
price NUMBER NOT NULL,
seasonal_discount NUMBER NULL,
stock_quantity int
);

create table order_item


(
order_item_id int NOT NULL
PRIMARY KEY,
quantity int ,
total_price NUMBER ,
item_id int not null,
order_id number not null,
foreign key(item_id) references items,
foreign key(order_id) references orders
);

create table procurement_plan


(
procurement_id int NOT NULL
PRIMARY KEY,
procurement_date date NOT NULL,
quantity int NOT NULL,
item_id int NOT NULL,
foreign key(item_id) references items
);

create table messages


(
message_id int not null
PRIMARY KEY,
message varchar(1000) NOT NULL,
sent_date date NOT NULL,
customer_id int NOT NULL,
foreign key(customer_id) references customer
);

T13
Study ORDER BY Clause
SQL>select * from employee
order by dob
asc;

EMPNO EMPNAME S PHONE DOB


---------- ------------------------- - ---------- ---------
5 Mani m 7 15-APR-68
3 Amay m 333 13-APR-88
6 Anusha f 778 09-MAR-94
4 Akshata f 141 14-NOV-98
1 Ananya f 777 07-SEP-04
2 Aditi f 666 06-JUN-06

6 rows selected.

SQL> select * from employee


order by dob desc;

EMPNO EMPNAME S PHONE DOB


---------- ------------------------- - ---------- ---------
2 Aditi f 666 06-JUN-06
1 Ananya f 777 07-SEP-04
4 Akshata f 141 14-NOV-98
6 Anusha f 778 09-MAR-94
3 Amay m 333 13-APR-88
5 Mani m 7 15-APR-68

6 rows selected.

b.ALTER Clause

SQL> alter table employee


add email char(255);

Table altered.
SQL> alter table employee
drop column email;

Table altered.

T14. Study of statistical function

a.Max()
SQL> select min(amount)
from payment;

MIN(AMOUNT)
-----------
10000

SQL> select max(amount)


from payment;

MAX(AMOUNT)
-----------
25000

b. sum() and avg()

SQL> select sum(amount)


from payment;

SUM(AMOUNT)
-----------
127000

SQL> select avg(amount)


from payment;

AVG(AMOUNT)
-----------
15875

c. variance() and STDDEV()

SQL> select variance(salary) from employee;

VARIANCE(SALARY)
----------------
249766667

SQL> select variance(amount) from payment;

VARIANCE(AMOUNT)
----------------
27267857.1

SQL> select stddev(amount) from payment;

STDDEV(AMOUNT)
--------------
5221.86338

SQL> select stddev(salary) from employee;

STDDEV(SALARY)
--------------
15804.0079

T15. Study of clause

a)BETWEEN

SQL> select * from employee


where salary between 25000 and 40000;

EMPNO EMPNAME S PHONE DOB SALARY


---------- ------------------------- - ---------- --------- ----------
3 Amay m 333 13-APR-88 28000
5 Mani m 7 15-APR-68 35000
6 Anusha f 778 09-MAR-94 40000

b)LIKE
SQL> select * from tenant
where name like 'G%';

TID NAME ADHARNO CONTACT


---------- --------------------------------------------- ---------- ----------
6 Gagan 1.2346E+11 9234567876

c)ALL
SQL> SELECT empName
FROM employee
WHERE empNo = ALL
(SELECT projNo
FROM assigned_to
WHERE projNo=2);

EMPNAME
-------------------------
Aditi

d)ANY
SQL> SELECT empName
FROM employee
WHERE empNo = ANY
(SELECT projNo
FROM assigned_to
WHERE projNo=1);

EMPNAME
-------------------------
Ananya

e)IN
SQL> select * from assigned_to
where projNo in 1;

EMPNO PROJNO
---------- ----------
1 1
2 1
3 1
5 1

f)EXISTS
SQL> SELECT name
FROM Tenant
WHERE EXISTS (SELECT *
FROM Holdproperty
WHERE Tenant.TID= HoldProperty.TID);

NAME
---------------------------------------------
David
Virat
Rohit
Smriti
Gagan
Akshata Gaonkar

6 rows selected.

g)ROWNUM

SQL> select * from employee


where rownum<=3;

EMPNO EMPNAME S PHONE DOB SALARY


---------- ------------------------- - ---------- --------- ----------
1 Ananya f 777 07-SEP-04 10000
2 Aditi f 666 06-JUN-06 12000
3 Amay m 333 13-APR-88 28000

f)COUNT

SQL> select count(empNo) from employee;

COUNT(EMPNO)
------------
6

i)DISTINCT

SQL> SELECT COUNT(DISTINCT empNo) FROM employee;

COUNT(DISTINCTEMPNO)
--------------------
6

SQL> select distinct empName from employee;

EMPNAME
-------------------------
Aditi
Amay
Ananya
Anusha
Akshata
Mani

6 rows selected.

T16. Study date related functions

SQL> select *
from employee
where employee.dob <= all
(
select dob
from employee);

EMPNO EMPNAME S PHONE DOB SALARY


---------- ------------------------- - ---------- --------- ----------
5 Mani m 7 15-APR-68 35000

SQL> select *
from employee e
where e.dob>= all
(
select dob
from employee
);

EMPNO EMPNAME S PHONE DOB SALARY


---------- ------------------------- - ---------- --------- ----------
2 Aditi f 666 06-JUN-06 12000

SQL> select to_char(dob,'SYEAR')


from employee;

TO_CHAR(DOB,'SYEAR')
-------------------------------------------
TWO THOUSAND FOUR
TWO THOUSAND SIX
NINETEEN EIGHTY-EIGHT
NINETEEN NINETY-EIGHT
NINETEEN SIXTY-EIGHT
NINETEEN NINETY-FOUR

6 rows selected.

SQL> SELECT ADD_MONTHS (TO_DATE ('2009-12-05', 'YYYY-MM-DD'), 1) FROM employee;

ADD_MONTH
---------
05-JAN-10

6 rows selected.

T17 . Study of views

T18.Study of copying tables, synonym


SQL> CREATE TABLE Coding_Employees AS
2 SELECT * FROM Employee;

Table created.

SQL> select * from Coding_employees;

EMPNO EMPNAME S PHONE DOB SALARY


---------- ------------------------- - ---------- --------- ----------
1 Ananya f 777 07-SEP-04
2 Aditi f 666 06-JUN-06
3 Amay m 333 13-APR-88
4 Akshata f 141 14-NOV-98
5 Mani m 7 15-APR-68
6 Anusha f 778 09-MAR-94

6 rows selected.

You might also like