Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
50 views

SQL Day2 2

The document contains instructions for 13 SQL and PL/SQL labs involving writing queries against employee data. The queries cover a range of concepts including aggregation, filtering, sorting, joins, subqueries, and creating tables. The goal of the labs is to practice selecting, filtering, grouping, aggregating data from related tables to answer business questions.

Uploaded by

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

SQL Day2 2

The document contains instructions for 13 SQL and PL/SQL labs involving writing queries against employee data. The queries cover a range of concepts including aggregation, filtering, sorting, joins, subqueries, and creating tables. The goal of the labs is to practice selecting, filtering, grouping, aggregating data from related tables to answer business questions.

Uploaded by

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

SQL and PL/SQL Labs

SQL (Day2):

1 Write a query that displays the difference between the highest and lowest salaries.
Label the column DIFFERENCE.

select max(salary) - min(salary) as Difference


from employees

2 From the employees table determine the number of managers without listing them.
Label the column Number of Managers. Hint: Use the MANAGER_ID column to
determine the number of managers.

select count(distinct manager_id)


from employees

3 Display the minimum, maximum, sum, and average salary for each job type. Label the
columns Maximum, Minimum, Sum, and Average, respectively. Round the average
salary to the nearest whole number.

select min(salary) as minimum ,


max(salary) as maximum,
sum(salary) as sum,
round(avg(salary),0)
from employees
group by job_id

4 Display the manager number and the salary of the lowest paid employee for that
manager. Exclude anyone whose manager is not known. Exclude any groups where the
minimum salary is $6,000 or less. Sort the output in descending order of salary.

select manager_id , min (salary)


from employees
where manager_id is not null
group by manager_id
having min(salary) > 6000
order by min(salary) desc

5 Write a query to display the last name, department number, and department name for
all employees.

select last_name, e.department_id, department_name


from employees e , departments d
where e.department_id = d.department_id
6 Write a query to display the last name, job id, department number, and department
name for all employees who work in Toronto

select last_name, job_id, e.department_id , city, department_name


from employees e, departments d, locations l
where e.department_id = d.department_id
and d.location_id = l.location_id
and lower(city) = 'toronto'

7 Display the names and hire dates for all employees who were hired before their
managers, along with their manager's names and hire dates. Label the columns
Employee, Emp Hired, Manager, and Mgr Hired, respectively.

select e.last_name as employee,


e.hire_date as EmpHired,
m.last_name as Manager,
m.hire_date as MgrHired
from employees e, employees m
where e.manager_id = m.employee_id
and e.hire_date < m.hire_date

8 Write a query to display each department’s name, location, number of employees, and
the average salary for all employees in that department. Label the columns Name,
Location, Number of People, and Salary, respectively. Round the average salary to two
decimal places.

select department_name as name ,


l.location_id as location,
count(employee_id) as num_of_employee,
round(avg(salary),2) as salary
from departments d, employees e, locations l
where e.department_id = d.department_id
and d.location_id = l.location_id
group by department_name , l.location_id

9 Create a query to display the employee numbers and last names of all employees who
earn more than the average salary. Sort the results in ascending order of salary.

select employee_id, last_name


from employees
where salary > (select avg(salary) from employees)
order by salary

10 Display the minimum salary in each department excluding the (minimum salary in the
company).

select department_id, min (salary)


from employees
group by department_id
having min(salary) > (select min(salary) from employees)

11 Create a query to display the employees that earn salary that is higher than the salary
of all the clerks. Sort results on salary from highest to lowest.\ note: use Multi-row sub
query.
select last_name
from employees
where salary > all (select salary from employees
where job_id like '%CLERK')
order by salary desc
12 Bonus:
Display the employee numbers, last names, and salaries of all employees who earn
more than the average salary and who work in a department with any employee with
the letter "u" in their name.

select employee_id, last_name, salary


from employees
where salary > (select avg(salary) from employees)
and department_id in (select department_id from employees
where last_name like '%u%' )

13 Create the following tables:

Students Courses
Student_id pk
Student_name not Course_id pk
null Course_name not
Address null
Bdate Credit_hour
Tel Unique

Students_Courses
Course_id
Student_id
Grade(0-100)
Reg_date

You might also like