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 (20)

Pl lab solution
Pl lab solutionPl lab solution
Pl lab solution
 
Subqueries
SubqueriesSubqueries
Subqueries
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
SQL BASIC QUERIES SOLUTION ~hmftj
SQL BASIC QUERIES SOLUTION ~hmftjSQL BASIC QUERIES SOLUTION ~hmftj
SQL BASIC QUERIES SOLUTION ~hmftj
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
 
Entity relationship diagram (erd)
Entity relationship diagram (erd)Entity relationship diagram (erd)
Entity relationship diagram (erd)
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 

Similaire à Sql task answers

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
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answersNawaz Sk
 
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
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answersMichael Belete
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence PortfolioChris Seebacher
 
SQL practice questions set
SQL practice questions setSQL practice questions set
SQL practice questions setMohd Tousif
 
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
 

Similaire à Sql task answers (20)

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
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
 
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
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answers
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
SQL practice questions set
SQL practice questions setSQL practice questions set
SQL practice questions set
 
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
 

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

Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Dernier (20)

Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

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);