0% found this document useful (0 votes)
1 views

DBMS Assignments

DBMS lab assignments

Uploaded by

Harshitha Poluju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

DBMS Assignments

DBMS lab assignments

Uploaded by

Harshitha Poluju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

<br>

ASSTGNMEN

CONVERSION FUNCTIONS IN SQL

1) Using the NVL function replace the some value in commission


column in emp table.
2) Using the NVL2function replace the some value in commission
column in emp table.
3) Using the NULLIF function replace the some value in commission
column in emp table.
4) Return the lD number of user session

TO CHARfunction converts a number or data to a string

TO_char functions with the dates.

1.Display the DATE in this format '2014/08/18

2.Display the DATE in this format 'august 18, 2014'

3.Display the DATE in this format "JUL 09TH, 2003'

4.Display the DATE in this format "JUL 9TH, 2003'

5.Display the DATE in this format "JUL 9TH, 2003"


6.Excute the below queires

1. SELECT ename, hiredate, TO_CHAR( (hiredate), 'fmDay') "Day"


FROM emp

ORDER BY "Day";

2.SELECT ename, hiredate, TO_CHAR( (hiredate), 'fmD') "Day"

FROM emp

ORDER BY "Day";
<br>

NUMBERS
TO_char functions with the

Convert thebelow numbers into string

1210.73 would return' 1210.7"

-1210.73 would return '-1210.7

1210.73 would return'1,210.73

1210.73 would return '$I,210.73'

21 would return '000021'

7) Display the sal, comm From emp for all the recordes below format

Martin sal is 1,250.00 and commission is 1,400.00

8) Display the ename,sal, comm,totsal(sal+comm) From emp


for all the recordes and convert the null
values with user specfied value & dísplay the o/p in below format

Eg: james sal is 0,950.00 and comm is 0.000.00 and totsal 0.950.00

9)Display the below o/p for all the recordes

Eg:KING you joined on the weekday of 3

10) Display the below o/p for all the recordes using
runtime weekday and use the aliae
11) Display the below o/p for all the recordes

Eg: KING you are Recruited on 17-nov-81 Tuesday

12)Display the employees information who is recruited on Friday.

13) ) Display the below o/p

Eg: we are on the day 18 of this month

14) Display the below o/p


Eg: The current Day running in this year:
<br>

15) Display the below o/p

Eg: Display the current week of the year

16) Display the below o/p

Eg: Display the current Quarter of the year


year 1981 using to_char function
17)Display the employees who are joined in the

18) Display the employees who


are joined in the first quarter using to_char function.

TO NUMBER FUNCTIONS

converts a string to a number


The Oracle/PLSQL TO_NUMBER function
NUMBER USING TO NUMBER
CONVERT THE GIVEN STRING INTO

"1210.73' would return the number 1210.73

"546' would return the number 546

"23" would return the number 23

1) Excute the below query


Select
'1,234.0 num1,
'2,345.0 num2,
'1,234.00+2,345.00 result
From dual;
Observe the error write the
reason

displaythe result using alias


2) Add the below two string using to_number and
'1,234.00
'2,345.00

converts a
string to a DATE
The Oracle/PLSQL TO DATE function
<br>

ASSTGNMENT5
JOINS
PArT-4
1. Suppose there is annual salary information provided by emp table. How to fetch monthly
salary of each and every employee?
2. Select allrecord from emp table where deptno=30 and sal>1500
3. Select all record from emp table where deptno =10 or 40.
.
4. In emp table add comm+sal as total sal Select any salary <3000 from emp table.
5. Select all record from emp where ename in 'BLAKE,"SCOTT,' KINGand'FORD'

6. Count MGR and their salary in emp table.


7. Count the totalsa deptno wise where more than 2 employees exist.
8. Display ename,sal.job,dname,loc
9. Display ename,sal.job
10. Display enamejob,select the deptno from emp,loc
11. Display ename,dname,loc from emp,dept and deptno should be from emp and it has
to display only 14 records.
12. suppose we want to fetch records from two tables emp,dept where deptno ofemployee
is equal to dept no of dept
13. we want to fetch the recordes like ename,empno, sal, annsal,deptno, loc using joins(equi
join)
14. fetch the data like deptno,dname, loc and sum of the salary of their respective
department using join and gruoup by respective columns.

15. display the year in four digit format deptno from dept table,dname&and
sum of salary
and group by above select columns and order by year.
more than 2
16. from the above query op display the information ofemployees who joined
employees in that particular.

17. fetch the recordes empno,ename,deptno,dname using table alias.

