SlideShare une entreprise Scribd logo
1  sur  8
Human Resources (HR)
This is the schema that is used in this course. In the Human Resource (HR) records, each employee
has an identification number, email address, job identification code, salary, and manager. Some
employees earn commissions in addition to their salary.
The company also tracks information about jobs within the organization. Each job has an
identification code, job title, and a minimum and maximum salary range for the job. Some
employees have been with the company for a long time and have held different positions within the
company. When an employee resigns, the duration the employee was working for, the job
identification number, and the department are recorded.
The sample company is regionally diverse, so it tracks the locations of its warehouses and
departments. Each employee is assigned to a department, and each department is identified either
by a unique department number or a short name. Each department is associated with one location,
and each location has a full address that includes the street name, postal code, city, state or
province, and the country code.
In places where the departments and warehouses are located, the company records details such as
the country name, currency symbol, currency name, and the region where the country is located
geographically.
HR Schema:

COUNTRIES
DEPARTMENTS
EMPLOYEES
JOB_GRADES
JOB_HISTORY
JOBS
LOCATIONS
REGIONS

Task:

1)
Write a query that displays the last name (with the first letter in uppercase and all the other letters in
lowercase) and the length of the last name for all employees whose name starts with the letters “J,”
“A,” or “M.” (Use Employees table)

SELECT INITCAP(last_name) "Name",
LENGTH(last_name) "Length"
FROM employees
WHERE last_name LIKE 'J%'
OR last_name LIKE 'M%'
OR last_name LIKE 'A%'
ORDER BY last_name ;

2)
Write a query which will print the employees last_name, no.of months worked till date and their
manager name.
Label the columns with the following: Employee – Experience in months – Manager Id

SELECT last_name “Employee”,
ROUND(MONTHS_BETWEEN(SYSDATE, hire_date)) “Experience in months”, manager_id
“Manager Id”
FROM employees
ORDER BY 2;

3)
Create a query that displays the first eight characters of the employees’ last names and indicates the
amounts of their salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in
descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
SELECT rpad(last_name, 8)||' '||
rpad(' ', salary/1000+1, '*')
EMPLOYEES_AND_THEIR_SALARIES
FROM employees
ORDER BY salary DESC;

4)
Create a query to display the last name and the number of weeks employed for all
employees in department 90. Label the number of weeks column TENURE. Truncate
the number of weeks value to 0 decimal places. Show the records in descending order
of the employee’s tenure.




SELECT last_name, trunc((SYSDATE-hire_date)/7) AS TENURE
FROM employees
WHERE department_id = 90
ORDER BY TENURE DESC;

5)
Display each employee’s last name, hire date, and salary review date, which is the first Monday after
six months of service. Label the column REVIEW. Format the dates to appear in the format similar to
“Monday, the Thirty-First of July, 2000.”




SELECT last_name, hire_date,
TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6),'MONDAY'),
'fmDay, "the" Ddspth "of" Month, YYYY') REVIEW
FROM employees;

6)
Create a report to 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;

7)
Create a query to display the total number of employees and, of that total, the number
of employees hired in 1995, 1996, 1997, and 1998. Create appropriate column
headings.




SELECT COUNT(*) total,
SUM(DECODE(TO_CHAR(hire_date,
'YYYY'),1995,1,0))"1995",
SUM(DECODE(TO_CHAR(hire_date,
'YYYY'),1996,1,0))"1996",
SUM(DECODE(TO_CHAR(hire_date,
'YYYY'),1997,1,0))"1997",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1998,1,0))"1998"
FROM employees;

8)
Create a matrix query to display the job, the salary for that job based on department
number, and the total salary for that job, for departments 20, 50, 80, and 90, giving
each column an appropriate heading.
SELECT job_id "Job",
SUM(DECODE(department_id          ,   20,   salary))   "Dept   20",
SUM(DECODE(department_id          ,   50,   salary))   "Dept   50",
SUM(DECODE(department_id          ,   80,   salary))   "Dept   80",
SUM(DECODE(department_id          ,   90,   salary))   "Dept   90",
SUM(salary) "Total"
FROM employees
GROUP BY job_id;

9)
Create a query that displays the employee last name, job title, department name, salary, and grade for
all employees. (Tables to be used Employees, Departments, Jobs)

SELECT e.last_name, e.job_id, d.department_name,
e.salary, j.grade_level
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
JOIN job_grades j
ON (e.salary BETWEEN j.lowest_sal AND j.highest_sal);

