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

SQL Query

The document details creating and populating tables to represent departments, faculty, students, and subjects in a university. Tables are created for departments, faculty, students, with attributes like name, id, date of joining. Data is inserted and relationships between tables are established using foreign keys. Various queries are run to retrieve, update and aggregate data from these tables.

Uploaded by

Hades Wartune
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

SQL Query

The document details creating and populating tables to represent departments, faculty, students, and subjects in a university. Tables are created for departments, faculty, students, with attributes like name, id, date of joining. Data is inserted and relationships between tables are established using foreign keys. Various queries are run to retrieve, update and aggregate data from these tables.

Uploaded by

Hades Wartune
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT - 3

create table DEPARTMENT (


DeptCode varchar(4) PRIMARY KEY,
DeptName varchar(40) not null,
HOD varchar(4) not null
);

create table FACULTY (


FacultyCode varchar(4) PRIMARY KEY,
FacultyName varchar(20) not null,
DateOfJoin date not null,
DeptCode varchar(4) not null
);

INSERT INTO FACULTY VALUES ('F101', 'M.Sinha','2005-01-01','CSE');


INSERT INTO FACULTY VALUES ('F105', 'P.sarkar','2019-02-01','CSE');
INSERT INTO FACULTY VALUES ('F201', 'S.Mazumder','2005-09-15','IT');
INSERT INTO FACULTY VALUES ('F301', 'S.Mondal','2018-08-01','CSE');
INSERT INTO FACULTY VALUES ('F401', 'D.Majumdar','2003-01-12','IT');
INSERT INTO FACULTY VALUES ('F506', 'N.Biswas','2013-12-31','ECE');
INSERT INTO FACULTY VALUES ('F607', 'R.Paul','2007-04-10','EE');
INSERT INTO FACULTY VALUES ('F704', 'S.Sarkar','2012-01-01','IT');
INSERT INTO FACULTY VALUES ('F808', 'K.Das','2010-06-15','IT');
INSERT INTO FACULTY VALUES ('F901', 'R.Roy','2017-06-15','EE');
INSERT INTO FACULTY VALUES ('F902', 'R.Biswas','2018-06-15','ECE');

insert into DEPARTMENT values ('CSE','Computer Science and Engineering','F101');