18. fetch the recordes ename.job,deptno,dname,loc using table alias &fetch those recordes
job should match with MANAGER AND ANALYST

manager and use the table


19. Display the employeename along with employee
alias&column alias(use selfjoin) the the column should be employee column should be
ename and their manager column name should be mangers

20.fetch the recordes of employee and their managers using table alias the column should
manager column nume should be
be employee column should be ename and their
ename
<br>

21) SELECT
Employees.Ename "Employees",
Managers.Ename "Managers"
FROM Emp Employees, Emp Managers
WHERE Employees.Mgr = Managers.Empno

22) Display the below olp using selfjoin

Employees And Managers


FORD'S Manager is JONES
SCOTTs Manager is JONES

23) fetch the ename sal deptno dname loc using left outer join

24) fetch the ename sal deptno dname loc using right outer join

25) SELECT Ename, Sal, Dept.Deptno Deptno, Dname, Loc


FROM Emp, Dept
WHERE Emp.Deptno = Dept.Deptno(+)
name whose belong to deptno 10& accounting
26) Display the employee
name whose belong to deptno 10& accounting & location
27) Display the employee
newyork
28) SELECT
NVL(E.Ename, 'NOT RECRUITED OR REQUESTED)) Ename,
D.Deptno,
D.Dnamne,
DLoc
FROM Emp E, Dept D
WHERE EDeptno(+)= D.Deptno
AND E.Deptno(+) =10
ORDER BÝ E.Deptno

29) SELECT
E.name Employee,
E.Deptno Department,
M.Ename Manager,
M.Deptno "Manager's Dept" FROM
Emp E, Dept D, Emp M WHERE
E.MGR = M.Empno AND
E.Deptno=D.Deptno;

30) SELECT
E.Ename Employee, Dname Department,
Loc Place,
M.Ename Manager,
Dname "Manager's Dept" E.MGR
FROM Emp E, Dept D, Emp M WHERE
= M.Empno AND
E.Deptno = D.Deptno;
<br>

31) SELECT Ename, Dept.Deptno, Dname, Loc


2 FROM Emp CROSS JOIN Dept

32) SELECT Ename, Dept.Deptno, Dname, Loc


FROM Emp CROSS JOIN Dept
WHERE Emp.Deptno = Dept.Deptno;

33) SELECT Ename, Deptno, Dname, Loc


FROM Emp NATURAL JOIN Dept;

34) SELECT Ename, Deptno, Dname, Loc


FROM Emp JOIN Dept
USING(Deptno);

35) SELECT Ename, Deptno, Dname, Loc


FROM Emp INNER JOIN Dept
USING(Deptno)

36) SELECT
E.Êname Employee,
M.Ename Manager
FROM
Emp E INNER JOIN Emp M
ONE.MGR=M.Empno)
<br>

ASSIGNMENT
-5
PART-)
1) Writea query to supply the alias to table and retricve the data from emp, dent.
2) Write a query use * to relerences display tlhe allthe enp details from dept 10 & along all the
columns rom emp using table alias.

3) Exccute the below query.

SQL> SELECT DEPTNO,ENAME,DNAME


FROM EMP,DEPT:
SELECT DEPTNO,ENAME,DNAME
ERROR at line 1: ORA-00918: column ambiguously defincd

4)Exccute the below query.

SQL> SELECT D.DEPTNO,D.DNAME,E.ENAME,E.DEPTNO,E.SAL


FROM EMP E, DEPT D;

5)Join tables and order by columns from differcnt tables

SET OPERATOR

1) Display ename, dname using SET opcrator.

2) Display ename, deptno using set opcrator ordered by ename.

3) Display ename, deptno using sct opcrator ordered by cname using sub-qucry.
4) Display ename from cmp and dname from dept using interscct operator from dept 10,30.

S) Display ename from cmp and dname from dept using minus operator from dept 10,30.

6) Display the job from emp,dept using union allopcrator from dept 10,30.

7) Display the job which is common in all 3 dept using union or intcrscct opcrator.

8)Display the job which is common in all3 dept using union opcrator and runtime values.

9) Display the total salary of cach group along with total salary & group by deptno.
<br>

1)Write a query using equijoin ename, empno, job, dname, loc from
emp,dept.
2) Write a query using equijoin ename, empno, job, dname, loc from emp,dept using USING
clause

3) Write a query using equijoinename, empno, job,dname, loc from emp,dept using ON clause.

NON-EQUIJOIN

1)Select ename, enmpno.job,dname, loc from emp, dept using Non-Equijoin.


2) Select ename, deptno, job from ename like %s% using Non-Equijoin.

