SQL Query
SQL Query
1)
Consider the following Entities and Relationships Employee (empno, empname, salary,commission,designation) Department (deptno, deptname,location) Relationship between Employee and Department is many-to-one. Constraints: Primary Key, Salary should be > 0. Create a RDB in 3NF & write queries in Oracle 8i for following. Display all details of employees who are working at Pune location Display department name wise list of Employees Count the number of employees who are working in Computer department Display maximum salary for every department.
SOLUTION
SQL>create table department(deptno number(5) primary key,deptname varchar2(15),location varchar2(10)); Table created. SQL>create table employee(empno number(5) primary key,empname varchar2(20),salary number(10),commission number(2),designation varchar2(10),deptno number(5) references department(deptno)); Table created.
SQL>Insert into department values(1,production,pune); 1 row created. SQL>Insert into department values(2,finance,mumbai); 1 row created.
SQL>Insert into department values(3,sales,mumbai); 1 row created. SQL>Insert into department values(4,accounts,mumbai); 1 row created. SQL>Insert into department values(5,manufacturing,mumbai); 1 row created. SQL>Insert into employee values(101,ashish,50000,50,manager); 1 row created. SQL>Insert into employee values(102,vivek,20000,20,accountant); 1 row created. SQL>Insert into employee values(103,ajit,10000,10,salesman); 1 row created. SQL>Insert into employee values(104,rohan,5000,5,clerk); 1 row created. SQL>Insert into employee values(105,nikhil,15000,15,peon); 1 row created. SQL>Select * from department; DEPTNO -------1 2 3 DEPTNAME -------production finance sales LOCATION -------pune mumbai mumbai
4 5
accounts
mumbai
manufacturing Mumbai
SQL>Select * from employee; EMPNO ------101 EMPNAME ---------- -ashish SALARY -------50000 COMMISSION ---------------50 DESIGNATION ---------------manager
EMPNO ----101
EMPNAME ---------ashish
SALARY -----50000
COMMISSION ---------50
DESIGNATION ------------manager
DEPTNO -------1
DEPTNAME -------production
LOCATION -------pune
DEPTNAME
EMPNAME
---------------------------------------
SQL> select count(empno) from employee, department where employee.deptno = department.deptno and deptname=computer; COUNT(EMPNO) ------------0 SQL> select max(salary) from employee, department where employee.deptno = department.deptno group by deptname, empname; MAX(SALARY) ----------10000 25000 20000 5000 15000
Q.2)
Consider the following Entities and Relationships Employee (empno, empname, salary,commission,designation) Department (deptno, deptname,location) Relationship between Employee and Department is many-to-one. Constraints: Primary Key, Salary should be > 0. Create a RDB in 3NF & write queries in Oracle 8i for following. Count all the movie names released in the year 2000. Display all actor details of movie Dhoom Display all actorwise movie names Display all movies of Abhishek
SOLUTION
SQL> create table actor(actno number(5) primary key,actname varchar2(20)); Table created. SQL> create table movie(mvno number(5) primary key,actno number(5) references actor(actno),mvname varchar2(20),releaseyear number check(releaseyear>0)); Table created. SQL> insert into actor values(1,'Abhishek'); 1 row created. SQL> insert into actor values(2,'Hrithik'); 1 row created. SQL> insert into actor values(3,'Amitabh'); 1 row created. SQL> insert into actor values(4,'Dharmendra'); 1 row created.
SQL> insert into actor values(5,'Abhishek'); 1 row created. SQL> insert into movie values(01,1,'Dhoom',2005); 1 row created. SQL> insert into movie values(02,2,'Krish',2008); 1 row created. SQL> insert into movie values(03,3,'Aagnipath',2000); 1 row created. SQL> insert into movie values(04,4,'Sholay',2000); 1 row created. SQL> insert into movie values(05,5,'Bluffmaster',2001); 1 row created. SQL> insert into movie values(06,2,'Dhoom',2005); 1 row created.
SQL> select * from movie; MVNO ACTNO MVNAME ------1 2 3 4 5 6 ------1 2 3 4 5 2 ----------------Dhoom Krish Aagnipath Sholay Bluffmaster Dhoom RELEASEYEAR ----------------2005 2008 2000 2000 2001 2005
SQL> select * from actor; ACTNO ACTNAME ---------1 2 3 4 5 -------------------Abhishek Hrithik Amitabh Dharmendra Abhishek
SQL> select count(mvname) from movie,actor where actor.actno=movie.actno and releaseyear=2000; COUNT(MVNAME) ------------2
SQL> select actname from actor,movie where actor.actno=movie.actno and mvname='Dhoom'; ACTNAME -------------------Abhishek Hrithik
ACTNAME -------------------Amitabh
MVNAME -------------------Aagnipath
SQL> select mvname,actname from actor,movie where actor.actno=movie.actno and actname='Abhishek'; MVNAME -------------------Dhoom Bluffmaster ACTNAME -------------------Abhishek Abhishek