Lab 3 CT-23082
Lab 3 CT-23082
1. Write a query that displays the employee’s last names all in uppercase and the length of
the last names, for all employees whose name starts with S, A, or M. Give each column
an appropriate label. Sort the results by the employees’ last names.
Query:
SELECT UPPER(last_name), LENGTH(last_name) AS Length
FROM HR.EMPLOYEES
WHERE last_name LIKE 'S%' OR last_name LIKE 'M%' OR last_name LIKE 'A%'
ORDER BY last_name
Output:
2. For each employee, display the employee’s last name, and calculate the number of weeks
between today and the date the employee was hired. Label the column
WEEKS_WORKED . Order your results by the number of WEEKs employed. Round the
number of weeks up to the closest whole number.
Query:
SELECT last_name, ROUND(MONTHS_BETWEEN(SYSDATE,hire_date)*4) AS
Weeks_Worked
FROM HR.EMPLOYEES
ORDER By Weeks_Worked
Output:
3. Study the NULLIF Function and solve the following question:
Write a query to display: employee_id, salary, commission_pct, total_earnings, which is
calculated as:
total_earnings = salary + (salary×commission_pct)
If commission_pct is NULL, it should be treated as 0 to prevent calculation errors.
Query:
SELECT last_name, salary, NVL(commission_pct,0) AS Commission_pct, salary + (salary *
NVL(commission_pct, 0)) AS total_earnings
FROM HR.EMPLOYEES;
Output:
4. 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 similar to “Monday, the Thirty-First of July, 2000.
Query:
SELECT last_name,
TO_CHAR(hire_date, 'Day, "the" Ddsp "of" Month, YYYY') AS Hired,
TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6), 'Monday'), 'Day, "the" Ddsp "of"
Month, YYYY') AS Review
FROM HR.EMPLOYEES;
Output:
5. Create a query to display the first name and salary for all employees. Format the salary to
be 15 characters long, right-padded with &. Label the column SALARY.
Query:
SELECT first_name, RPAD(salary,15,'&') AS SALARY
FROM HR.EMPLOYEES;
Output:
6. Write a query that produces the following for each employee:
<employee last name> earns <salary> monthly but wants <3 times salary>. Label the
column Dream Salaries.
Query:
SELECT
CONCAT(
CONCAT(
CONCAT(last_name, ' earns '),
CONCAT(salary, ' monthly but wants ')), (salary * 3)) AS "Dream Salaries"
FROM HR.EMPLOYEES;
Output:
7. Create a query that displays the employees’ last names and indicates the amounts of their
annual salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in
descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
Query:
SELECT last_name || ' ' || RPAD('*', ROUND(salary / 1000), '*') AS
EMPLOYEES_AND_THEIR_SALARIES
FROM HR.EMPLOYEES
ORDER BY salary DESC;
Output:
8. Solve first 2 questions of “Advanced String Functions / Regex / Clause” section of SQL
50 Badge on leetcode.
1)
2)