10)
Write a query to find the names and hire dates of all the employees who were hired before their
managers, along with their managers’ names and hire dates.
SELECT w.last_name, w.hire_date, m.last_name, m.hire_date
FROM employees w JOIN employees m
ON (w.manager_id = m.employee_id)
WHERE w.hire_date < m.hire_date;


11)
Write a query that displays the employee number and last name of all employees who
work in a department with any employee whose last name contains the letter “u.”

SELECT employee_id, last_name
FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name like '%u%');

12)
Create a table MY_EMPLOYEE which is a replicate of 'EMPLOYEES' table with all the data.

CREATE TABLE MY_EMPLOYEE AS SELECT * FROM EMPLOYEES;

13)
Add a new column USER_ID to MY_EMPLOYEE table and enforce a unique constraint.

ALTER TABLE MY_EMPLOYEE ADD USER_ID VARCHAR2(50);

14)
Update the USER_ID column of MY_EMPLOYEE table with the following format.
user_id should contain first letter of the last_name concatenated with first_name of the employee and
the maximum length of the user_id should be 8.
UPDATE MY_EMPLOYEE a
SET user_id = (SELECT substr(substr(last_name,1,1)||substr(first_name,1,7),1,8)
                  FROM MY_EMPLOYEE b
                 WHERE b.employee_id = a.employee_id);
15)
Change the salary to $1,000 for all employees who have a salary less than $900 in MY_EMPLOYEE
table.

UPDATE my_employee
SET salary = 1000
WHERE salary < 900;

16)
Create a view named DEPT50 that contains the employee numbers, employee last names, and
department numbers for all employees in department 50. Label the view columns EMPNO, EMPLOYEE,
and DEPTNO. For security purposes, do not allow an employee to be reassigned to another department
through the view.
CREATE VIEW dept50 AS
SELECT employee_id empno, last_name employee,
department_id deptno
FROM employees
WHERE department_id = 50
WITH CHECK OPTION CONSTRAINT emp_dept_50;

17)
Show all employees who were hired on the day of the week on which the highest number of employees
were hired.

SELECT last_name, TO_CHAR(hire_date, 'DAY') day
FROM employees
WHERE TO_CHAR(hire_date, 'Day') =
(SELECT TO_CHAR(hire_date, 'Day')
FROM employees
GROUP BY TO_CHAR(hire_date, 'Day')
HAVING COUNT(*) = (SELECT MAX(COUNT(*))
FROM employees
GROUP BY TO_CHAR(hire_date,
'Day')));

18)
Write a query to get the nth highest salary among the employees.
Query should accept a value for n.
If n = 2, then it should retrieve all the employee names who are earning 2nd highest salary among the
employees.

select distinct
    a.sal
,   a.ename
from emp a
where n = ( select count(distinct(sal))
         from emp b
         where a.sal <= b.sal
       );
or

SELECT department_id, first_name, last_name, salary
FROM
(
SELECT
department_id, first_name, last_name, salary,
ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary desc) rn
FROM employees
)
WHERE rn <= n
ORDER BY department_id, salary DESC, last_name;

19)
Suppose the primary key constraint has been disabled on employee_id column and users have entered
duplicate employee_id's within the employee table.
Write a query to remove the duplicate rows from the table and preserve the original employee_id which
was existing.

DELETE FROM employees
WHERE ROWID NOT IN (SELECT MIN(ROWID)
                       FROM employees
                      GROUP BY employee_id);

Contenu connexe

Tendances

Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting dataSyed Zaid Irshad
 
SQL practice questions set
SQL practice questions setSQL practice questions set
SQL practice questions setMohd Tousif
 
SQL Queries - DDL Commands
SQL Queries - DDL CommandsSQL Queries - DDL Commands
SQL Queries - DDL CommandsShubhamBauddh
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesMohammad Imam Hossain
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answersvijaybusu
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functionsNitesh Singh
 
List Processing in ABAP
List Processing in ABAPList Processing in ABAP
List Processing in ABAPsapdocs. info
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functionssinhacp
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries LectureFelipe Costa
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL CommandsShrija Madhu
 
Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Sanjay Pathak
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Punjab University
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and OperatorsMohan Kumar.R
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joinsDeepthi Rachumallu
 

Tendances (20)

Sql queires
Sql queiresSql queires
Sql queires
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
 
SQL practice questions set
SQL practice questions setSQL practice questions set
SQL practice questions set
 
SQL BASIC QUERIES SOLUTION ~hmftj
SQL BASIC QUERIES SOLUTION ~hmftjSQL BASIC QUERIES SOLUTION ~hmftj
SQL BASIC QUERIES SOLUTION ~hmftj
 
