Skip to content

Commit bb188e5

Browse files
MY SQL practice files
1 parent 4cee355 commit bb188e5

8 files changed

+355
-0
lines changed

sql.practice1.sql

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--1
2+
Select job_title,max_salary- min_salary as "Salary differences" from hr.jobs where max_salary BETWEEN 12000 and 18000;
3+
--2
4+
Select * from hr.employees where commission_pct is null and salary BETWEEN 7000 and 12000 and department_id not in(50,30,80);
5+
--3
6+
Select first_name||' '||last_name as "full_name", hire_date, commission_pct,email ||'-'|| phone_number,salary from hr.employees
7+
where salary>11000 order by "full_name" desc;
8+
--4
9+
Select first_name, last_name, salary from hr.employees where first_name like '%m' and hire_date< '05-JUN-10';
10+
--5
11+
Select first_name||' '||last_name as "full_name",email ||'-'|| phone_number as "Contact_detail",salary as "Remunaration" from hr.employees
12+
where salary not BETWEEN 9000 and 17000 and commission_pct is not null;
13+
--6
14+
Select * from hr.departments where department_id=20;
15+
--7
16+
Select * from hr.job_history order by employee_id desc, start_date asc;
17+
--8
18+
Select job_id, salary from hr.employees where phone_number like '515%' or phone_number like '590%' and hire_date>'31-DEC-03' order by hire_date,salary ;
19+
--9
20+
Select * from hr.employees where hire_date BETWEEN '01-Jan-01'and '31-Dec-01';
21+
--10
22+
Select first_name,last_name from hr.employees where hire_date not between '01-JAN-06' and '31-DEC-07';'31-dec-2005' and '01-jan-2008'
23+
24+
--11
25+
Select email,job_id,first_name from hr.employees where hire_date like '%2007' or hire_date like '%JAN%';
26+
--12
27+
Select * from hr.employees where hire_date>'31-Dec-07' or salary<10000;

