0% found this document useful (0 votes)
148 views

Solved SQ L Dumps

The document provides examples of SQL queries using functions, joins, and subqueries to select, filter, and aggregate data from database tables. Some of the queries: - Calculate employee IDs with an offset based on their modulo, raise or lower salaries by a percentage, and convert salaries to hourly rates - Filter employees by salary thresholds, job IDs, names, and other attributes - Join tables to retrieve related data like department names, cities, and dates - Use subqueries to filter by conditions on related records

Uploaded by

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

Solved SQ L Dumps

The document provides examples of SQL queries using functions, joins, and subqueries to select, filter, and aggregate data from database tables. Some of the queries: - Calculate employee IDs with an offset based on their modulo, raise or lower salaries by a percentage, and convert salaries to hourly rates - Filter employees by salary thresholds, job IDs, names, and other attributes - Join tables to retrieve related data like department names, cities, and dates - Use subqueries to filter by conditions on related records

Uploaded by

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

Solved problems in sql using functions, joins, select in select

select employee_id, case when mod(employee_id, 2)=0 then employee_id +2


when mod(employee_id, 2)=1 then employee_id +1
end employee_id
from employees;

select salary as actual_sal, (salary*0.1) as RAISE, (salary+ salary * 0.1) as RAised_sal from employees;
--SELECT all employees from employees table as a select statistic with a salary raise of 10%. Please
retrieve the following in the result header columns: ACTUAL_SAL, RAISE, RAISED_SAL
--. SELECT all employees from employees table as a select statistic with a salary penalty of 6.3%. Please
retrieve the following in the result header columns: ACTUAL_SAL, penalty, RAISED_SAL
select salary as actual_sal, (salary*0.063) as penalty, (salary- salary * 0.063) as RAised_sal from
employees;
select FIRST_NAME, LAST_NAME, salary, (salary/160) as hourly_rate from employees;
--5.Create a query that would make unique pairs of employee's where job_id's are IT_PROG. Create a -
--statistical report on their salary differences by using substraction(-).
--Result header: employee_a, salary_a, employee_b ,salary_b total_salary_difference