SQL Queries - DDL Commands
SQL Queries - DDL CommandsSQL Queries - DDL Commands
SQL Queries - DDL Commands
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related Queries
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
List Processing in ABAP
List Processing in ABAPList Processing in ABAP
List Processing in ABAP
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
 
AGGREGATE FUNCTION.pptx
AGGREGATE FUNCTION.pptxAGGREGATE FUNCTION.pptx
AGGREGATE FUNCTION.pptx
 
MySQL Functions
MySQL FunctionsMySQL Functions
MySQL Functions
 
Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
07.Advanced Abap
07.Advanced Abap07.Advanced Abap
07.Advanced Abap
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 

Similaire à Sql task answers

Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handoutsjhe04
 
Data Exploration with Apache Drill: Day 2
Data Exploration with Apache Drill: Day 2Data Exploration with Apache Drill: Day 2
Data Exploration with Apache Drill: Day 2Charles Givre
 
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole CollegeDivyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole Collegedezyneecole
 
Simran kaur,BCA Final Year 2015
Simran kaur,BCA Final Year 2015Simran kaur,BCA Final Year 2015
Simran kaur,BCA Final Year 2015dezyneecole
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxmetriohanzel
 
SQL case study Analysis PPT by Radhika Kashidd
SQL case study Analysis PPT by Radhika KashiddSQL case study Analysis PPT by Radhika Kashidd
SQL case study Analysis PPT by Radhika Kashiddradhikakashid25
 
Nikhil Khandelwal BCA 3rd Year
Nikhil Khandelwal BCA 3rd YearNikhil Khandelwal BCA 3rd Year
Nikhil Khandelwal BCA 3rd Yeardezyneecole
 
Add a new check on the emp table to make sure that any new employee in.docx
Add a new check on the emp table to make sure that any new employee in.docxAdd a new check on the emp table to make sure that any new employee in.docx
Add a new check on the emp table to make sure that any new employee in.docxwviola
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence PortfolioChris Seebacher
 
21390228-SQL-Queries.doc
21390228-SQL-Queries.doc21390228-SQL-Queries.doc
21390228-SQL-Queries.docSatishReddy212
 
Apurv Gupta, BCA ,Final year , Dezyne E'cole College
 Apurv Gupta, BCA ,Final year , Dezyne E'cole College Apurv Gupta, BCA ,Final year , Dezyne E'cole College
Apurv Gupta, BCA ,Final year , Dezyne E'cole Collegedezyneecole
 
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxMSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxgilpinleeanna
 
Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Mohd Tousif
 
Vishwajeet Sikhwal ,BCA,Final Year 2015
Vishwajeet Sikhwal ,BCA,Final Year 2015Vishwajeet Sikhwal ,BCA,Final Year 2015
Vishwajeet Sikhwal ,BCA,Final Year 2015dezyneecole
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Finalmukesh24pandey
 

Similaire à Sql task answers (20)

Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
 
Data Exploration with Apache Drill: Day 2
Data Exploration with Apache Drill: Day 2Data Exploration with Apache Drill: Day 2
Data Exploration with Apache Drill: Day 2
 
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole CollegeDivyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
Simran kaur,BCA Final Year 2015
Simran kaur,BCA Final Year 2015Simran kaur,BCA Final Year 2015
Simran kaur,BCA Final Year 2015
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptx
 
SQL case study Analysis PPT by Radhika Kashidd
SQL case study Analysis PPT by Radhika KashiddSQL case study Analysis PPT by Radhika Kashidd
SQL case study Analysis PPT by Radhika Kashidd
 
Nikhil Khandelwal BCA 3rd Year
Nikhil Khandelwal BCA 3rd YearNikhil Khandelwal BCA 3rd Year
Nikhil Khandelwal BCA 3rd Year
 
Add a new check on the emp table to make sure that any new employee in.docx
Add a new check on the emp table to make sure that any new employee in.docxAdd a new check on the emp table to make sure that any new employee in.docx
Add a new check on the emp table to make sure that any new employee in.docx
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
Ravi querys 425
Ravi querys  425Ravi querys  425
Ravi querys 425
 
21390228-SQL-Queries.doc
21390228-SQL-Queries.doc21390228-SQL-Queries.doc
21390228-SQL-Queries.doc
 
Vijay Kumar
Vijay KumarVijay Kumar
Vijay Kumar
 
Apurv Gupta, BCA ,Final year , Dezyne E'cole College
 Apurv Gupta, BCA ,Final year , Dezyne E'cole College Apurv Gupta, BCA ,Final year , Dezyne E'cole College