sql.practice2.sql

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--1
2+
Select first_name||' '||last_name as "full name", salary, commission_pct*salary, hire_date from hr.employees where salary<10000;
3+
--2
4+
select DISTINCT city, location_id from hr.locations order by location_id asc;
5+
-- distinct ferqlendirir, location id e gore asc
6+
--3
7+
select first_name, hire_date, job_id from hr.employees where (job_id = 'IT_PROG' or job_id='SA_REP') and
8+
(hire_date between '01-JAN-2002' and '31-DEC-2005');
9+
--4
10+
Select job_title from jobs order by job_title desc;
11+
--5
12+
Select * from hr.employees where commission_pct is null and salary between 5000 and 10000 and department_id=30;
13+
--6
14+
Select first_name, last_name,hire_date from hr.employees where hire_date > '01-JAN-08';
15+
--7
16+
Select * from hr.employees where department_id in (150,160,170) ;
17+
--8
18+
Select * from hr.employees where first_name like 'S%' or last_name like 'S%';
19+
--9
20+
SELECT first_name, last_name FROM hr.employees WHERE INSTR(last_name,'b'> 3;
21+
--kecilmiyib 1 ci weekde

sql.practice3.sql

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--1. Display employees who joined in the month of May.
2+
Select first_name,hire_date from hr.employees where (extract(month from hire_date))=5 ;
3+
4+
-----------2. Display employees who joined in the current year.
5+
Select first_name as Name, hire_date from hr.employees
6+
where extract(year from hire_date)=extract(year from SYSDATE) ;
7+
8+
-----------3. Display the number of days between system date and 1st January 2011.
9+
Select round( sysdate -to_date('01-JAN-11','DD-MM-YYYY')) "number of days" from hr.employees;
10+
11+
--4. Display maximum salary of employees.
12+
Select max(salary) from hr.employees;
13+
14+
--5. Display number of employees in each department.
15+
Select count(*), department_id from hr.employees group by department_id;
16+
17+
---------6. Display number of employees who joined after 15th of month.
18+
Select count(*) from hr.employees
19+
where extract( day from hire_date)>15;
20+
21+
-----------7. Display average salary of employees in each department who have commission percentage.
22+
Select AVG(salary) "Average",department_id from hr.employees
23+
WHERE commission_pct is not null group by department_id ;
24+
25+
--8. Display job ID for jobs with average salary more than 10000.
26+
Select job_id from hr. employees group by job_id having avg(salary)>1000;
27+
28+
/*9. Display job ID, number of employees, sum of salary, and difference between the highest
29+
salary and the lowest salary of the employees for all jobs.*/
30+
Select job_id, count(*) NOE, sum(salary),max(salary)-min(salary)difference from hr.employees group by job_id;
31+
--10.Display manager ID and number of employees managed by the manager.
32+
Select manager_id, count(*) NOEm from hr.employees group by manager_id having manager_id is not null;
33+
34+
--11.Search for the key differences between CHAR and VARCHAR�data�types.

sql.practice4.sql

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--1. Show minimum, average and maximum salary in last 15 years according to job id.
2+
Select MIN(salary),round(AVG(salary)),MAX(salary),job_id
3+
from hr.employees where EXTRACT(year from sysdate)-EXTRACT(year from hire_date)>15 group by job_id;
4+
5+
--2. How many employees hired after 2005 for each department?
6+
Select count(*), department_id from hr.employees where hire_date> to_date('31-12-2005','dd.mm.yyyy') group by department_id;
7+
8+
/*3. Write a query to show departments in which the difference between maximum and minimum
9+
salary is greater than 5000.*/
10+
Select department_id from hr.employees group by department_id having max(salary)-min(salary)>5000;
11+
12+
/*4. Display salaries of employees who has not commission pact according to departments
13+
(without using where).*/
14+
Select sum(
15+
case
16+
when commission_pct is null then salary
17+
else 0
18+
end)
19+
from hr.employees group by department_id;

sql.practice5.sql

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--1. Display last name, job title of employees who have commission percentage and belongs to department 30.
2+
Select last_name,job_title from hr.employees emp left join hr.jobs jh on jh.job_id=emp.job_id
3+
where emp.commission_pct is not null and emp.department_id=30;
4+
--2. Display department name, manager name, and salary of the manager for all managers whose experience is more than 5 years.
5+
select dep.department_name, man.first_name || ' ' || man.last_name manager_name, man.salary
6+
from hr.departments dep
7+
left join hr.employees man on man.manager_id = dep.manager_id
8+
where extract(year from sysdate) - extract(year from man.hire_date) >= 5;
9+
10+
--3. Display employee name if the employee joined before his manager.
11+
SELECT emp.first_name "Employee name",
12+
emp.hire_date,
13+
man.first_name "Mananger name",
14+
man.hire_date
15+
FROM hr.employees emp
16+
LEFT JOIN hr.employees man ON emp.manager_id=man.employee_id
17+
where emp.hire_date < man.hire_date
18+
order by emp.employee_id;
19+
--4. Display employee name, job title for the jobs, employee did in the past where the job was done less than six months.
20+
select emp.first_name||' '|| emp.last_name "Employee name", jobs.job_title
21+
from hr.employees emp
22+
left join hr.job_history jh on emp.employee_id = jh.employee_id
23+
left join hr.jobs jobs on jh.job_id = jobs.job_id
24+
where months_between(sysdate, jh.start_date) < 6 ;
25+
--5. Display department name, average salary and number of employees with commission within the department.
26+
select dep.department_name,
27+
round(avg(emp.salary), 2) avg_salary,
28+
count(*)
29+
from hr.departments dep
30+
left join hr.employees emp on dep.department_id = emp.department_id
31+
where emp.commission_pct is not null
32+
group by dep.department_name;
33+
--6. Display employee name and country in which he is working.
34+
select emp.first_name||' '|| emp.last_name "Employee name",
35+
cnt.country_name "Country name"
36+
from hr.employees emp
37+
left join hr.departments dep on emp.department_id = dep.department_id
38+
left join hr.locations loc on dep.location_id = loc.location_id
39+
left join hr.countries cnt on loc.country_id = cnt.country_id ;

sql.practice6.sql

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--1. Display the first promotion year for each employee.
2+
Select emp.first_name, emp.employee_id,to_char (min(end_date),'YYYY') FPY
3+
from hr.employees emp left join hr.job_history jb on emp.employee_id=jb.employee_id group by emp.first_name, emp.employee_id;
4+
--2. Display location, city and department name of employees who have been promoted more than
5+
--once.
6+
Select dep.location_id, loc.city, dep.department_name from hr.employees emp inner join hr.departments dep on emp.department_id=dep.department_id
7+
inner join hr.locations loc on dep.location_id=loc.location_id where emp.employee_id in(
8+
Select employee_id from hr.job_history jh group by employee_id having count(*)>1);
9+
--3. Display minimum and maximum “hire_date” of employees work in IT and HR departments.
10+
Select min(hire_date),max(hire_date) from hr.employees emp left join hr.departments dep on dep.department_id=emp.department_id
11+
where department_name in ('Human Resources', 'IT')
12+
group by department_name;
13+
--4. Find difference between current date and hire dates of employees after sorting them by hire
14+
--date, then show difference in days, months and years.
15+
select first_name||' '||last_name name, hire_date, sysdate,
16+
extract(year from sysdate) - extract(year from hire_date) year_diff,
17+
round(MONTHS_BETWEEN(SYSDATE, hire_date), 2) month_diff,
18+
round(sysdate-hire_date, 2) day_diff
19+
from hr.employees;
20+
21+
--5. Find which departments used to hire earliest/latest.
22+
Select distinct department_name from hr.departments dep left join hr.employees emp on dep.department_id=emp.department_id where hire_date = (select min(hire_date) from hr.employees) or
23+
hire_date = (select max(hire_date) from hr.employees);
24+
25+
--6. Find the number of departments with no employee for each city.
26+
select loc.city, count(employee_id) no_employee
27+
from hr.locations loc
28+
left join hr.departments dep on loc.location_id = dep.location_id
29+
left join hr.employees emp on dep.department_id = emp.department_id
30+
group by loc.city
31+
having count(employee_id)=0;
32+
--7. Create a category called “seasons” and find in which season most employees were hired.
33+
select case when extract(month from hire_date) in(12, 1, 2) then 'Winter'
34+
when extract(month from hire_date) in(3, 4, 5) then 'Spring'
35+
when extract(month from hire_date) in(6, 7, 8) then 'Summer'
36+
when extract(month from hire_date) in(9, 10, 11) then 'Autumn'
37+
else 'N/A' end as seasons,
38+
count(distinct employee_id)
39+
from hr.employees
40+
group by case when extract(month from hire_date) in(12, 1, 2) then 'Winter'
41+
when extract(month from hire_date) in(3, 4, 5) then 'Spring'
42+
when extract(month from hire_date) in(6, 7, 8) then 'Summer'
43+
when extract(month from hire_date) in(9, 10, 11) then 'Autumn'
44+
else 'N/A' end ;
45+
46+
--8. Find the cities of employees with average salary more than 5000.
47+
Select loc.city,emp.first_name from hr.employees emp inner join hr.departments dep on emp.department_id=dep.department_id
48+
inner join hr.locations loc on dep.location_id=loc.location_id where avg(salary)>5000;
49+

sql.practice7.sql

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
-- 1. According to the given diagram create STUDENTS, ACTIVITIES and SCHEDULE tables.
2+
3+
4+
-- 2. Insert data into students table from employees table.
5+
6+
insert into hr.students (s_id, first_name, last_name, phone_number, email)
7+
select employee_id, first_name, last_name, phone_number, email
8+
from hr.employees;
9+
10+
11+
-- 3. Change phone number to ‘***’ for students with s_id > 200.
12+
13+
update students
14+
set phone_number = '***'
15+
where s_id > 200;
16+
17+
18+
-- 4. Update first name and last names of students in Upper cases.
19+
20+
update students
21+
set first_name = upper(first_name),
22+
last_name = upper(last_name);
23+
24+
25+
-- 5. Based on the students table populated with the following data, update the email to 'DSA' for all records whose s_id
26+
-- is greater than 150.
27+
28+
update students
29+
set email = 'DSA'
30+
where s_id > 150;
31+
32+
33+
-- 6. Create PROGRAMMERS table using records from EMPLOYEES where job_id contains ‘PROG’ substring
34+
35+
create table PROGRAMMERS as
36+
select *
37+
from hr.employees
38+
where job_id like '%PROG%';
39+
40+
41+
-- 7. Delete records from students table where s_id is between 150 and 160.
42+
43+
delete from students
44+
where s_id between 150 and 160;
45+
46+
47+
-- 8.
48+
-- a) Insert some date into SCHEDULE, then truncate and see results.
49+
50+
insert into schedule (s_id, a_id, s_date)
51+
values (123, 321, to_date('2023-05-25', 'YYYY-MM-DD'));
52+
insert into schedule (s_id, a_id, s_date)
53+
values (234, 432, to_date('2023-05-26', 'YYYY-MM-DD'));
54+
55+
select * from schedule;
56+
57+
truncate table SCHEDULE;
58+
59+
select * from schedule;
60+
61+
62+
-- b) Drop schedule table
63+
64+
drop table schedule;
65+
66+
select * from schedule;
67+
68+
69+
-- 9. For any date given, write a script to find:
70+
71+
-- a) The first and the last days of the next year;
72+
73+
select '01-01-'|| to_char(extract(year from sysdate)+1) as "First Day of Next Year",
74+
'31-12-'|| to_char(extract(year from sysdate)+1) as "Last Day of Next Year"
75+
from dual;
76+
77+
-- b) The first and the last days of the next month;
78+
79+
select last_day(trunc(sysdate, 'mm')) + 1 as "First_Day_Next_Month",
80+
last_day(add_months(trunc(sysdate, 'mm'), 1)) as "Last_Day_Next_Month"
81+
from dual;
82+
83+
-- c) The first and the last days of the previous month.
84+
85+
select last_day(add_months(trunc(sysdate, 'mm'), -2)) + 1 as "First_Day_Previous_Month",
86+
last_day(add_months(trunc(sysdate, 'mm'), -1)) as "Last_Day_Previous_Month"
87+
from dual;
88+
89+
90+
-- 10. Create a table named “Participants” which consists of first_name, last_name and salary (have to more than 10000).
91+
92+
create table PARTICIPANTS as
93+
select first_name, last_name, salary
94+
from hr.employees
95+
where salary > 10000;
96+
97+
select * from participants;
98+
99+

