You are on page 1of 5

create table Employee (Employee_ID int identity(1,1) primary key,First_Name

char(20),Last_Name char(20),Salary int,Joining_Date Datetime,Department char(20));

insert into Employee(First_Name, Last_Name, Salary ,Joining_Date ,Department)


values
('Anika','Aorra',100000,'2020-02-14 9:00:00','HR'),
('Veena','Verma',80000,'2011-06-15 9:00:00','Admin'),
('Vishal','Singhal',300000,'2020-02-16 9:00:00','HR'),
('Sushanth','Singh',500000,'2020-02-17 9:00:00','Admin'),
('Bhupal','Bhatti',500000,'2011-06-18 9:00:00','Admin'),
('Dheeraj','Diwan',200000,'2011-06-19 9:00:00','Account'),
('Karan','Kumar',75000,'2020-01-14 9:00:00','Account'),
('Chandrika','Chauhan',90000,'2011-04-15 9:00:00','Admin');

create table Employee_Bonus(Emp_ID int, Amount int,Bonus_Date datetime


constraint FK_EmpID foreign key (Emp_ID) references dbo.Employee(Employee_ID)
);

insert into Employee_Bonus (Emp_ID,Amount,Bonus_Date)


values
((SELECT Employee_ID from dbo.Employee where First_Name='Anika'), 5000,'2020-02-16
0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Veena'),3000,'2011-06-16
0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Vishal'),4000,'2020-02-16
0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Anika'),4500,'2020-02-16
0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Veena'),3500,'2011-06-16
0:00:00');

create table Employee_Title(Emp_ref_ID int,Emp_Title varchar(20),Effective_Date


datetime);

insert into Employee_Title(Emp_ref_ID,Emp_Title,Effective_Date)


values
((SELECT Employee_ID from dbo.Employee where First_Name='Anika'),'Manager','2016-
02-20 0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Veena'),'Executive','2016-
06-11 0:00:00'),
((SELECT Employee_ID from dbo.Employee where
First_Name='Chandrika'),'Executive','2016-06-11 0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Bhupal'),'Manager','2016-
06-11 0:00:00'),
((SELECT Employee_ID from dbo.Employee where
First_Name='Sushanth'),'Asst.Manager','2016-06-11 0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Karan'),'Executive','2016-
06-11 0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Dheeraj'),'Lead','2016-06-
11 0:00:00'),
((SELECT Employee_ID from dbo.Employee where First_Name='Vishal'),'Lead','2016-06-
11 0:00:00');

select*from Employee;
select*from Employee_bonus;
select* from employee_title;

Tasks To Be Performed:
Display the “FIRST_NAME” from Employee table using the alias name as Employee_name.
Syntax:
select first_name employee_name from Employee;

2.Display “LAST_NAME” from Employee table in upper case.


Syntax:
select upper(Last_Name) from Employee;

3. Display unique values of DEPARTMENT from EMPLOYEE table.


Syntax:
select distinct(department) from employee;

4. Display the first three characters of LAST_NAME from EMPLOYEE table.


Syntax:
select left(last_name, 3) from Employee;

5. Display the unique values of DEPARTMENT from EMPLOYEE table and prints its
length.
Syntax:
select distinct (department),len(department) from Employee;

6. Display the FIRST_NAME and LAST_NAME from EMPLOYEE table into a single column AS
FULL_NAME.
Syntax:
select first_name+last_name as Full_Name from employee;

7. DISPLAY all EMPLOYEE details from the employee table order by FIRST_NAME
Ascending.
Syntax:
select* from employee order by(First_Name);

8. Display all EMPLOYEE details order by FIRST_NAME Ascending and DEPARTMENT


Descending.
Syntax:
select* from employee order by(First_Name),(department)desc;

9. Display details for EMPLOYEE with the first name as “VEENA” and “KARAN” from
EMPLOYEE table.
Syntax:
select * from employee where First_Nam in (‘Veena’,’Karan’);

10. Display details of EMPLOYEE with DEPARTMENT name as “Admin”.


Syntax:
select * from Employee where Department='admin'

11. DISPLAY details of the EMPLOYEES whose FIRST_NAME contains ‘V’.


Syntax:
select* from Employee where first_name like '%v%'
12. DISPLAY details of the EMPLOYEES whose SALARY lies between 100000 and 500000.
Syntax:
select* from Employee where Salary between 100000 and 500000
OR
select* from Employee where Salary>=100000 and Salary<=500000

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


Syntax:
select* from Employee where Joining_Date >='01 Feb 2020'

14. Display employee names with salaries >= 50000 and <= 100000.
Syntax:
select First_Name, Salary from Employee where Salary>= 50000 and Salary<= 100000

15. DISPLAY details of the EMPLOYEES who are also Managers.


Syntax:
select
employee_id,First_Name,Last_Name,Salary,Joining_Date,Department,emp_ref_id,Emp_titl
e
from
Employee E left join
Employee_Title T on E.employee_id=T.Emp_ref_ID where Emp_Title='manager'

17. DISPLAY duplicate records having matching data in some fields of a table.
Syntax:
select department count from Employee group by Department having count(*)>1

18. Display only odd rows from a table


Syntax:
select * from Employee where Employee_ID % 2 <> 0;

19. Clone a new table from EMPLOYEE table.


Syntax:
select* into C_Employee from Employee;
select* from C_Employee;

20. DISPLAY the TOP 2 highest salary from a table


Syntax:
select top 2 * from Employee
order by salary desc;

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


Syntax:
select * from Employee where Salary
in (select Salary from Employee
group by Salary having COUNT(*)>1
);

22.Display the second highest salary from a table.


Syntax:
select max(salary) from Employee where
Salary<(select max(salary) from Employee)

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


Syntax:
select top 50 percent * from employee;

24. Display the departments that have less than 4 people in it.
Syntax:
select department from Employee group by Department
having count(*)< 4

25. Display all departments along with the number of people in there.
Syntax:
select department, count(*) from Employee
group by Department

26 Display the name of employees having the highest salary in each department.
Syntax:
select first_name, salary, department from Employee
where Salary in(
select max(salary) from Employee
group by Department);

27 Display the names of employees who earn the highest salary.


Syntax:
SELECT first_name from Employee
where Salary in( select max(salary)
from Employee);

28 Display the average salaries for each department.


Syntax:
select avg(salary) as Avg_salary, Department from Employee group by Department

29 display the name of the employee who has got maximum bonus.
Syntax:
select E.first_name, E.last_name, B.amount from dbo.Employee E
INNER JOIN
(select Emp_ID, Amount, row_number() over (order by Amount desc) RN_Bonus from
dbo.Employee_Bonus )B
on E.Employee_ID = B.Emp_ID
where B.RN_Bonus = 1

30. Display the first name and title of all the employees.
Syntax:
select E.first_name, T.Emp_Title from dbo.Employee E
Inner join
(select Emp_ref_ID, EMP_Title from dbo.Employee_Title )T
on E.Employee_ID=T.Emp_ref_ID

THANK YOU.

You might also like