This page provides answers for the SQL Problem sets.
SQL Exercise Set #5 | ||||||||
1. Display department locations with initial letters capitalized. | SELECT Left([Loc],1) & Right(LCase([loc]),Len([loc])-1) AS FormettedLoc FROM Dept; | |||||||
2. For each employee, calculate the number of months between today and the date the employee was hired. Order the result by the number of months employed. Round the number of months up to the closest whole number. | SELECT round(DateDiff("m",[HireDate],Now()),0) AS
monthsEmployed, Emp.Ename, Emp.EmpNo
FROM Emp ORDER BY round(DateDiff("m",[HireDate],Now()),0), monthsEmployed; |
|||||||
3. Display the employee name, total compensation, and hire date for all employees whose salary is greater than twice their commission. Format the hire date to look like "3rd of December, 1982." | ||||||||
|
||||||||
4. Display the employee name in lowercase and salary rounded to hundreds for all employees. | ||||||||
|
||||||||
5. Display the employee name and the decoded department number as follows: 10-Accounting, 20-Research, 30-Sales, 40-Operations. | SELECT Emp.Ename, [dept].[deptNo] & " - " & Left(UCase([dept].[Dname]),1) & Right(LCase([dept].[dname]),Len([dept].[dname])-1) AS Department
FROM Dept INNER JOIN Emp ON Dept.DeptNo = Emp.DeptNo ORDER BY Emp.Ename; Results are: |
|||||||
6. Display employee name, department number, current monthly salary, and the adjusted monthly salary to satisfy the following pay increase scenario: All employees in department 10 will receive a 5% increase, department 20 a 7% increase, and all others will remain the same. The proposed monthly salary column should read PROPOSED SALARY. | SELECT Emp.Ename, Emp.DeptNo, Emp.Sal, IIf([DeptNo]=10,[sal]*1.05,IIf([deptno]=20,[sal]*1.07,[sal])) AS [PROPOSED SALARY]
FROM Emp; Results: |
|||||||
7. Write a query that produces the following for each employee: ALLEN EARNS 1600 PER MONTH BUT WANTS 4800 | SELECT UCase([ename] & " earns " & [sal] & " per month but wants 4800") AS Message1
FROM Emp
ORDER BY emp.ename; Results: |
SQL Problem Sets |