Sie sind auf Seite 1von 7

############################## SQL ASSIGNMENT

######################################
ASSIGNMENT - LAB 1
[ EMPLOYEE NAME - ankit ]
[ EMPLOYEE ID - 142731
]
###################################################################################
###

1.1 : Data Query Language


1. Retrieve the details (Name, Salary and dept code) of the employees who are
working in department 20, 30 and 40.
ANS: SELECT STAFF_NAME, STAFF_SAL, DEPT_CODE FROM STAFF_MASTER WHERE DEPT_CODE IN
(20,30,40)
STAFF_NAME STAFF_SAL DEPT_CODE
-------------------------------------------------- ---------- ----------
Arvind 17000 30
Shyam 20000 20
Anil 20000 20
Allen 42000 30
Smith 62000 20
Raviraj 18000 40
Rahul 22000 20
Ram 32000 30
===================================================================================
============================================
2. List the details of the employees with user defined Column headers.

ans:
STAFF_ID NAME DESIGN_ID DEPI_ID
DOH MANAGER_ID SALARY
---------- -------------------------------------------------- ---------- ----------
--------- ---------- ----------
ADDRESS
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
100001 Arvind 102 30
15-JAN-03 100006 17000
Bangalore

100002 Shyam 102 20


17-FEB-02 100007 20000
Chennai

100003 Mohan 102 10


19-JAN-02 100006 24000
Mumbai

100004 Anil 102 20


11-MAR-01 100006 20000
Hyderabad

100005 John 106 10


21-JAN-01 100007 32000
Bangalore

100006 Allen 103 30


23-APR-01 100005 42000
Chennai

100007 Smith 103 20


12-MAR-02 100005 62000
Mumbai

100008 Raviraj 102 40


11-JAN-03 100006 18000
Bangalore

100009 Rahul 102 20


11-DEC-03 100006 22000
Hyderabad

100010 Ram 103 30


17-JAN-02 100007 32000
Bangalore
===================================================================================
============================================================================
3. Display the code, subjects and total marks for every student. Total Marks will
be calculated as Subject1+Subject2+Subject3. (Refer Student_Marks table)

ANS:
SQL> SELECT STUDENT_CODE,SUBJECT1,SUBJECT2,SUBJECT3,SUBJECT1+SUBJECT2+SUBJECT3 AS
TOTAL_MARKS FROM STUDENT_MARKS;

STUDENT_CODE SUBJECT1 SUBJECT2 SUBJECT3 TOTAL_MARKS


------------ ---------- ---------- ---------- -----------
1001 55 45 78 178
1002 66 74 88 228
1003 87 54 65 206
1004 65 64 90 219
1005 78 88 65 231
1006 65 86 54 205
1007 67 79 49 195
1008 72 55 55 182
1009 71 59 58 188
1010 68 44 92 204
1011 89 96 78 263
1012 78 56 55 189
1013 75 58 65 198
1014 73 74 65 212
1015 66 45 74 185
1016 68 78 74 220
1017 69 44 52 165
1018 65 78 56 199
1019 78 58 74 210
1020 45 55 65 165
1021 78 79 78 235
1001 68 44 92 204
1002 89 96 78 263
1003 78 56 55 189
1004 75 58 65 198
1005 73 74 65 212
1006 66 45 74 185
1007 68 78 74 220
1008 69 44 52 165
1009 65 78 56 199
1010 78 58 74 210
1011 45 55 65 165
1012 78 79 78 235
1013 66 74 88 228
1014 65 64 90 219
1015 78 88 65 231
1016 65 86 54 205
1017 67 79 49 195
1018 72 55 55 182
1019 71 59 58 188
1020 55 45 78 178
1021 87 54 65 206
1001 62 44 92 198
1002 89 96 78 263
1003 78 56 55 189
1004 75 58 65 198
1005 73 74 65 212
1006 66 45 74 185
1007 68 78 71 217
1008 69 44 52 165
1009 63 78 56 197
1010 78 54 74 206
1011 45 55 65 165
1012 75 79 71 225
1013 66 74 88 228
1014 65 64 90 219
1015 78 88 65 231
1016 61 81 54 196
1017 67 79 49 195
1018 72 55 55 182
1019 71 59 58 188
1020 50 45 78 173
1021 87 54 65 206
1001 62 44 91 197
1002 89 96 78 263
1003 78 59 59 196
1004 75 58 65 198
1005 73 74 65 212
1006 66 45 75 186
1007 68 78 41 187
1008 69 41 52 162
1009 63 78 56 197
1010 78 24 74 176
1011 45 55 65 165
1012 75 79 73 227
1013 68 74 78 220
1014 65 64 91 220
1015 78 89 65 232
1016 61 81 58 200
1017 67 79 49 195
1018 72 55 55 182
1019 71 59 58 188
1020 50 45 78 173
1021 87 54 65 206

84 rows selected.
===================================================================================
=======================================================================
4. List the details of the staff whose designations are either PROFESSOR or
LECTURER.
ANS:
SQL> SELECT STAFF_NAME,STAFF_SAL, DESIGN_NAME FROM STAFF_MASTER,DESIGNATION_MASTER
WHERE STAFF_MASTER.DESIGN_CODE=DESIGNATION_MASTER.DESIGN_CODE AND
DESIGNATION_MASTER.DESIGN_CODE IN (102,105);