HOMEWORK
2. select first_name, last_name, salary as total_salary, salary*12 as yearly_salary from employees where
salary*12>100000 order by total_salary desc;
3. SELECT first_name, last_name, salary, round((salary/12),2) as daily_salary from employees;
4. SELECT first_name, last_name,department_id from employees where department_id <>20;
5. select * from employees where EMPLOYEE_ID in (select MANAGER_ID from employees) and
salary>5000;
6. select * from employees where EMPLOYEE_ID in (select MANAGER_ID from employees) and
salary<20000 and job_id like '%FI%';
7. select * from employees where EMPLOYEE_ID in (select MANAGER_ID from employees) and
salary<=20000 and salary >=5000 and job_id like '%FI%';
8. SELECT first_name, last_name, salary, commission_pct from employees where commission_pct is not
null;
9. SELECT first_name, last_name, salary from employees where FIRST_NAME like '_an%';
10. SELECT first_name, last_name,job_id from employees where job_id not like '%MGR%' or job_id not
like '%PRESIDENT%' order by salary;
11 select first_name, last_name, hire_DATE from employees where HIRE_DATE not like '%05';
select first_name, last_name, hire_DATE from employees where HIRE_DATE <'01-JAN-05' or hire_date
>'31-DEC-05';
12. select first_name, last_name, hire_DATE from employees where HIRE_DATE in ('1-JUL-2006','24-
MAR-2007','04-JAN-2008') order by hire_date;
13. select first_name ||' ' || last_name || ' ' || job_id as employee from employees;
14. select first_name, last_name, hire_DATE from employees where HIRE_DATE between '01-Jan-2004'
and '31-Dec-2008' order by hire_date;
WILD CARDS! HOME WORK
4. select first_name, last_name from employees where length(FIRST_NAME)=5;
5. select first_name, last_name from employees where length(FIRST_NAME)=5 and first_name like 'A%';
6. select first_name, last_name from employees where length(FIRST_NAME)=5 and last_name like
'__s%';
7. select first_name, last_name from employees where length(FIRST_NAME)=5 and first_name like 'A%'
and first_name like '%n';
8. select first_name, last_name, hire_date from employees where hire_date like'__-_U%';
9. select first_name, last_name, salary from employees where length(salary)=4 and salary like '%0';
10. select first_name, last_name, salary from employees where first_name like '%ll%' or last_name like
'%ll%';
11. select first_name, last_name, salary from employees where first_name like '%l_x%';

--SLIDE 27
--1. Display the first name and salary for all employees who earn more than employee number 103
(Employees table).
select FIRST_NAME, SALARY from employees
where salary > (
select salary from employees where employee_id =103);

--2. Display the department number and department name for all departments whose location
number is equal to the location number of department number 90 (Departments table).
select department_id, DEPARTMENT_NAME from DEPARTMENTS
where location_id = (
select location_id from DEPARTMENTS where department_id = 90);

--3. the last name and hire date for all employees who was hired after employee number 101
(Employees table).
select last_name, hire_date from employees
where hire_date > (
select hire_date from employees where employee_id = 101);

--4. Display the first name, last name, and department number for all employees who work in Sales
department (Employees and Departments table).
select FIRST_NAME, last_name, DEPARTMENT_ID from employees
where DEPARTMENT_ID = (
select DEPARTMENT_ID from departments
where DEPARTMENT_name = 'Sales');

--5. Display the department number and department name for all departments located in Toronto
(Departments table).
select department_id, department_id from DEPARTMENTS
where location_id = (
select location_id from LOCATIONS where city = 'Toronto');

--6. Display the first name, salary and department number for all employees who work in the
department as employee number 124 (Employees table).
select first_name, salary, department_id from employees
where salary > (
SELECT round(AVG(salary),2) as avg FROM employees);

--7. Display the first name, salary, and department number for all employees
--who earn more than the average salary (Employees table).
select first_name, salary, department_id from EMPLOYEES
where salary >(
select round(avg(salary),2) from EMPLOYEES);

--8. Display the first name, salary, and department number for all employees
--whose salary equals one of the salaries in department number 20 (Employees table).
select first_name, salary, department_id from employees
where salary in (
select salary from employees where DEPARTMENT_ID = 20);

--9. Display the first name, salary, and department number for all employees
--who earn more than maximum salary in department number 50 (Employees table).
select first_name, salary, department_id from employees
where salary > (
select max(salary) from employees where DEPARTMENT_ID = 50);

--10. Display the first name, salary, and department number for all employees
--who earn more than the minimum salary in department number 60 (Employees table).
select first_name, salary, department_id from employees
where salary > (
select min(salary) from employees
where DEPARTMENT_ID = 60);

--11. Display the first name, salary, and department number for all employees who
--earn less than the minimum salary of department number 90 (Employees table).
select first_name, salary, department_id from employees
where salary < (
select min(salary) from employees
where DEPARTMENT_ID = 90);

--12. Display the first name, salary and department number for all employees whose
--department is located Seattle (Employees, Departments and Locations table).
select first_name, salary, department_id from employees
where department_id in (select department_id from DEPARTMENTS
where location_id = (
select location_id from locations
where city = 'Seattle'));

--13. Display the first name, salary, and department number for all employees
--who earn less than the average salary, and also work at the same department as
--employee whose first name is Kevin and last_name is Mourgos.
select first_name, salary, department_id from employees
where salary < (
SELECT round(AVG(salary),2) as avg FROM employees)
and department_id = (
select department_id from employees
where first_name='Kevin' and last_name = 'Mourgos');

--SLIDE 45
--Write a query in SQL to display the first name, last name, department number,
--and department name for each employee.
select first_name, last_name, e.department_id, d.DEPARTMENT_NAME from EMPLOYEES e
left join DEPARTMENTS d on d.DEPARTMENT_ID = e.DEPARTMENT_ID;

--Write a query in SQL to display the first and last name, department, city,
--and state province for each employee.
select first_name, last_name, e.department_id, d.DEPARTMENT_NAME, l.CITY, c.COUNTRY_NAME
--, r.REGION_NAME
from EMPLOYEES e
left join DEPARTMENTS d on d.DEPARTMENT_ID = e.DEPARTMENT_ID
left join LOCATIONS l on l.LOCATION_ID = d.LOCATION_ID
left join COUNTRIES c on c.COUNTRY_ID = l.COUNTRY_ID;
--left join REGIONS r on r.REGION_ID = c.REGION_ID;

--Write a query in SQL to display the first name, last name, salary,
--and job grade for all employees.
select first_name, last_name, e.SALARY, j.JOB_TITLE from EMPLOYEES e
left join Jobs j on j.JOB_ID = e.JOB_ID;
--Write a query in SQL to display the first name, last name, department number
--and department name, for all employees for departments 80 or 40.
select first_name, last_name, e.SALARY,e.department_id, d.DEPARTMENT_NAME from EMPLOYEES e
left join DEPARTMENTS d on d.DEPARTMENT_ID = e.DEPARTMENT_ID
where e.DEPARTMENT_ID in (80,40);

--Write a query in SQL to display those employees who contain a letter z


--to their first name and also display their last name, department, city, and state province.
select first_name, last_name, d.DEPARTMENT_NAME, l.CITY, c.COUNTRY_NAME
--r.REGION_NAME
from EMPLOYEES e
left join DEPARTMENTS d on d.DEPARTMENT_ID = e.DEPARTMENT_ID
left join LOCATIONS l on l.LOCATION_ID = d.LOCATION_ID
left join COUNTRIES c on c.COUNTRY_ID = l.COUNTRY_ID
--left join REGIONS r on r.REGION_ID = c.REGION_ID
where e.first_name like '%z%';

--Write a query in SQL to display all departments including those where


--does not have any employee.
select * from departments;

--Write a query in SQL to display the first and last name and salary for those
--employees who earn less than the employee earn whose number is 182.
select first_name, last_name, e.salary from EMPLOYEES e
where salary < (select salary from employees where EMPLOYEE_ID = 182);
SELECT E.first_name, E.last_name, E.salary
FROM employees E
JOIN employees e2
ON E.salary < e2.salary
AND e2.employee_id = 182;

--Write a query in SQL to display the department name, city, and state province for each department.
select d.DEPARTMENT_NAME,l.CITY, c.COUNTRY_NAME from departments d
left join LOCATIONS l on l.LOCATION_ID = d.LOCATION_ID
left join COUNTRIES c on c.COUNTRY_ID = l.COUNTRY_ID;

--Write a query in SQL to display the first name, last name, department number
--and name, for all employees who have or have not any department.
select e.first_name, e.last_name,d.department_id, d.DEPARTMENT_NAME
from EMPLOYEES e
left join DEPARTMENTS d on d.DEPARTMENT_ID = e.DEPARTMENT_ID;

--SLIDE 46
--1. Write a query in SQL to display the first name, last name, and department
--number for those employees who works in the same department as the employee
--who holds the last name as Taylor.
SELECT E.first_name, E.last_name, E.DEPARTMENT_ID
FROM employees E
JOIN employees e2
ON E.department_id = e2.department_id
AND e2.last_name = 'Taylor';

--2. Write a query in SQL to display the job title, department name, full name
--(first and last name ) of employee, and starting date for all the jobs which
--started on or after 1st January, 1993 and ending with on or before 31 August, 1997.
select j.JOB_TITLE, d.department_name, e.first_name ||' ' || e.last_name as Employee_name,
e.HIRE_DATE
from employees e
full join jobs j on j.job_id = e.job_id
join departments d on d.department_id=e.department_id
where e.HIRE_DATE >= '01-JAN-03'
and e.hire_date<='31-AUG-07';

--3. Write a query in SQL to display job title, full name (first and last name ) of employee,
--and the difference between maximum salary for the job and salary of the employee.
select j.JOB_TITLE, e.first_name ||' ' || e.last_name as Employee_name,j.MAX_SALARY - e.SALARY
from employees e
join jobs j on j.job_id = e.job_id;

--4. Write a query in SQL to display the name of the department, average salary and
--number of employees working in that department who got commission.
select e.department_id,d.department_name, round(avg(e.salary),2), count(e.employee_id)
from employees e
join DEPARTMENTS d on d.department_id=e.department_id
where e.COMMISSION_PCT is not null
group by (e.department_id, d.DEPARTMENT_NAME);

--5. Write a query in SQL to display the full name (first and last name ) of employee,
--and job title of those employees who is working in the department which ID 80 and
--deserve a commission percentage.(nu inteleg ce inseamna deserve a commission
--percentage. E comisionul null?)
select e.first_name ||' ' || e.last_name as Employee_name,
j.JOB_TITLE, e.department_id, e.COMMISSION_PCT from employees e
join jobs j on j.job_id = e.job_id
where e.department_id = 80
and e.COMMISSION_PCT is not null;
--6. Write a query in SQL to display the name of the country, city,
--and the departments which are running there.
select c.COUNTRY_NAME, l.CITY, d.DEPARTMENT_NAME from COUNTRIES c
left join LOCATIONS l on l.COUNTRY_ID = c.COUNTRY_ID
left join departments d on d.location_id = l.LOCATION_ID
order by c.country_name;

--7. Write a query in SQL to display department name and the full name (first and last name)
--of the manager. I have duplicates
select d.DEPARTMENT_NAME, e1.first_name ||' ' || e1.last_name as Employee_name
from departments d
left join employees e1 on e1.employee_id = d.manager_id
order by employee_name desc;

--8. Write a query in SQL to display job title and average salary of employees.
select j.job_title, round(avg(e.salary),2) from employees e
join jobs j on j.JOB_ID=e.JOB_ID
group by (j.JOB_TITLE);

--9. Write a query in SQL to display the details of jobs which was done by any of
--the employees who is presently earning a salary on and above 12000.
select j.* from jobs j
join employees e on e.job_id = j.job_id
where e.salary>=12000;

--slide 47

--Write a query in SQL to display the country name, city, and number of those
--departments where at leaste 2 employees are working.
select c.country_name, l.city, count(d.department_id) from departments d
right join locations l on l.location_id = d.location_id
right join countries c on c.COUNTRY_ID = l.country_id
where d.department_id in (select department_id from employees
GROUP BY (department_id)
having count(employee_id)>2)
group by (c.country_name, l.city);

--Write a query in SQL to display the department name, full name (first and last name)
--of manager, and their city.
select d.DEPARTMENT_name,e1.FIRST_NAME ||' '||e1.LAST_NAME as Manager, l.city
from departments d
join employees e1
on e1.employee_id = d.manager_id
join locations l on l.location_id = d.location_id;

--Write a query in SQL to display the employee ID, job name, number of days worked in
--for all those jobs in department 80.
SELECT employee_id, job_title, end_date-start_date DAYS
FROM job_history
NATURAL JOIN jobs
WHERE department_id=80;

--Write a query in SQL to display the full name (first and last name), and salary of
--those employees who working in any department located in London.
select e1.first_name ||' ' || e1.last_name as Employee_name, salary
from employees e1
join departments d on d.DEPARTMENT_ID = e1.department_id
join LOCATIONS l on d.location_id = l.location_id
where l.city = 'London';

--Write a query in SQL to display full name(first and last name), job title, starting and
--ending date of last jobs for those employees with worked without a commission percentage.
select e.first_name ||' ' || e.last_name as Employee_name, j.job_title,
jh.START_DATE, jh.END_DATE, e.COMMISSION_PCT
from JOB_HISTORY jh
join JOBS j on j.JOB_ID = jh.JOB_ID
join employees e on e.EMPLOYEE_ID = jh.EMPLOYEE_ID
where e.COMMISSION_PCT is null;

--Write a query in SQL to display the department name and number of employees in each of the
department.
select d.department_name, count(e.employee_id) as employees_number
from employees e
right join departments d on d.department_id = e.department_id
group by (d.department_name)
order by employees_number desc;

--Write a query in SQL to display the full name (firt and last name ) of employee with ID
--and name of the country presently where (s)he is working.
select e.employee_id, e.first_name ||' ' || e.last_name as Employee_name, c.country_name
from employees e
join departments d on d.DEPARTMENT_ID = e.DEPARTMENT_ID
join locations l on l.LOCATION_ID = d.location_id
join countries c on c.COUNTRY_ID = l.COUNTRY_ID
order by c.country_name;

--Slide 53
--1. Return 2 different jobs where we could see min, max,
--avg salary from employees using union.
select avg(e.salary), j.job_title, j.min_salary, j.max_salary from employees e
join jobs j on j.job_id = e.job_id
where e.job_id='AC_ACCOUNT'
group by (j.job_title, j.min_salary, j.max_salary)
union
select avg(e.salary), j.job_title, j.min_salary, j.max_salary from employees e
join jobs j on j.job_id = e.job_id
where e.job_id='IT_PROG'
group by (j.job_title, j.min_salary, j.max_salary);

--2. Return first_name, last_name , phone number, for those employees who uses
-- different sets of phone numbers which contains 1343,1344,1345,1346 using UNION.
select e.FIRST_NAME, e.LAST_NAME, e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1343%' and rownum =1
union
select e.FIRST_NAME, e.LAST_NAME, e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1345%' and rownum =1
union
select e.FIRST_NAME, e.LAST_NAME, e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1344%' and rownum =1
union
select e.FIRST_NAME, e.LAST_NAME, e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1346 %' and rownum =1;

--3. a.remove the first_name , last_name, and order by from the previous query
select e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1343%'
union
select e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1345%'
union
select e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1344%'
union
select e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1346 %';

-- b.use substr(phone_number,8,4) as Search_criterias to display only the


--phone_numbers for each in each query written with the union.
select substr(phone_number,8,4) from employees e
where e.PHONE_NUMBER like '%1343%'
union
select substr(phone_number,8,4) from employees e
where e.PHONE_NUMBER like '%1345%'
union
select substr(phone_number,8,4) from employees e
where e.PHONE_NUMBER like '%1344%'
union
select substr(phone_number,8,4) from employees e
where e.PHONE_NUMBER like '%1346 %';

-- c. Explain what happed and why you see only one result from each one. Have no idea :? Why?
-- union - e ca un distinct: le uneste dar nu duplica

--4. a. Run the query from exercise 3 and instead of UNION use UNION ALL.
select e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1343%'
union all
select e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1345%'
union all
select e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1344%'
union all
select e.PHONE_NUMBER from employees e
where e.PHONE_NUMBER like '%1346 %';

-- b. Add back first_name, last_name, phone_number, keep the substr thing.


select first_name, last_name, phone_number, substr(phone_number,8,4)
from employees e
where e.PHONE_NUMBER like '%1343%'
union all
select first_name, last_name, phone_number, substr(phone_number,8,4)
from employees e
where e.PHONE_NUMBER like '%1345%'
union all
select first_name, last_name, phone_number, substr(phone_number,8,4)
from employees e
where e.PHONE_NUMBER like '%1344%'
union all
select first_name, last_name, phone_number, substr(phone_number,8,4)
from employees e
where e.PHONE_NUMBER like '%1346 %';

--5. using union all create pair of employees who have the following search criterias over the
phone_number
-- 1343.9 and 1343.8 ==> result into a single result this time HEADER :
first_name,last_name,phone_number
-- 1344.1 and 1344.3 ==> result into a single result this time HEADER :
first_name,last_name,phone_number
-- 1345.9 and 1345.8 ==> result into a single result this time HEADER :
first_name,last_name,phone_number
-- 1346.1 and 1346.2 ==> result into a single result this time HEADER :
first_name,last_name,phone_number
-- Each pair of employees who have the same search criteria to be in separate query using union ALL,
--joins --> same thing only different sinonyms( SELF JOIN(reflection join)).
select emp1.first_name , emp1.last_name, emp1.phone_number,
emp2.first_name , emp2.last_name, emp2.phone_number
from employees emp1
join employees emp2
on emp2.EMPLOYEE_ID < emp1.EMPLOYEE_ID
where substr(emp2.phone_number,8,6)= '1343.9'
and substr(emp1.phone_number,8,6)= '1343.8'
union all
select emp1.first_name , emp1.last_name, emp1.phone_number,
emp2.first_name , emp2.last_name, emp2.phone_number
from employees emp1
join employees emp2
on emp2.EMPLOYEE_ID < emp1.EMPLOYEE_ID
where substr(emp2.phone_number,8,6)= '1344.1'
and substr(emp1.phone_number,8,6)= '1344.3'
union all
select emp1.first_name , emp1.last_name, emp1.phone_number,
emp2.first_name , emp2.last_name, emp2.phone_number
from employees emp1
join employees emp2
on emp2.EMPLOYEE_ID < emp1.EMPLOYEE_ID
where substr(emp2.phone_number,8,6)= '1345.8'
and substr(emp1.phone_number,8,6)= '1345.9'
union all
select emp1.first_name , emp1.last_name, emp1.phone_number,
emp2.first_name , emp2.last_name, emp2.phone_number
from employees emp1
join employees emp2
on emp2.EMPLOYEE_ID < emp1.EMPLOYEE_ID
where substr(emp2.phone_number,8,6)= '1346.1'
and substr(emp1.phone_number,8,6)= '1346.2';

--6. Find the employees working in the department which will be returned from the
-- set opration using Minus.
-- employee_id first query must have 100,120,140
-- emloyee_id second query must be in 110,130,150
select department_id from employees where employee_id in (100,120,140)
minus
select department_id from employees where employee_id in (110,130,150);yee_id in (110,130,150);

You might also like