You are on page 1of 29

1. Consider the employee database defined by the following schemas.

Employee
(employee-name, street, city) Works (employee-name, company-name, salary)
1.Find employee names who earn the same payroll as that of the smallest
payroll in the company.
2.Find those companies who earn more than the average salary at First
CODE:
conn/as sysdba;
Connected.
SQL> create table employee12
2 (emp_name char(30) NOT NULL,
3 street char(30), 4 city char(20)); insert into employeel2
values('&emp_name','&street','&city') create table worksl2
(emp_name char(30),
3 comp_name char(30), 4 salary number(10)); insert into worksl2
values('&emp_name','&comp_name','salary')
SELECT emp_name FROM worksl2 2 WHERE salary IN (SELECT MIN(salary) FROM worksl2);
select comp_name 2 from worksl2 3 group by comp_name 4 having avg(salary)>(select
avg(salary) from worksl2 where comp_name.'First Bank');
CODE SNAPSHOT:
OUTPUT SNAPSHOT:
1.

.
2.

(No rows selected because all employees are from First Bank corporation)

EXPLANATION:
Create tables employee and works
Enter values
For the first question, we select employees from works table having the minimum
salary
For the second question, we select employee name from companies which earn
more than average salary of employees working for First Bank corporation
2.

CODE:
2 conn/as sysdba;
3 Connected.
4 SQL> create table employee12
5 2 (emp_name char(30) NOT NULL,
6 3 street char(30), 4 city char(20)); insert into employeel2
values('&emp_name','&street','&city') create table worksl2
7 (emp_name char(30),
8 3 comp_name char(30), 4 salary number(10)); insert into worksl2
values('&emp_name','&comp_name','salary')
9 SELECT emp_name FROM worksl2 2 WHERE salary IN (SELECT MIN(salary) FROM
worksl2);
10 select comp_name 2 from worksl2 3 group by comp_name 4 having
avg(salary)>(select avg(salary) from worksl2 where comp_name.'First Bank');
11
12 create table employee
13 2(fname char(10),
14 3 lname char(10),
15 4 ssn number(5),
16 5 address char(10),
17 6 sex char(1),
18 7 saLary number(10),
19 8 superssn number(5),
20 9 dno number(5));
21 insert into employee values('&fname', '&lname', &ssn, '&address', '&sex',
&salary, &superssn, &dno);
22 create table department(dname char(10),
23 3 dnumber number(5),
24 4 mgrssn number(5), 5 mgrsd char(10));
25 insert into department values('&dname', &dnumber, &mgrssn, &mgrsd);
26 create table project
27 2(pname char(10),
28 3 pnumber number(5),
29 4 plocation number(5), 5 dnum number(5));
30 insert into project values('&pname', &pnumber, &plocation, &dnum);
31 create table works_on
32 2(essn number(5),3 pno number(5), 4 hours number(2));
33 insert into works_on values(&essn, &pno, &hours);
34 create table dependent 2(essn number(5),
35 3 dependent_name char(20),
36 4 sex char(1),
37 5 birth_date char(20),
38 6 relationship char(10));

CODE SNAPSHOT:
OUTPUT:
1

2.

3.
4.

5.

6.

7.

8.
9.

EXPALANTION:
We create the desired tables.
We use select operations to select the queries and put conditions according to the
question.
Use count(*) operator to count the values.
3. From the following tables write a SQL query to find the salesperson and
customer who belongs to same city. Return Salesman, cust_name and city.

CODE:
create table supplier
2 (salesman_id number(10),
3 name char(30),
4 city char(20), 5 commission float(5));
insert into supplier values(&salesman_id,'&name','&city',&commission); create table
customer
2 (customer_id number(10),
3 cust_name char(30),
4 city char(20),
5 grade number(5),
6 salesman_id number(5));
insert into customer values(&customer_id,'&cust_name','&city',&grade,&salesman_id);
select supplier.name,customer.cust_name,customer.city
2 from supplier,customer
3 where supplier.city=customer.city;
CODE SNAPSHOT:
OUTPUT:
EXPLANATION:

We create the required tables.


Input values from the user

Using the select operator we select the required names and perform the
appropriate actions
4. From the following tables write a SQL query to find those orders where order
amount exists between 500 and 2000. Return ord_no, purch_amt, cust_name,
city
CODE:
create table orders
2 (ord_no number(5),
3 purch_amt number(5),
4 ord_date char(10),
5 customer_id number(5),
6 salesman_id number(5));
insert into orders values(&ord_no,&purch_amt,'&ord_date',&customer_id,&salesman_id);
create table customer
2 (customer_id number(10),
3 cust_name char(30),
4 city char(20),
5 grade number(5), 6 salesman_id number(5));
insert into customer values(&customer_id,'&cust_name','&city',&grade,&salesman_id);
select orders.ord_no,
2 orders.purch_amt,
3 customer.cust_name,
4 customer.city
5 from orders
6 inner join customer
7 on orders.customer_id=customer.customer_id
8 where orders.purch_amt between 500 and 2000;
CODE SNAPSHOT:
OUTPUT:

EXPLANATION:
We create table orders and customer.
After inserting values in them and using INNER JOIN operator we merge both the
tables.
Find the desired rows using SELECT operator
5.

CODE:
create table employee
2 (fname char(10),
3 lname char(10),
4 ssn number(5),
5 address char(10),
6 sex char(1),
7 saLary number(10),
8 superssn number(5), 9 dno number(5));
insert into employee
values('&fname','&lname',&ssn,'&address','&sex',&salary,&superssn,&dno); create table
department
(dname char(10),
3 dnumber number(5),
4 mgrssn number(5), 5 mgrsd char(10));
insert into department values('&dname',&dnumber,&mgrssn,&mgrsd); create table project
2 (pname char(10),
3 pnumber number(5),
4 plocation number(5), 5 dnum number(5));
insert into project values('&pname',&pnumber,&plocation,&dnum);
create table works_on
2 (essn number(5),
3 pno number(5), 4 hours number(2));
insert into works_on values(&essn,&pno,&hours);
(select distinct pnumber from project,department,employee where dnum=dnumber and
mgrssn=ssn and lname='Scott')
2 UNION
3 (select distinct pnumber from project,works_on,employee where pnumber=pno and
essn=ssn and lname='Scott');
select fname,lname,1.1*salary from employee,works_on,project where ssn=essn and
pno=pnumber and pname='Iot';
select sum(salary),max(salary),min(salary),avg(salary) from employee,department where
dno=dnumber and dname='Accounts';
select lname,fname from employee where NOT EXISTS (select * from works_on b where
(b.pno in (select pnumber from project where dnum=5)
2 AND
3 NOT EXISTS (select * from works_on c where c.essn=ssn and c.pno=b.pno)));
select dnumber,count(*) from department,employee where dnumber=dno and salary>600000
and dno in (select dno from employee group by dno having count(*)>5) group by
dnumber;
CODE SNAPSHOT:
OUTPUT:
Q1.

Q2.

Q3.

Q4.
Q5.

EXPALANTION:
We create the desired tables.
Find the desired rows using SELECT operator and put conditions according to them.
Use count(*) operator to count the values.

You might also like