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

Number Varchar2 Varchar2 Number

The document describes creating and modifying tables, inserting data, and writing SQL queries on the tables. It includes: 1. Creating an employee table with constraints and inserting sample records 2. Altering the employee table to modify a column size and add a new column 3. Ways to add and drop columns from an existing table 4. A query to select names and rolls from a student table matching certain criteria and counting the results 5. Creating item tables to store related data and inserting sample records 6. A join query to retrieve matching records from the item tables 7. Creating additional tables and writing more queries for different business cases.

Uploaded by

Gagan Verma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

Number Varchar2 Varchar2 Number

The document describes creating and modifying tables, inserting data, and writing SQL queries on the tables. It includes: 1. Creating an employee table with constraints and inserting sample records 2. Altering the employee table to modify a column size and add a new column 3. Ways to add and drop columns from an existing table 4. A query to select names and rolls from a student table matching certain criteria and counting the results 5. Creating item tables to store related data and inserting sample records 6. A join query to retrieve matching records from the item tables 7. Creating additional tables and writing more queries for different business cases.

Uploaded by

Gagan Verma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

create table employee with employee name, employee id, employee salary and
salary should not exceed 50000, id should be unique, and name first should
always be in capital letter.

create table employee (


employee_id NUMBER(6) ,
first_name VARCHAR2(20),
last_name varchar2(25),
salary NUMBER(8,2),
CONSTRAINT id_unique Unique ( employee_id),
CONSTRAINT sal_range CHECK( salary <= 50000) ,
CONSTRAINT fname_upper CHECK(first_name = UPPER(first_name))
)

Inserts -

insert into employee(employee_id, first_name, last_name, salary ) values (1,'PREETI','Jyoti',10000.00);


insert into employee(employee_id, first_name, last_name, salary ) values (2,'RAJ','Kumar',20000.00);

Commit all the inserts:

commit;

2. Display all table created by user and modify emp_id with 6 digits and also new
column emp_name.

Show Tables of user -

select * from cat;

Modify column -

alter table employee modify employee_id Number(6);

Add column -

alter table employee add emp_name VARCHAR2(50);

3. How will you add in an existing table,also remove an existing column from
table.
Add a new column -

alter table employee add my_column VARCHAR2(50);

Drop an existing column –

alter table employee drop column my_column;

4. Select name ,roll no. from table where name is started from R and roll no. is
greater than 10 less than 50,also count the no. of students those who belong to
this criteria.

Create Student table -

create table student (

student_id NUMBER(6),
roll_no NUMBER(4),
name VARCHAR2(100)

);

Insert data -

insert into student (student_id, roll_no, name ) values (1,08, 'Preeti Jyoti');
insert into student (student_id, roll_no, name ) values (2,10, 'Rajesh Kumar');
insert into student (student_id, roll_no, name ) values (3,11, 'Radha Kumari');
insert into student (student_id, roll_no, name ) values (4,20, 'Raman Dubey');
insert into student (student_id, roll_no, name ) values (5,55, 'Ramesh Joshi');
insert into student (student_id, roll_no, name ) values (6,68, 'Tarun Sharma');
insert into student (student_id, roll_no, name ) values (7,70, 'Ashish Saxena');

Commit all the inserts:

commit;

Query to list students with name starting with R and roll no greater than 10 and less than 50:

select * from student where name like 'R%' AND (roll_no > 10 AND roll_no < 50);

Count -

select count(*) from student where name like 'R%' AND (roll_no > 10 AND roll_no < 50);

5. Create two table A and B store item id,item name and item price in table
A.store item color , item id and item quantity in table B.
create table A (
item_id NUMBER(4),
item_name VARCHAR2(50),
item_price NUMBER(8,2)
)

create table B (
item_id NUMBER(4),
item_color VARCHAR(20),
item_quantity NUMBER(6)
)

insert into A (item_id,item_name,item_price) values( 1,'Pen', 25.00);


insert into A (item_id,item_name,item_price) values( 2,'Pencil', 4.00);
insert into A (item_id,item_name,item_price) values( 3,'Notebook', 40.00);
insert into A (item_id,item_name,item_price) values( 4,'Book', 150.00);

insert into B (item_id,item_color,item_quantity) values (1,'red', 40);