Apurv Gupta, BCA ,Final year , Dezyne E'cole College
 
Sql queries
Sql queriesSql queries
Sql queries
 
Dump Answers
Dump AnswersDump Answers
Dump Answers
 
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxMSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
 
Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1
 
Vishwajeet Sikhwal ,BCA,Final Year 2015
Vishwajeet Sikhwal ,BCA,Final Year 2015Vishwajeet Sikhwal ,BCA,Final Year 2015
Vishwajeet Sikhwal ,BCA,Final Year 2015
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 

Plus de Nawaz Sk

Plsql task
Plsql taskPlsql task
Plsql taskNawaz Sk
 
Oracle order management implementation manual
Oracle order management implementation manualOracle order management implementation manual
Oracle order management implementation manualNawaz Sk
 
Forall & bulk binds
Forall & bulk bindsForall & bulk binds
Forall & bulk bindsNawaz Sk
 
R12 Shipping Execution User guide
R12 Shipping Execution User guideR12 Shipping Execution User guide
R12 Shipping Execution User guideNawaz Sk
 

Plus de Nawaz Sk (7)

Tables
TablesTables
Tables
 
Plsql task
Plsql taskPlsql task
Plsql task
 
Oracle order management implementation manual
Oracle order management implementation manualOracle order management implementation manual
Oracle order management implementation manual
 
Forall & bulk binds
Forall & bulk bindsForall & bulk binds
Forall & bulk binds
 
R12 Shipping Execution User guide
R12 Shipping Execution User guideR12 Shipping Execution User guide
R12 Shipping Execution User guide
 
121poug
121poug121poug
121poug
 
121ontapi
121ontapi121ontapi
121ontapi
 

Dernier

Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscapingDr. M. Kumaresan Hort.
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsISCOPE Publication
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 

Dernier (20)

Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 

