k224111463 - bùi Thị Hồng Thi (4) - Lab 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

LAB PRACTICING 03 – FUNCTION

1. Display employee_id, last_name, salary, salary has been increased by 10% and
rounded and labeled by “New Salary”, increased amount of old salary and new
salary, labeled by “Increased Amount”

Convert, Round

SELECT EMPLOYEE_ID, LAST_NAME, SALARY,

CONVERT(int, ROUND(SALARY * 1.1, 0)) AS "New_Salary",

ROUND(SALARY * 1.1, 0) - SALARY AS "Increased Amount"

FROM EMPLOYEES;

2. Display employee_id, last_name, calculate the number of employee's working


years, assume until 2000, label the column as “Year of working”. Sort the
results by the number of working months.

SELECT EMPLOYEE_ID, LAST_NAME,

DATEDIFF(YEAR, HIRE_DATE, '2000-01-01') AS 'Year of working'


FROM EMPLOYEES

ORDER BY 'Year of working' DESC;

3. Display employee_id, last_name, hire_date and the day of the week of


hire_date (e.g. Sunday). Name the column “Day of Hire”. Sort results by day of
the week, starting with Monday

sắp xếp: ORDER BY

1001 bc 1999-01-01 Monday


102 Monday
103 Tuesday
104 Tuesday
105 Tuesday
SELECT EMPLOYEE_ID, LAST_NAME, HIRE_DATE,

DATENAME(WEEKDAY, HIRE_DATE) AS 'DAY_OF_HIRE'

FROM EMPLOYEES

ORDER BY DATEPART(W, DATEADD(W, -1, HIRE_DATE))


4. Write a query to display employee last_name, hire_date, date of salary
review (first Monday after 6 months of working). Labeled column as
“Salary review”:

SELECT LAST_NAME, HIRE_DATE,

CASE

WHEN DATEPART(WEEKDAY, DATEADD(MONTH, 6, HIRE_DATE)) = 1


THEN DATEADD(DAY, 1, DATEADD(MONTH, 6, HIRE_DATE))

ELSE DATEADD(DAY, 9 - DATEPART(WEEKDAY, DATEADD(MONTH,


6, HIRE_DATE)), DATEADD(MONTH, 6, HIRE_DATE))

END AS SALARY_REVIEW

FROM EMPLOYEES;
5. Write a query toL display employee_id, last_name, commission_pct. If
the employee has no commission_pct, the content is displayed as “With no
commission”, labeled as “Information of Comission”

1001 Nguyen 0.1


1001 Nguyen With no commission

SELECT EMPLOYEE_ID, LAST_NAME,

ISNULL(CAST(COMMISSION_PCT AS VARCHAR(20)), 'With no commission')


AS [Information of Commission]

FROM

EMPLOYEES;
6. Suppose the JOB_ID must always have 10 characters. If a JOB_ID has less
than 10 characters, pad leading *, so that the length of the JOB_ID always has
10 characters. Write a query to display employee_id, last_name, job_id, new
job_id, which always has 10 characters.

SELECT EMPLOYEE_ID, LAST_NAME, JOB_ID,

LEFT(JOB_ID + REPLICATE('*', 10), 10) AS new_job_id

FROM EMPLOYEES;
7. Display number of employees joined after 15th of the month.

SELECT COUNT(*)

FROM EMPLOYEES

WHERE DAY(HIRE_DATE) > 15;


8. Write a query to display the total number of distinct region in table
COUNTRIES.

SELECT COUNT(DISTINCT(REGION_ID)) FROM COUNTRIES


9. Write a query to display the average salary of employee from Department_ID
are 90 and 110

AVG(): tính trung bình

SELECT AVG(SALARY) AS "Average Salary"

FROM EMPLOYEES

WHERE DEPARTMENT_ID IN (90, 110);


10.Write a query to display the min salary and max salary among employees

MIN(), MAX()

SELECT MIN(SALARY) AS 'Minimum Salary', MAX(SALARY) AS 'Maximum


Salary'

FROM EMPLOYEES;
11.Display manager ID and number of employees managed by the manager.

SELECT MANAGER_ID,

COUNT(EMPLOYEE_ID) AS "Number of Employees Managed"

FROM EMPLOYEES

GROUP BY MANAGER_ID;

You might also like