SQL Lab Exercise For Day 3,4,5
SQL Lab Exercise For Day 3,4,5
2.Modify above query to add a column that subtracts the old salary from the new salary.
Label the column Increase
3.For each employee, display the employees last name, and calculate the number of
months between today and the date the employee was hired. Label the column
MONTHS_WORKED. Order your results by the number of months employed. Round the number of
months up to the closest whole number.
4.Create a query that displays employee last names, department numbers, and all the
employees who work in the same department as a given employee. Give each column an
appropriate label.
5. Display the names and hire dates for all employees who were hired before their
managers, along with their managers names and hire dates. Label the columns Employee,
Emp Hired, Manager, and Mgr Hired, respectively.
6. Write a query to display each departments name, location, number of employees, and
the average salary for all employees in that department. Label the columns Name,
Location, Number of People, and Salary, respectively. Round the average salary to two
decimal places.
7. Create a query to display the employee numbers and last names of all employees who
earn more than the average salary. Sort the results in ascending order of salary
Assignment II
Read and understand the following table structures. The 5 questions given in this paper are based on these
table structures.
TAB1: ITEM
Item_No
Item_Description
Item_Group
Item_Family
Item_Unit_Of_Measure
Item_Unit_Price
Number(5)
Varchar2(50)
Varchar2(10)
Varchar2(20)
Varchar2(5)
Number
Varchar2(5)
Varchar2(10)
Varchar2(10)
40
97
15
45
50
TAB3: FISCAL_CALENDAR
Fiscal_Date
Date
Fiscal_Week
Number
Fiscal_Month
Number
Fiscal_Qtr
Number
Fiscal_Year
Number
FISCAL_MONTH FISCAL_QTR
-------------------1
1
2
1
4
2
TAB4: PRODUCTION_DATA
Start_Date
Date
End_Date
Date
Location_Id
Varchar2(5)
Item_No
Number(5)
Production_Qty
Number
Not Null,
Not Null,
Not Null (Pk),
Not Null (Pk),
Not Null
ITEM_NO
------1
1
TAB5: PRODUCTION_MASTER
Production_Date
Date
Location_Id
Varchar2(5)
Item_No
Number(5)
Production_Qty
Number
Not Null,
Not Null (Pk),
Not Null (Pk),
Not Null
TAB6: CUSTOMER_MASTER
Cust_Id
Number(5)
Cust_Name
Varchar2(50)
Cust_Location
Varchar2(10)
TAB7: ORDER_HEADER
Order_Id
Number(5)
Order_Dt
Date
Order_Cust_Id
Number(5)
TAB8: ORDER_DETAIL
Order_Id
Item_No
Order_Qty
Number (5)
Number(5)
Number (5)
FISCAL_YEAR
2005
2005
2005
PRODUCTION_QTY
-------------180
90
Q1: Write sql to generate the total cost of all the item groups produced by region. Expected sample output is
given below:
REGION
ITEM_GROUP
TOTAL COST
-----------------------WEST
SNACKS
7200
SOUTH
SNACKS
3600
.....
.......... ....
NOTE: THE TOTAL COST IN THIS EXAMPLE IS THE (PRODUCTION_QTY * ITEM_UNIT_COST)
Q2: Write sql to generate the sales report by customer. Expected output is as follows:
CUST_NAME
------------
ITEM_NAME
-----
TOTAL_COST
----------
ITEM_NAME
PRODUCTION_QTY
------------------------------
SALES_QTY
NOTE: THE REPORT SHOULD INCLUDE REGIONS FOR WHICH THERE IS ONLY PRODUCTION
& NO SALES AND REGIONS WHICH ONLY HAS SALES BUT NO PRODUCTION
Q6: GENERATE LIST OF ITEMS, WHOSE TOTAL ORDER QTY LAST YEAR IS LESS THAN THE
TOTAL ORDER QTY THIS YEAR.
NOTE: DO NOT HARDCODE 2004 OR 2005 IN THE QUERY. LAST YEAR AND CURRENT YEAR
SHOULD BE POPULATED USING SYSDATE
Q7: WRITE AN SQL TO GENERATE THE DAILY PRODUCTION INFORMATION FROM THE
PRODUCTION_DATA TABLE:
PRODUCTION_DATE
LOCATION_NAME ITEM_NAME
DAILY_PRODUCTION_QTY
--------------01-JAN-05
02-JAN-05
03-JAN-05
.........
31-MAR-05
01-JAN-05
02-JAN-05
03-JAN-05
.........
31-MAR-05
------------BARODA
BARODA
BARODA
.......
BARODA
CHENNAI
CHENNAI
CHENNAI
.......
CHENNAI
--------CHIPS
CHIPS
CHIPS
......
CHIPS
CHIPS
CHIPS
CHIPS
......
CHIPS
-----
--------------2
2
2
..
2
1
1
1
..
1