Sql task answers

  • 1. Human Resources (HR) This is the schema that is used in this course. In the Human Resource (HR) records, each employee has an identification number, email address, job identification code, salary, and manager. Some employees earn commissions in addition to their salary. The company also tracks information about jobs within the organization. Each job has an identification code, job title, and a minimum and maximum salary range for the job. Some employees have been with the company for a long time and have held different positions within the company. When an employee resigns, the duration the employee was working for, the job identification number, and the department are recorded. The sample company is regionally diverse, so it tracks the locations of its warehouses and departments. Each employee is assigned to a department, and each department is identified either by a unique department number or a short name. Each department is associated with one location, and each location has a full address that includes the street name, postal code, city, state or province, and the country code. In places where the departments and warehouses are located, the company records details such as the country name, currency symbol, currency name, and the region where the country is located geographically.
  • 2. HR Schema: COUNTRIES DEPARTMENTS EMPLOYEES JOB_GRADES JOB_HISTORY JOBS LOCATIONS REGIONS Task: 1) Write a query that displays the last name (with the first letter in uppercase and all the other letters in lowercase) and the length of the last name for all employees whose name starts with the letters “J,” “A,” or “M.” (Use Employees table) SELECT INITCAP(last_name) "Name", LENGTH(last_name) "Length" FROM employees WHERE last_name LIKE 'J%' OR last_name LIKE 'M%' OR last_name LIKE 'A%' ORDER BY last_name ; 2) Write a query which will print the employees last_name, no.of months worked till date and their manager name. Label the columns with the following: Employee – Experience in months – Manager Id SELECT last_name “Employee”, ROUND(MONTHS_BETWEEN(SYSDATE, hire_date)) “Experience in months”, manager_id “Manager Id” FROM employees ORDER BY 2; 3) Create a query that displays the first eight characters of the employees’ last names and indicates the amounts of their salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
  • 3. SELECT rpad(last_name, 8)||' '|| rpad(' ', salary/1000+1, '*') EMPLOYEES_AND_THEIR_SALARIES FROM employees ORDER BY salary DESC; 4) Create a query to display the last name and the number of weeks employed for all employees in department 90. Label the number of weeks column TENURE. Truncate the number of weeks value to 0 decimal places. Show the records in descending order of the employee’s tenure. SELECT last_name, trunc((SYSDATE-hire_date)/7) AS TENURE FROM employees WHERE department_id = 90 ORDER BY TENURE DESC; 5) Display each employee’s last name, hire date, and salary review date, which is the first Monday after six months of service. Label the column REVIEW. Format the dates to appear in the format similar to “Monday, the Thirty-First of July, 2000.” SELECT last_name, hire_date, TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6),'MONDAY'), 'fmDay, "the" Ddspth "of" Month, YYYY') REVIEW FROM employees; 6) Create a report to 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.
  • 4. 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; 7) Create a query to display the total number of employees and, of that total, the number of employees hired in 1995, 1996, 1997, and 1998. Create appropriate column headings. SELECT COUNT(*) total, SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1995,1,0))"1995", SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1996,1,0))"1996", SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1997,1,0))"1997", SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1998,1,0))"1998" FROM employees; 8) Create a matrix query to display the job, the salary for that job based on department number, and the total salary for that job, for departments 20, 50, 80, and 90, giving each column an appropriate heading.
  • 5. SELECT job_id "Job", SUM(DECODE(department_id , 20, salary)) "Dept 20", SUM(DECODE(department_id , 50, salary)) "Dept 50", SUM(DECODE(department_id , 80, salary)) "Dept 80", SUM(DECODE(department_id , 90, salary)) "Dept 90", SUM(salary) "Total" FROM employees GROUP BY job_id; 9) Create a query that displays the employee last name, job title, department name, salary, and grade for all employees. (Tables to be used Employees, Departments, Jobs) SELECT e.last_name, e.job_id, d.department_name, e.salary, j.grade_level FROM employees e JOIN departments d ON (e.department_id = d.department_id) JOIN job_grades j ON (e.salary BETWEEN j.lowest_sal AND j.highest_sal); 10) Write a query to find the names and hire dates of all the employees who were hired before their managers, along with their managers’ names and hire dates.
  • 6. SELECT w.last_name, w.hire_date, m.last_name, m.hire_date FROM employees w JOIN employees m ON (w.manager_id = m.employee_id) WHERE w.hire_date < m.hire_date; 11) Write a query that displays the employee number and last name of all employees who work in a department with any employee whose last name contains the letter “u.” SELECT employee_id, last_name FROM employees WHERE department_id IN (SELECT department_id FROM employees WHERE last_name like '%u%'); 12) Create a table MY_EMPLOYEE which is a replicate of 'EMPLOYEES' table with all the data. CREATE TABLE MY_EMPLOYEE AS SELECT * FROM EMPLOYEES; 13) Add a new column USER_ID to MY_EMPLOYEE table and enforce a unique constraint. ALTER TABLE MY_EMPLOYEE ADD USER_ID VARCHAR2(50); 14) Update the USER_ID column of MY_EMPLOYEE table with the following format. user_id should contain first letter of the last_name concatenated with first_name of the employee and the maximum length of the user_id should be 8. UPDATE MY_EMPLOYEE a SET user_id = (SELECT substr(substr(last_name,1,1)||substr(first_name,1,7),1,8) FROM MY_EMPLOYEE b WHERE b.employee_id = a.employee_id);
  • 7. 15) Change the salary to $1,000 for all employees who have a salary less than $900 in MY_EMPLOYEE table. UPDATE my_employee SET salary = 1000 WHERE salary < 900; 16) Create a view named DEPT50 that contains the employee numbers, employee last names, and department numbers for all employees in department 50. Label the view columns EMPNO, EMPLOYEE, and DEPTNO. For security purposes, do not allow an employee to be reassigned to another department through the view. CREATE VIEW dept50 AS SELECT employee_id empno, last_name employee, department_id deptno FROM employees WHERE department_id = 50 WITH CHECK OPTION CONSTRAINT emp_dept_50; 17) Show all employees who were hired on the day of the week on which the highest number of employees were hired. SELECT last_name, TO_CHAR(hire_date, 'DAY') day FROM employees WHERE TO_CHAR(hire_date, 'Day') = (SELECT TO_CHAR(hire_date, 'Day') FROM employees GROUP BY TO_CHAR(hire_date, 'Day') HAVING COUNT(*) = (SELECT MAX(COUNT(*)) FROM employees GROUP BY TO_CHAR(hire_date, 'Day'))); 18) Write a query to get the nth highest salary among the employees. Query should accept a value for n. If n = 2, then it should retrieve all the employee names who are earning 2nd highest salary among the employees. select distinct a.sal , a.ename from emp a where n = ( select count(distinct(sal)) from emp b where a.sal <= b.sal );
  • 8. or SELECT department_id, first_name, last_name, salary FROM ( SELECT department_id, first_name, last_name, salary, ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary desc) rn FROM employees ) WHERE rn <= n ORDER BY department_id, salary DESC, last_name; 19) Suppose the primary key constraint has been disabled on employee_id column and users have entered duplicate employee_id's within the employee table. Write a query to remove the duplicate rows from the table and preserve the original employee_id which was existing. DELETE FROM employees WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM employees GROUP BY employee_id);