sql.practice8.sql

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
-- 1. Return the name of the employee with the lowest salary in department 90.
2+
3+
select min(salary)
4+
from hr.employees
5+
where department_id=90;
6+
7+
select* from hr.employees emp
8+
where salary = (select min(salary) from hr.employees where department_id=90)
9+
and department_id = 90 ;
10+
11+
12+
--2. Select the department name, employee name, and salary of all employees who work in the human resources or purchasing
13+
-- departments. Compute a rank for each unique salary in both departments.
14+
15+
SELECT
16+
department_id, last_name, salary,
17+
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary) DENSE_RANK,
18+
RANK() OVER (PARTITION BY department_id ORDER BY salary) RANK
19+
FROM employees
20+
WHERE department_id in (40, 60)
21+
ORDER BY DENSE_RANK, last_name;
22+
23+
24+
--3. Select the 3 employees with minimum salary for department id 50.
25+
26+
SELECT * from (select
27+
department_id, last_name, salary,
28+
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary) DRANK
29+
FROM employees
30+
WHERE department_id = 50)
31+
WHERE drank in (1, 2, 3);
32+
33+
34+
--4. Show first name, last name, salary and previously listed employee’s salary who works in “IT_PROG” over hire date.
35+
36+
select
37+
first_name, last_name, hire_date, salary, job_id,
38+
lag(salary, 1, 0) over (partition by job_id order by hire_date) previous
39+
from employees
40+
where job_id = 'IT_PROG';
41+
42+
43+
--5. Display details of current job for employees who worked as IT Programmers in the past.
44+
45+
select * from hr.jobs
46+
where job_id in
47+
(select job_id from employees where employee_id in
48+
(select employee_id from job_history where job_id='IT_PROG'));
49+
50+
-- Diger usul
51+
select * from JOBS J
52+
inner join hr.employees emp on j.job_id=emp.job_id
53+
inner join hr.job_history jh on jh.employee_id=emp.employee_id
54+
and jh.job_id='IT_PROG';
55+
56+
57+
--6. Make a copy of the employees table and update the salaries of the employees in the new table with the maximum salary
58+
-- in their departments.
59+
60+
update employees_copy ec set salary=(select max(salary) from hr.employees emp
61+
where emp.department_id=ec.department_id);
62+
63+
64+
--7. Make a copy of the employees table and update the salaries of the employees in the new table with a 30 percent increase.
65+
66+
update employees_copy ec set salary=1.3*salary;
67+

0 commit comments

Comments
 (0)