3) Select ename, empno, loc from emp who are working in dept 10, 20.

SELF JOIN
1)Select empno,ename,job,deptno from emp using selfjoin.

2) Select empno,ename,mgr,comm from emp using selfjoin.

3) Display the employees and their managers information


<br>

4SsTGNMENT-6
ASSIGNMENT IV GROUP BY CLAUSE),HAVING CLAUSE),AGGREGATE FUNTIOONS

Group by clause:

Aggregate functions are normally used in conjunction with a GROUP BY clause. The GROUP
BY clause enables you to use aggregate functions to answer more
complex managerial questions
such as:

Group by function establishes data groups based on columns and aggregates the
information
within a group only. The grouping criterion is defined by the columns specified in GROUP BY
clause. Following this hierarchy, data is first organized in the groups and then WHERE clause
restricts the rows in each group.

Guidclines of using GROUP BY clause

() Allthe dependent columns or columns used in GROUP BY function must form the basis of
grouping, hence must be included in GROUP BY clause also.

SELECT DEPARTMENT ID, SUM(SALARY)

FROM employees;

DEPARTMENT ID,

*ERROR at line 2: ORA-00937: not a single-group group function

(2) GROUP BY clause does not support the use of column alias, but the actual names.

(3) GROUP BY clause can only be used with aggregate functions like SUM, AVG, COUNT,
MAX, and MIN.If it is used with single row functions,

Oracle throws and exception as "ORA-00979: not a GROUP BY expression".

(4) Aggregate functions cannot be used in a GROUP BY clause.

Oracle will return the "ORA-00934: group function not allowed" here error message.

Below query lists the count of cmployees working in each department.

SELECT DEPARTMENT ID, COUNT (*)

FROM employees

GROUP BY DEPARTMENT ID;


<br>

department. Note
Similarly, below query to find sum of salarics for respective job ids cach
in
appcar in GROUP BY
the group is establishedbascd on Department and Job id. So they
clause.

SELECT DEPARTMENT ID, JOB ID, SUM (SAL)

FROM employees

GROUP BY DEPARTMENT ID, JOB_ID;

on the
The below query also produces the same result. Please note that grouping is based
department id and job id columns but not used for display purpose.

SELECT SUM (SALARY)

FROM employees

GROUP BY DEPARTMENT_ID, JOB_ID;

1)Select the job from emp using group by clause


2) Display the totsal from emp table for all the departments. Using aggregate function &
group by deptnum wise

3) Display the departments tot salary using aggregate function&group by exclude dept 10.

4) Display the below o/p using to _char & group by

Year
1981
1982
1983

5. Display the below o/p using TO CHAR & GROUP BY & ORDERBY

Year

April
December
February

6. Excute the following query

Select deptno from emp


Group by deplno
<br>

7. Execute the following qucry

Select deptno from emp note: assending descending exit with orderby clase

Group by deptno DESC

Observe the error

S. Execute the following query

Select deptno from emp

Group by deptno

Order by desc

9. Execute the following query

