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

DBMS Rough Data

The document creates tables for an employee database including tables for employees, departments, department locations, projects, employee work on projects, and employee dependents. It inserts data into the tables and writes several queries to retrieve information from the tables.

Uploaded by

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

DBMS Rough Data

The document creates tables for an employee database including tables for employees, departments, department locations, projects, employee work on projects, and employee dependents. It inserts data into the tables and writes several queries to retrieve information from the tables.

Uploaded by

Rohan Raqo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

create database company;

use company;

create table employee(

Fname varchar(15) not null,

Minit char,

Lname varchar(15) not null,

Ssn char(9) primary key not null,

Bdate date,

Address varchar(30),

Sex char,

Salary decimal(10,2),

Super_ssn char(9),

Dno int );

insert into employee values

('sachin', NULL, 'yadav', 'a112', '2000-08-17', 'delhi', 'm', 250000, 'a112', 1),

( 'ashish', NULL, 'kumar', 'a113', '2000-12-30', 'rewari', 'm', 24000, 'a113', 2),

( 'nimish', NULL, 'yadav', 'a114', '1998-05-12', 'chandigarh', 'm', 64000, 'a114', 3),

( 'subham', NULL, 'yadav', 'a121', '2000-11-07', 'rewari', 'm', 2100000, 'a121', 4),

( 'aditya', NULL, 'soni', 'a221', '2000-12-07', 'hisar', 'm', 250000, 'a221', 5),

( 'naveen', null, 'rao', 'a300', '2000-11-14', 'rewari', 'm', 220000, 'a300', 6),

('aman', NULL, 'bansiwal', 'a323', '2000-12-24', 'rewari', 'm', 2500, 'a323', 7),

( 'sandeep', NULL, 'kataria', 'a421', '2000-12-16', 'rewari', 'm', 250000, 'a421', 8 ),

('hitesh', NULL, 'yadav', 'a921', '2000-10-09', 'rewari', 'm', 25000, 'a921', 9),

( 'kuldeep', NULL, 'yadav', 'b321', '2000-03-09', 'rewari', 'm', 2150000, 'b321', 10);

select * from employee;


create table department(

Dname varchar(15) not null,

Dnumber int primary key not null,

Mgr_ssn char(9) ,

foreign key (Mgr_ssn) references employee(Ssn),

Mgr_start_date date

);

insert into department values

('Management', 1,'a112','2021-11-11'),

('Finance', 2,'a113','2021-11-11'),

( 'IT', 3,'a114','2021-11-11'),

( 'Accounts', 4,'a121','2021-11-11'),

('Projects', 5,'a221','2021-11-11'),

('Data Analytics', 6,'a300','2021-11-11'),

('Sales', 7,'a323','2021-11-11'),

('HR', 8,'a421','2021-11-11'),

('Marketing', 9,'a921','2021-11-11'),

('Operations', 10,'b321','2021-11-11');

select* from department;

create table dept_locations(

Dnumber int ,

foreign key (Dnumber) references department(Dnumber),

Dlocation varchar(15) not null,

primary key(Dnumber,Dlocation)
);

insert into dept_locations values

(1, 'Rewari'),

( 2,'Gurugram'),

( 3, 'Delhi'),

( 4, 'Jaipur'),

( 5, 'Mumbai'),

( 6, 'Goa'),

( 7,'Agra'),

( 8, 'Udaipur'),

( 9, 'Rohtak'),

( 10, 'Chennai');

select* from dept_locations;

create table project(

Pname varchar(15) not null,

Pnumber int primary key not null,

Plocation varchar(15),

Dnum int ,

foreign key (Dnum) references department(Dnumber)

);

insert into project values('pro_1', 000, 'Delhi', 1),

('pro_2', 111, 'Chandigarh', 2),

('pro_3',222, 'Mumbai', 3),

('pro_4', 333, 'Shimla', 4),

('pro_5', 444, 'Hyderabad', 5),

('pro_6', 555, 'Lucknow', 6),


('pro_7', 666, 'Sonipat', 7),

('pro_8', 777, 'Rewari', 8),

('pro_9', 888, 'Noida', 9),

('pro_10', 999, 'Kolkata', 10);

select* from project;

create table works_on

( Essn char(9) ,

Pno int ,

primary key(Essn, Pno),

hours decimal(3,1) not null,

foreign key (Essn) references employee(Ssn),

foreign key (Pno) references project(Pnumber)

);

insert into works_on values

('a112', 0,3.5),

( 'a113',111, 6.0),

( 'a114',222,7.0),

( 'a121',333,4.0),

( 'a221',444,3.0),

( 'a300',555,5.0),

( 'a323',666,6.0),

( 'a421',777,7.0),

( 'a921',888,2.0),

( 'b321',999,1.0);

select* from works_on;


create table dependent(

Essn char(9) ,

Dependent_name varchar(15) not null,

Sex char,

Bdate date,

Relationship varchar(8),

primary key(Essn, Dependent_name),

foreign key (Essn) references employee(Ssn)

);

insert into dependent values

('a112', 'rohan', 'm', '2000-12-09', 'single'),

( 'a113', 'subham', 'm', '2000-12-08', 'married'),

( 'a114', 'anamika', 'f', '2000-12-07', 'married'),

('a121', 'ravina', 'f', '2000-12-06', 'single'),

('a221', 'naveen', 'm', '2000-12-04', 'married'),

( 'a300', 'aman', 'm', '2000-11-04', 'single'),

( 'a323', 'sanjana', 'f', '2000-11-09', 'married'),

('a421', 'kuldeep', 'm', '2000-10-09', 'single'),

( 'a921', 'riya', 'f', '2000-11-26', 'married'),

( 'b321', 'himanshi', 'f', '2000-03-09', 'single');

select* from dependent;

( select sum(salary), max(salary), min(salary), avg(salary)

from employee);

select sum(salary), min(salary), max(salary), avg(salary)


from employee , department

where dname='finance'and dno=dnumber;

select count(*)

from employee;

select count(*)

from employee, department

where dname='IT' and dno=dnumber ;

select count(distinct salary)

from employee;

select dno, count(*) , avg(salary)

from employee , department

group by dno;

select pnumber, pname, count(*)

from project

group by pnumber;

select pnumber, pname, count(*)

from project , works_on

where pnumber=pno

group by pnumber , pname

having count(*)>2;
select pnumber, pname, count(*)

from employee, project, works_on

where pnumber=pno and ssn=essn and dno=5

group by pnumber, pname;

select dnumber, count(*)

from department, employee

where dnumber=dno and salary>40000 and

dno in (select dno

from employee

group by dno

having count(*)>0)

group by Dnumber;

select distinct Pnumber

from project, department, employee

where Dnum= 7 and mgr_ssn='a112' and Lname='yadav'

union

select distinct Pnumber

from project, works_on, employee

where pnumber=8 and super_ssn='a300' and Lname='yadav';

select distinct Essn

from works_on

where (pno,hours) in(select pno, hours

from works_on
where Essn='a421');

select fname, lname

from employee

where salary > (select salary from employee where dno=5);

select e.fname, e.lname

from employee as e

where e.ssn in (select essn from dependent where e.fname=dependent_name and e.sex=sex);

select fname, minit, lname

from employee

where (select count(*)

from dependent

where employee.ssn=dependent.essn)=0;

select fname lname

from employee

where exist (select*

from dependent

where ssn=essn

and

exists (select *

from department

where ssn=mgr_ssn);

You might also like