insert into DEPARTMENT values ('ECE','Electronics Communication
Engineering','F506');
insert into DEPARTMENT values ('EE','Electrtical Engineering','F901');
insert into DEPARTMENT values ('IT','Information Technology','F201');

insert into STUDENT values ('012301', 123001, 'Ashish', 'Jadavpur', 24761892,


2003,'CSE', 3, '1981-06-01');

alter table STUDENT add constraint Departments foreign key(DeptCode) references


DEPARTMENT(DeptCode);

alter table FACULTY add constraint Cons_Faculty Check(DeptCode


IN('CSE','IT','EE','ECE'));

alter table FACULTY add contraint Con_Faculty Check(FacultyCode like 'F%');

alter table DEPARTMENT add constraint Fkey_HOD Foreign key(HOD) references


FACULTY(FacultyCode);

select FacultyName from FACULTY where DeptCode = "CSE";

select count(*) as 'number of faculty' from FACULTY where Deptcode=('IT');

select DeptName from DEPARTMENT order by HOD;

SELECT D.HOD,D.DeptName, F.FacultyName FROM DEPARTMENT D JOIN FACULTY F ON D.HOD =


F.FacultyCode;

select count( * )as "no of faculty" from FACULTY where month(DateOfJoin)= 8;


update FACULTY set salary=10000.00 where FacultyCode='F506';

SELECT d.DeptName from DEPARTMENT d where (select count(*) from FACULTY f where
d.DeptCode=f.DeptCode)>1;

select DeptName, Count(*) as FacultyCount from DEPARTMENT JOIN FACULTY on DEPARTMEN


T.DeptCode = FACULTY.DeptCode Group by DeptName Order by FacultyCount desc limit 1;

select timestampdiff(year,DateOfJoin,curdate()) from FACULTY order by DateOfJoin


limit 1;

SELECT FacultyName from FACULTY where(DATEDIFF(SYSDATE(),DateOfJoin)/365)>=5;

SELECT d.DeptName from DEPARTMENT d where (select count(*) from FACULTY f where
d.DeptCode=f.DeptCode)>1;

select FacultyName,DeptCode from FACULTY where Salary > 8000.00 and Salary <
12000.00;

ASSIGNMENT - 2

select * from STUDENT;

alter table STUDENT add Primary key(RegNo);

select Name,Address,YearofAdm from STUDENT;

select Name,Year from STUDENT where DeptCode="CSE";

select Name,DeptCode from STUDENT where Year = 3;

select Name from STUDENT where Name like '_a%';

select Name from STUDENT order by Name desc;

select Name,Address from STUDENT where BirthDate > '1981-06-01';

select Name from STUDENT where PhnNo is NULL;

alter table STUDENT add CHECK(Year>1 and Year<=4);

update STUDENT set Year = 5 where Year = 4;

ASSIGNMENT - 1

create table STUDENT (


RegNo varchar(6) Not null,
RollNo int(6) Not null,
Name varchar(10) Not null,
Address varchar(15) Not null,
PhoneNo int(10),
YearofAdm int(4) Not null,
DeptCode varchar(4) Not null,
Year int(1) Not null,
BirthDate date Not null);

insert into STUDENT values (012301, 123001, 'Ashish', 'Jadavpur',


24761892,2003,'CSE', 3, '1981- 06-01');
insert into STUDENT values (012315, 123015, 'Kamal', 'Kashba', 24424987,
2003,'CSE', 3, '1981-09-19');
insert into STUDENT values (012424, 124024, 'Ipsita', 'Kaikhali', 25739608,
2004,'CSE', 2, '1982-08-15');
insert into STUDENT values (012250, 122050, 'Anita', 'Hooghly', 36719695,
2002,'IT', 4, '1980-12-22');
insert into STUDENT values (012344, 123044, 'Biplab', 'Howrah', null, 2003,'IT',
3, '1982-01-03');
insert into STUDENT values (012357, 123057, 'Samik', 'Barasat', 25426742,
2003,'IT', 3, '1981-07-15');insert into STUDENT values (012419, 124019,
'Srija','Garia', 24755655, 2004,
'EE', 2, '1982-10-25');
insert into STUDENT values (012427, 124027, 'Saibal', 'Garia', 24753306,
2004,'ECE', 2, '1983-03-22');
insert into STUDENT values (012236, 122036, 'Santanu', 'DumDum', null,
2002,'ECE', 4, '1980-12-11');

ASSIGNMENT - 5

create table SUBJECT(


SubjectCode varchar(4) not null,
SubjectName varchar(20) not null,
Faculty varchar (4) not null,
FOREIGN KEY(Faculty) references FACULTY(FacultyCode)
);
insert into SUBJECT values ('IT50','RDBMS','F201');
insert into SUBJECT values ('CS45','DAA','F301');
insert into SUBJECT values ('CS35','DSA','F101');
insert into SUBJECT values ('EC21','AE','F506');
insert into SUBJECT values ('EE69','IM','F901');

select DeptName, count(*) as NumOfStudents from DEPARTMENT JOIN STUDENT on


STUDENT.DeptCode = DEPARTMENT.DeptCode group by STUDENT.DeptCode;

update FACULTY set Salary = Salary + 500;

select Name from STUDENT where Name like 'S%' union select FacultyName from
FACULTY where FacultyName like 'S%';

select FacultyName from FACULTY where DeptCode = "IT";

select Name from STUDENT where Address = "Kaikhali";

select HOD,FacultyName from DEPARTMENT JOIN FACULTY ON DEPARTMENT.HOD =


FACULTY.FacultyCode ;
alter table SUBJECT add column Department varchar(4);
alter table SUBJECT add column year varchar(1);

update SUBJECT set Department = 'IT', year = 3 where SubjectName = 'RDBMS';


update SUBJECT set Department = 'CSE’, year = 2 where SubjectCode = 'CS45';
update SUBJECT set Department = 'ECE', year = 1 where SubjectCode = 'EC21';
update SUBJECT set Department = 'IM', year = 4 where SubjectCode = 'EE69';
update SUBJECT set Department = 'CSE', year = 3 where SubjectCode = 'CS35';

select FacultyName from FACULTY group by FacultyName having avg(Salary) > (select
avg(salary) from FACULTY);

select FacultyName from FACULTY group by FacultyName having avg(Salary) > (select
avg(Salary) from FACULTY where DeptCode = 'CSE');

select max(Salary) as MaxSal, min(Salary) as MinSal from FACULTY;

select *from FACULTY order by Salary desc limit 1,1;

select FacultyName from FACULTY where FacultyCode Not in(select HOD from
DEPARTMENT);

select SubjectName from SUBJECT where Department = 'CSE' and year = '3';

SET SQL_MODE = ''; (need to use this to avod exception)


// select DeptName,Count(*) as FacultyCount from DEPARTMENT JOIN FACULTY on
DEPARTMENT.DeptCode = FACULTY.DeptCode Group by DeptName Order by FacultyCount desc
limit 1; (DOUBT) //

(CORRECTED nt much tho)

ASSIGNMENT - 6
//V_STD part:

create view V_STD as select * from STUDENT;


select * from V_STD;

insert into V_STD values (012363, 123011, 'Bishakh', 'Salt Lake',


23371987,2005,'IT', 2, '1982-05-01');

select * from V_STD where Name = 'Bishakh';

update V_STD set Address = 'Sector V' where RegNo = '12363';


select * from V_STD where RegNo = '12363';

//V_STD_2 part:

create view V_STD_2 as select RegNo,Name,Year,DeptCode from STUDENT;


select * from V_STD_2;

SET SQL_MODE = ''; insert into V_STD_2 values (5353, 'Kami', 2, 'CSE');

delete from V_STD_2 where Year = 4;


//V_FACULTY part

create view V_FACULTY as select FACULTY.FacultyName,FACULTY.DeptCode,DEPARTMENT.HOD


from FACULTY join DEPARTMENT on FACULTY.DeptCode = DEPARTMENT.DeptCode;

select * from V_FACULTY;

insert into V_FACULTY(FacultyName,DeptCode) values ('B.Bas','CSE'); // ITS ILLEGAL


LMAOOOO

update V_FACULTY set DeptCode = 'IT' where FacultyName = 'M.Sinha';

ASSIGNMENT - 7

select emp_name,salary, d.dep_name from employees e,department d WHERE e.dep_id =


d.dep_id;

select d.dep_location from employees e JOIN department d on e.dep_id = d.dep_id


where e.emp_id = 67832 ;

select count(*) as Empcount from employees e JOIN department d on e.dep_id =


d.dep_id where d.dep_name = 'FINANCE';

//In Question 4 there's no attribute named Manager name in the given table(s). only
manager id// select manager_id from employees where emp_name = 'SCARLET';

select emp_name from employees where hire_date > '1991-06-09';

select emp_name from employees where salary = (select Max(salary) from employees);

select dep_name from department JOIN employees on department.dep_id =


employees.dep_id group by department.dep_name;

SELECT d.dep_name,max(salary) from employees e JOIN department d on e.dep_id =


d.dep_id group by d.dep_name;

//In Question 9 No table/attribute provided for Salesman

You might also like