Select TO CHAR(HIREDATE, MONTH') MONTH

FROM EMP

GROUP BY TO CHAR(HIREDATE, MONTH');

10. Execute the following query

SELECT

TO CHAR(HIREDATE, MM') "S.NO",

TO CHAR(HIREDATE,'MONTH') MONTH,

FROM EMP

GROUP BY

TO CHAR(HIREDATE,'MM')

TO CHAR(HIREDATE,'MONTH')

11. SELECT DEPTNO,JOB

FROM EMP

GROUPBY DEPTNO

OBSERVE THE ERROR WRITE THE REASON (POINT NO:3)


<br>

12. SELECT DISTINCT DEPTNO,AVG(SAL)

FROM EMP:

OBSERVE THE ERROR WRITE THE REASON (POINT NO:1l)

13. Display the deptno,avg sal of


each department using groupby clause,orderby clause.

14. Execute the following query

Select

To_char(hiredate,'yyyy') year,

Deptnojob,sum(sal)

From emp

Group by

Deptno,

Job,

To char(hiredate,'yyyy')

15.Display the year, halfyear, quarter, month, week, deptnum, job, sum(sal)
using group by
clause.

Aggregate Functions
Aggregate functions perform a variety of actions such as counting all the rows in a table,
averaging a column's data, and summing numeric data.
1. Count the number of employees in the emp table.

2. Write the query returns the average of the salaries of employees in the organization using
alias.

3. Write the query returns the sum of thesalaries of employees in theorganization.using alias
4. SELECT query returns the oldest and latest hired dates
of employees in the organization.
<br>

SQL> SELECT
AVG (SALARY)
FROM EMPLOYEE:

SQL> SELECT
JOB \D.
SUM (SALARY)
FROM EMPLOYEE
GROUPBY JOB ID

SELECT
JOB ID,
MAX(SALARY)
FROM EMPLOYEE
GROUPBY JOB ID

SELECT
DEPARTMENT ID,
MIN(HIRE DATE)
FROM EMPLOYEE
GROUP BY DEPARTMENT_ID

SQL> SELECT
DEPARTMENT ID,
COUNT ()
FROM EMPLOYEE
GROUP BY DEPARTMENT ID

SQL> SELECT
COUNT(EMPLOYEE ID)
FROM EMPLOYEE
WHERE SAL > 5000
<br>

Nesting of Group functions


SQL>SELECT
MAX(COUNT(EMPNO)
FROM EMP
GROUP BY DEPARTMENT ID

Group by function must be specified in the query.

Multiple single-row functions can be nested together. but group functions can be nested up to
two levels only. The nested function call COUNT(SUM(AVG(
X)))

Returns the error, "ORA-00935: group function is nested too deeply." Hovwever, using
nested
single-row functions within group functions works fine

SELECT
SUM(AVG(LENGTH(LAST NAME))
FROM EMPLOYEES
GROUP BY DEPARTMENT ID

SQL> SELECT ename, SUM(SALARY) FROM emp

GROUP BY ename;

Handling NULL values in GROUP BY functions

GROUP BY functions work with actual values of the column only i.e. ignoring
the NULL values
(COUNT(*) is exception here). But this feature creates major difference in terms calculations,
of
end analytic results.

But to avoid their ignoring, handling of NULL values in the columns is necessary
because
ignoring of NULL values might produce incorrect results

Select

Avg(comm)

From emp

Select

Avg(nvl(comm.,0)

From emp
<br>

The HAVING clause


a
The HAVING clause is used for aggregate functions in the same way that WHERE clause is
used for column names and expressions. Essentially,the HAVING and WHERE clauses do the
same thing, that is filter rows from inclusion in a result table bascd on a condition. While it may
appear that a HAVING clause filters out groups, it does not .Rather ,a HAVING clause filters
rows.

so is the group.To summarize, the important


When all rows for a group are eliminated
are:
differences between the WHERE and HAVING clauses

A WHERE clause is used to filter rows BEFORE the GROUPING action (i.e., before the
calculation of the aggregate functions).

A HAVING clause filters rows AFTER the GROUPING action (i.e., after the calculation of the
aggregate functions).

A HAVING clause filters rows AFTER the GROUPING action (i.e., after the calculation of the
aggregate functions).

SELECT JOB ID, SUM (SALARY)

FROM employees

GROUP BY JOB ID

HAVING SUM (SALARY) > 10000;

The HAVING clause is a conditional option that is directly related to the GROUP BY clause
option because a HAVING clause eliminates rows from a result table based on the result of a
GROUPBY clause.

SELECT department_id, AVG(Salary)

FROM employees

HAVING AVG(Salary) > 33000;

ERROR at line 1: ORA-00937: not a single-group group function


<br>

SSI GNMENT
SUBQUERIES

1) list the employees working in research department.

2) List employees who are located in New York and Chicago.

3)Display the department name in which ANALYST ARE WORKING

4)Display employees who are reporting to Jones.

5)Display the employees who are reporting to jones manager.

6)Display the all the managers in sales and accounting department

7)Display all employees who are having at least 2 reporting

8)Display all employees who do not have any reportees.

9)list the department names which are having more than 5 employees

10)list department name having at least 3 salesmen.

11)list employees from research and accounting having at-least 2 reporting

12)Display all the employee names in research and sales department who are having at least
1

person reporting to them

13) Display second max salary.

14)Display 4th max salary

15)Display 5th max salary


<br>

16)get the details of all


the employees whose salaries are matching with higest
departments investment of

17) get the details of all


the employees whose salaries are matching
departments with least investment of

18)get the details of all


the employees whose salaries are matching
Designation with highest investment of

19)get the details of allthe employees whose salaries are


designation matching with lowest investment
of

20) get the details of allthe employees whose salaries are


designation matching with average investment
of

21)get the employees whose salaries are


match with salaries of salesman

22)get the details of all the employees


who are equally paid to designation
salesman give the other employees salesman excluding the

23)get the details of all the employees


whose salaries are matching to salaries
department of research

24)who are earning below the highest


investment of dept 20

You might also like