0% found this document useful (0 votes)
6 views2 pages

SQL Queries - in

Sql

Uploaded by

Amarnarh A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

SQL Queries - in

Sql

Uploaded by

Amarnarh A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

List count of employees per country


-------------------
select c.country_name, count(e.employee_id) as EmpCount
from employees e
join departments d
on e.department_id = d.department_id
join locations l
d.location_id=l.location_id
join countries c
on l.country_id = c.country_id
group by c.country_name
---------------------------------------
----------------------------------

2.Give the country that has highest number of employees


----------------------
with countryrank as (
select country_name, Dense_Rank() over(order by EmpCount desc) as rk
from (
select c.country_name, count(e.employee_id) as EmpCount
from employees e
join departments d
on e.department_id = d.department_id
join locations l
d.location_id=l.location_id
join countries c
on l.country_id = c.country_id
group by c.country_name ) as sc
)
select country_name from countryrank where rk=1
------------------------------------------------------
-----------------------------------------------------
3.List employee’s manager details (managers name, Ph.No)
--------------------------------------------

select e.employee_id,
e.first_name as Employee_FirstName,
e.last_name as Employee_LastName,
m.first_name as Manager_FirstName,
m.last_name as Manager_LastName,
m.Phone_number as Manager_PhoneNumber
from Employees e
left join Employees m
on e.manager_id = m.employee_id
--------------------------------------------------
------------------------------------------------
4. List top earning employee in each department
-------------------------------------------

with EmpRanks as (
select d.department_name, e.employee_id, e.salary, Dense_Rank() over(partition by
d.department_name order by e.salary desc) as rk
from employees e
join departments d
on e.department_id = d.department_id
)
select department_name, Employee_id
from EmpRanks where rk=1
-----------------------------------
----------------------------------------
5. List duration of employment for each employee
------------------------------------------
select employee_id,
first_name as EmployeeFirstName,
datediff(day,hire_date,getdate()) as Duration
from employees
------------------------
select employee_id,
first_name as EmployeeFirstName,
CONCAT(
datediff(Year,hire_date,getdate()), 'Years',
datediff(Month,hire_date,getdate())%12, 'Months'
datediff(day,
dateadd(month,Datediff(month,hire_date,getdate()),
hire_date), getdate()), 'Days'
)as Duration
from employees

You might also like