STAFF_NAME STAFF_SAL DESIGN_NAME


-------------------------------------------------- ----------
--------------------------------------------------
Arvind 17000 Professor
Shyam 20000 Professor
Mohan 24000 Professor
Anil 20000 Professor
Raviraj 18000 Professor
Rahul 22000 Professor
===================================================================================
=======================================================================
5. List the code, name, and department number of the employees who have experience
of more than 18 years.

ANS:
SQL> SELECT STAFF_NAME FROM STAFF_MASTER WHERE MONTHS_BETWEEN (SYSDATE,HIREDATE)/12
>18 ;

no rows selected
===================================================================================
=======================================================================
6. List the name and Designations of the staff who have joined before Jan 2003.

ANS:
SQL> select STAFF_NAME,S.DESIGN_CODE, DESIGN_NAME from STAFF_MASTER S,
DESIGNATION_MASTER D where S.DESIGN_CODE=D.DESIGN_CODE AND S.hiredate < ('01-jan-
2003');

STAFF_NAME DESIGN_CODE DESIGN_NAME


-------------------------------------------------- -----------
--------------------------------------------------
Shyam 102 Professor
Mohan 102 Professor
Anil 102 Professor
John 106 Director
Allen 103 Reader
Smith 103 Reader
Ram 103 Reader
===================================================================================
=======================================================================
7. List the name, designation, and income for 10 years of the employees who are
working in departments 10 and 30.

ANS:
SQL> SELECT STAFF_NAME, DESIGN_CODE, STAFF_SAL, STAFF_SAL*120 AS
DECADE_SAL,DEPT_CODE FROM STAFF_MASTER WHERE DEPT_CODE IN (10,30);

STAFF_NAME DESIGN_CODE STAFF_SAL


DECADE_SAL DEPT_CODE
-------------------------------------------------- ----------- ----------
---------- ----------
Arvind 102 17000
2040000 30
Mohan 102 24000
2880000 10
John 106 32000
3840000 10
Allen 103 42000
5040000 30
Ram 103 32000
3840000 30
===================================================================================
=======================================================================
8. List the name and experience (in years) of employees who are working as
LECTURER.

ANS:
SQL> SELECT STAFF_NAME, ROUND(MONTHS_BETWEEN(SYSDATE,HIREDATE) /12,2) AS EXPERIENCE
FROM STAFF_MASTER,DESIGNATION_MASTER WHERE
STAFF_MASTER.DESIGN_CODE=DESIGNATION_MASTER.DESIGN_CODE AND DESIGN_NAME =
'Lecturer';

no rows selected
===================================================================================
=======================================================================
9. Display name concatenated with dept code separated by comma and space. Name
the column as �Student Info�.

ANS:

SQL> SELECT STUDENT_NAME ||', is in Department ' || DEPT_CODE AS STUDENT_INFO FROM


STUDENT_MASTER;

STUDENT_INFO
-----------------------------------------------------------------------------------
-----------------
Amit, is in Department 10
Ravi, is in Department 10
Ajay, is in Department 20
Raj, is in Department 30
Arvind, is in Department 40
Rahul, is in Department 50
Mehul, is in Department 20
Dev, is in Department 10
Vijay, is in Department 30
Rajat, is in Department 40
Sunder, is in Department 50
Rajesh, is in Department 30
Anil, is in Department 20
Sunil, is in Department 10
Kapil, is in Department 40
Ashok, is in Department 40
Ramesh, is in Department 30
Amit Raj, is in Department 50
Ravi Raj, is in Department 50
Amrit, is in Department 10
Sumit, is in Department 20

21 rows selected.
===================================================================================
=======================================================================
10. List the Name and Salary of the staff who are earning between 12000 and 25000.
Sort
them based on their salaries and name.

ANS:

SQL> SELECT STAFF_NAME, STAFF_SAL FROM STAFF_MASTER WHERE STAFF_SAL<=25000 AND


STAFF_SAL>=12000 ORDER BY STAFF_NAME;

STAFF_NAME STAFF_SAL
-------------------------------------------------- ----------
Anil 20000
Arvind 17000
Mohan 24000
Rahul 22000
Raviraj 18000
Shyam 20000
===================================================================================
=======================================================================
11. Display employees who do not have manager.

ANS:

SQL> SELECT STAFF_NAME FROM STAFF_MASTER WHERE MGR_CODE IS NULL;

no rows selected

===================================================================================
=======================================================================
15. Display the name and department code of students. If student does not belong to
any
department, display �No Department�. Label the column as �Department�. (Hint: Use
NVL function)

ANS:

STUDENT_NAME
NVL(TO_CHAR(DEPT_CODE),'NODEPARTMENT')
--------------------------------------------------
----------------------------------------
Amit 10
Ravi 10
Ajay 20
Raj 30
Arvind 40
Rahul 50
Mehul 20
Dev 10
Vijay 30
Rajat 40
Sunder 50
Rajesh 30
Anil 20
Sunil 10
Kapil 40
Ashok 40
Ramesh 30
Amit Raj 50
Ravi Raj 50
Amrit 10
Sumit 20

21 rows selected.

Das könnte Ihnen auch gefallen