0% found this document useful (0 votes)
18 views3 pages

SQL NEW Assignment

The document contains SQL queries that create three database tables (Employee, Employee_Bonus, Employee_Title) and inserts data into them. It then lists 30 SQL queries that retrieve and analyze data from these tables, such as finding highest salaries, duplicate records, average salaries by department, and joining data from the tables.

Uploaded by

Mayuri Salankar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
18 views3 pages

SQL NEW Assignment

The document contains SQL queries that create three database tables (Employee, Employee_Bonus, Employee_Title) and inserts data into them. It then lists 30 SQL queries that retrieve and analyze data from these tables, such as finding highest salaries, duplicate records, average salaries by department, and joining data from the tables.

Uploaded by

Mayuri Salankar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 3

create table Employee(Employee_id INT,First_name varchar(20),Last_name

varchar(20),Salary INT,Joining_date datetime,Department varchar(10));

insert into Employee values(1,'Anika','Arora',100000,'2020-02-14 9:00:00','HR');


insert into Employee values(2,'Veena','Verma',80000,'2011-06-15 9:00:00','Admin');
insert into Employee values(3,'Vishal','Singhal',300000,'2020-02-16 9:00:00','HR');
insert into Employee values(4,'Sushanth','Singh',500000,'2020-02-17
9:00:00','Admin');
insert into Employee values(5,'Bhupal','Bhati',500000,'2011-06-18
9:00:00','Admin');
insert into Employee values(6,'Dheeraj','Diwan',200000,'2011-06-19
9:00:00','Account');
insert into Employee values(7,'Karan','Kumar',75000,'2020-01-14
9:00:00','Account');
insert into Employee values(8,'Chandrika','Chauhan',90000,'2011-04-15
9:00:00','Admin');

--Drop table Employee;

create table Employee_Bonus(Employee_ref_id INT,Bonus_Amount INT,Bonus_Date


datetime);

insert into Employee_Bonus values(1,5000,'2020-02-16 0:00:00');


insert into Employee_Bonus values(2,3000,'2011-06-16 0:00:00');
insert into Employee_Bonus values(3,4000,'2020-02-16 0:00:00');
insert into Employee_Bonus values(1,4500,'2020-02-16 0:00:00');
insert into Employee_Bonus values(2,3500,'2011-06-16 0:00:00');

--Drop table Employee_Bonus;

create table Employee_Title(Employee_ref_id INT,Employee_title


varchar(20),Affective_Date datetime);

insert into Employee_Title values(1, 'Manager','2016-02-20 0:00:00');


insert into Employee_Title values(2,'Executive','2016-06-11 0:00:00');
insert into Employee_Title values(8,'Executive','2016-06-11 0:00:00');
insert into Employee_Title values(5,'Manager','2016-06-11 0:00:00');
insert into Employee_Title values(4,'Asst. Manager','2016-06-11 0:00:00');
insert into Employee_Title values(7,'Executive','2016-06-11 0:00:00');
insert into Employee_Title values(6,'Lead','2016-06-11 0:00:00');
insert into Employee_Title values(3,'Lead','2016-06-11 0:00:00');

--Drop table Employee_Title;

--1 Display the �FIRST_NAME� from Employee table using the alias name as
Employee_name.
select First_name from Employee as Employee_name;

--2 Display �LAST_NAME� from Employee table in upper case


select upper (Last_name) from Employee;

--3 Display unique values of DEPARTMENT from EMPLOYEE table


select distinct Department from Employee;

--4 Display the first three characters of LAST_NAME from EMPLOYEE table.
select substring(Last_Name,1,3) from Employee;

--5 Display the unique values of DEPARTMENT from EMPLOYEE table and prints its
length.
select distinct(len(Department)),Department from Employee group by Department;

--6 Display the FIRST_NAME and LAST_NAME from EMPLOYEE table into a single column
AS FULL_NAME.
select concat(First_name,' ',Last_Name) as FULL_NAME from Employee;

--7 DISPLAY all EMPLOYEE details from the employee table order by FIRST_NAME
Ascending.
select * from Employee order by First_name asc;

--8 Display all EMPLOYEE details order by FIRST_NAME Ascending and DEPARTMENT
Descending.
select * from Employee order by First_name asc,Department desc;

--9 Display details for EMPLOYEE with the first name as �VEENA� and �KARAN� from
EMPLOYEE table.
select * from Employee where First_name='Veena' union select * from Employee where
First_name='Karan';

--10 Display details of EMPLOYEE with DEPARTMENT name as �Admin�.


select * from Employee where Department='Admin';

--11 DISPLAY details of the EMPLOYEES whose FIRST_NAME contains �V�.


select * from Employee where First_name like 'V%';

--12 DISPLAY details of the EMPLOYEES whose SALARY lies between 100000 and 500000.
select * from Employee where Salary between 100000 and 500000;

--13 Display details of the employees who have joined in Feb-2020.


select * from Employee where Month(Joining_date)=2 and Year(Joining_date)=2020;

--14 Display employee names with salaries >= 50000 and <= 100000.
select concat(First_name,' ',Last_Name) as Full_Name from Employee where Salary
between 50000 and 100000;

--16 DISPLAY details of the EMPLOYEES who are also Managers.


select e.* from Employee e,Employee_Title et where e.Employee_id=et.Employee_ref_id
and et.Employee_title='Manager';

--17 DISPLAY duplicate records having matching data in some fields of a table.
select Employee_title,Affective_Date from Employee_Title group by
Employee_title,Affective_Date having count(*)>1;

--18 Display only odd rows from a table.


select * from Employee where Employee_id in (select Employee_id from Employee where
Employee_id%2<>0);

--19 Clone a new table from EMPLOYEE table.


create table employee_clone LIKE Employee;
insert into employee_clone select * from Employee;
select * from employee_clone;

--20 DISPLAY the TOP 2 highest salary from a table.


select Salary from Employee order by Salary desc limit 1,2;

--21 DISPLAY the list of employees with the same salary.


select First_name from Employee where Salary in(select Salary from Employee e
where Employee.Employee_id<>e.Employee_id);
--22 Display the second highest salary from a table.
select distinct Salary from Employee order by Salary desc limit 1,1;

--23 Display the first 50% records from a table.


select * from Employee limit 4;

--24 Display the departments that have less than 4 people in it.
select Department,count(First_name) as cnt from Employee group by Department having
cnt<4;

--25 Display all departments along with the number of people in there.
select Department,count(First_name) as cnt from Employee group by Department;

--26 Display the name of employees having the highest salary in each department.
select concat(First_name,' ',Last_Name) as Full_Name,Department,Salary from
Employee where Salary in(select max(Salary) from Employee group by Department);

--27 Display the names of employees who earn the highest salary.
select concat(First_name,' ',Last_Name) as Full_Name from Employee where Salary
in(select max(Salary) from Employee);

--28 Display the average salaries for each department.


select Department,AVG(Salary) from Employee group by Department;

--29 display the name of the employee who has got maximum bonus.
select concat(e1.First_name,' ',e1.Last_Name) as Full_Name,e2.Bonus_Amount from
Employee e1,Employee_Bonus e2 where e1.Employee_id=e2.Employee_ref_id and
Bonus_Amount in (select max(Bonus_Amount) from Employee_Bonus);

--30 Display the first name and title of all the employees
select e1.First_name,e2.Employee_title from Employee e1,Employee_Title e2 where
e1.Employee_id=e2.Employee_ref_id;

You might also like