TD SQL
TD SQL
1. Basic Orders
1.1. SELECT statement
4. There are four coding mistakes in this statement:
7. The HR department wants a query to display the last name, job code, hire date, and employee number
for each employee, with employee number appearing first. Provide an alias STARTDATE for the hire_date
column.
9. The HR department wants more descriptive column headings for its report on employees. Get the
statement from lab_02_01.sql in iSQL*Plus. Name the column headings Emp #, Employee and Title (case-
sensitive).
4. Create a report to display the last name, job id, and start date for the employees with the last names of
Matos and Taylor. Order the query in ascending order by start date.
8. Create a report to display the last name and job title of all employees who do not have a manager
14. Display the last name, job, and salary for all employees whose job is sales representative (SA_REP)
or stock clerk (ST_CLERK) and whose salary is not equal to $2,500, $3,500, or $7,000.
WHERE job_id IN ('SA_REP', 'ST_CLERK') AND salary NOT IN (2500, 3500, 7000);
2. Functions in SQL
2.1. Single Row Functions
2. The HR department needs a report to display the employee number, last name, salary, and salary
increased by 15.5% (expressed as whole number) for each employee. Label the column “New Salary”.
FROM employees;
4. Write a query that displays the last name (with the first letter uppercased and all other letters
lowercased) and the length of the last name for all employees whose name starts with the letter J, A, or
M. Give each column an appropriate label. Sort by employees’ last names.
FROM employees
6. The HR dept’ wants to find the length of employment for each employee. Display the last name and
calculate the number of months between today and the date on which the employee was hired. Order by
the number of months worked. Round up to a whole number.
9. 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”.
FROM employees;
12. 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.
FROM employees;
SELECT
ROUND(MAX(salary)) AS "Maximum",
ROUND(MIN(salary)) AS "Minimum",
ROUND(SUM(salary)) AS "Sum",
ROUND(AVG(salary)) AS "Average"
FROM employees;
7. Generalize the query so that the user in the HR department is prompted for a job title.
FROM employees
11. Create a query that will 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.
12. 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,
SUM(CASE WHEN department_id = 20 THEN salary END) AS "Dept_20",
SUM(CASE WHEN department_id = 50 THEN salary END) AS "Dept_50",
SUM(CASE WHEN department_id = 80 THEN salary END) AS "Dept_80",
SUM(CASE WHEN department_id = 90 THEN salary END) AS "Dept_90",
SUM(salary) AS "Total"
FROM employees WHERE department_id IN (20, 50, 80, 90)
GROUP BY job_id;
3. Retrieving Data from Several Tables
2. Write a query to display the last name, department number, and department name for all employees.
3.The HR department needs a report of employees in Toronto. Display the last name, job, department
number, and department name for all employees who work in Toronto.
SELECT e.last_name, e.job_id, e.department_id, d.department_name
FROM employees e, departments d, locations l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND l.city = 'Toronto';
5.Modify your script to display all employees including King, who has no manager
8. The HR department wants to determine the names of all employees who were hired after Davies.
Create a query to display the name and hire date of any employee hired after employee Davies.