insert into B (item_id,item_color,item_quantity) values (2,'green', 20);
insert into B (item_id,item_color,item_quantity) values (3,'black', 50);
insert into B (item_id,item_color,item_quantity) values (4,'red', 5);

commit;

6. Select item id item name and color with quantity item color is red.

select A.item_id,item_name, item_color,item_quantity from A,B where A.item_id=B.item_id AND


item_color='red';

7.Create table name worker with name,worker id , worker contact no, worker id
should be primary key salary should not exceed 5000.

create table worker (


worker_id NUMBER(6) Primary key,
name VARCHAR2(100),
worker_contact NUMBER(20),
city varchar2(50),
salary Number(6,2) check (salary < 5000)

)
8.Select all salaries of employees where salary is greater than 2000 and city is
Allahabad phone no. first 4 digit are 9839.

select worker_id, salary from worker where salary > 2000 AND city='Allahabad' AND worker_contact like
'9839%'

9.Retrieve the product no. and the total quantity ordered fro products ‘p0001’,
’p0004’ from the sales order table.

create table sales_order(

sales_order_no number,
client_no varchar2(20),
product_no varchar2(20),
unit_price number(20,2),
qty_sold integer ,
total_price number (20,2),
sales_person_id number

);

insert into sales_order (sales_order_no,client_no,product_no, unit_price, qty_sold, total_price, sales_person_id)


values(1,'c0001','p0001',10.50,100,1050,1);
insert into sales_order (sales_order_no,client_no,product_no, unit_price, qty_sold, total_price, sales_person_id)
values(2,'c0001','p0001',10,50,500,2);
insert into sales_order (sales_order_no,client_no,product_no, unit_price, qty_sold, total_price, sales_person_id)
values(3,'c0002','p0001',10,10,100,2);
insert into sales_order (sales_order_no,client_no,product_no, unit_price, qty_sold, total_price, sales_person_id)
values(4,'c0002','p0004',10,25,250,2);
insert into sales_order (sales_order_no,client_no,product_no, unit_price, qty_sold, total_price, sales_person_id)
values(5,'c0002','p0004',10,60,600,3);

Query –

select product_no , sum(qty_sold) from sales_order where product_no in ('p0001','p0004')group by product_no;

10.Retrieve the names of all personal who work in Mr.Pradeep’s dept. and have
worked on inventory control system as well from the table emp and inventory.

create table employee (


employee_id NUMBER(6) ,
first_name VARCHAR2(20),
last_name varchar2(25),
salary NUMBER(8,2),
department varchar2(100)
)
select first_name, last_name from employee where department in ( select department from employee where
first_name='Pradeep');

11. Retrieve the product no. their description and the total quantity ordered for
each product.

create table product (

product_no varchar2(20),
product_name varchar2 (100),
description varchar2(256)
)

insert into product (product_no,product_name,description) values('p0001',' Product 1', 'This is description for
first product');
insert into product (product_no,product_name,description) values('p0002',' Product 2', 'This is description for
second product');
insert into product (product_no,product_name,description) values('p0003',' Product 3', 'This is description for
third product');
insert into product (product_no,product_name,description) values('p0004',' Product 4', 'This is description for
fourth product');

Query -

select p.product_no,p.description, so.total_qty from product p , (select product_no , sum(qty_sold) as total_qty


from sales_order group by product_no) so where so.product_no=p.product_no;

12. Retrieve the ordered no,client no. and salesman no. where a has been
serviced by more than one salesman from the sales-order table.
select sales_order_no,client_no,sales_person_id from sales_order where client_no in ( select client_no from
sales_order group by client_no having count(distinct sales_person_id) > 1);

13.Create table client master with the following mandatory fields:Client no. ,
name address1 and address2 column.

create table client (


client_no varchar2(20) not null,
client_name varchar2 (100) not null,
address1 varchar2(256) not null,
address2 varchar2 (256) not null,
CONSTRAINT client_no_unique Unique ( client_no)
)

14. Create a table cline master such that the contents of the column client no.
are unique across the entire column.

Same as 13 -- added unique constraint in last table only

15.Find out all ate product that are not being sokd from product master table
based on the products actually sold as shown in sate order.

select product_no, product_name from product where product_no not in ( select product_no from sales_order);

11. simple query

select product_no , sum(qty_sold) as total_qty from sales_order group by product_no

You might also like