You are on page 1of 39

SULEYMAN DEMIREL UNIVERSITY

ENGINEERING FACULTY

DATABASE MANAGEMENT SYSTEMS


LABORATORY WORKS

COMPILERS Chetinkaya K. , Dulayev I.

ALMATY
2009

1
LABORATORY WORK-1 : Introduction to sqlplus

1-Connect to the database as SCOTT, in command prompt.

2-Connect to the database as SCOTT, in graphic windows.

3-Connect to the database as SCOTT, in web interface from intranet.

4-Connect to the database as SCOTT, in web interface from internet.

5-List all tables from the current database.

6-List all records from EMP table.

7-List all student name from EMP table.

8-List all rows from EMP table in Ascending order.

9-List all rows from EMP table in Descending order.

10-Copy Oracle 10g and install on your notebook( OS :windows XP )

2
LABORATORY WORK-2 : Using arithmetics operators

1-Calculate the following expression using SQL statement.


2
6 2x6
2
7− 78 x 67
8 13
43

2-SQL statement to calculate the area of triangle. Table : TRIANGLE

a
3-SQL statement to calculate the area of circle. Table : CIRCLE

4-SQL statement to calculate the value of “C” Table : TRIANGLE

c
a

3
5-Write an SQL statement to list addition of salary and commission for each employee in the
table “EMP”

6-Write an SQL statement to calculate the remainder of X divided by 3 in the table MATH.

7-Write an SQL statement to calculate the remainder of Y divided by 4 in the table MATH.

8-Write an SQL statement to calculate the remainder of 21 divided by X in the table MATH.

9-Write an SQL statement to add a and b from table “NUMBERS”


1
10-Write an SQL statement to list x2 ,  x ,
x from the table “x”
11-Write an SQL statement to list the total grades of the student on Table
“GRADES”

12-Write an SQL statement to list the average of students in table “GRADES”.

4
LABORATORY WORK-3 : Using DATE function

Write an SQL statement to answer the following questions.

1- How many months have staff worked until now?

2- 20 days later, what will be the date ?

3- 5 months later, what will be the date ?

4- Which day have they started to work ?(EMP) ( day as number e.g. 21)

5- Which day have they started to work ? (EMP) ( full day name e.g. SATURDAY )

6-What time is it ? 12:01:30PM

Write an SQL statements which returns,

7- Employee name and only months of hiredate.

8- Current day as a number. 25

9- Current day as a full name. MONDAY

10- Current month as a full name. JANUARY

11- Current date as format 2010-MONDAY-JANUARY

12- Current hour and minute. 12:20

13- Current meridian indicator . PM

14- Last day of February,1996 . 29-FEB-96

15- Current year . TWENTY TEN

16- The next SUNDAY ? 31-JAN-10

17- The next Friday . 29-JAN-10

18- Five days later . 30-JAN-10

19-Change date format of session as 2010-01-26

20-Change date format of session as 2010-MONDAY

5
21- Create the stuff table below.

ID NAME DOB DEPARTMENT SALARY


1 NURLAN 13 JUN 1978 CANTEEN 500
2 ASEL 4 JAN 1979 CAFE 800
3 BAKIT 12 FEB 1977 LIBRARY 1000
4 BAURJAN 4 MAY 1976 LIBRARY 750
5 SERIK 6 APR 1979 CAFE 600
6 AYNUR 7 OCT 1980 FOLDERCENTER 500
7 ELENA 11 DEC 1974 FOLDERCENTER 600

22-Add 10 or more records.

6
LABORATORY WORK-4 : Using WHERE clause

emp

1- List employee name that start with “B”

2- List employee name ends with “S”

3- List employee work in department no=20 or department no=30

4- List employee whose salary is greater than or equal to 600

5- List employee whose salary is smaller than 1500 and department no=30

6- List all salesman and manager

7- List employee whose empno is between 7500 and 7700

8- List the employee which have not got any commision.

9- List only department numbers.

10- List employee which are not working as CLERK.

11- List employee whose hiredate is between 01-JAN-1979 AND 01-MAR-1982

12- Second letter of ENAME is “A”

13- List HIREDATE is less than 1977

14- List Employee not working on Depertmant no : 10

7
15- List the last letter of ENAME is “N”

16- List ENAME whose salary is not equal to 1000 and 2000

8
LABORATORY WORK-5 : Using Built-in Functions

1- 1-Write an SQL statement to returns name and surname separately.

STUDENT CLASS NAMESURNAME


MULDASHEV.NURLAN 3A -----------------------------------------------
NURUMBETOV.SYRYM 4A NURLAN MULDASHEV
SYRYM NURUMBETOV

2-Write an SQL statement to update the table as shown figure below.

STUDENT CLASS
Nurlan MULDASHEV 3A
Syrym NURUMBETOV 4A

3-Write an SQL statement to returns the index of the books as shown in the figure below.

BOOKNAME TOTALPAGE BOOKNAME TOTALPAGE


MAGIC BOOK 520 -----------------------------------------------------------
X-MAN 400 MAGIC BOOK……………………… 520
X-MAN……………………………… 400

4-Write two SQL statement to returns the position of the third ‘or’ in the following string.
(search from left and right)

Corporate Floor

5-Write an SQL statement to returns the position of the first “FOUR” from the string: Take
the first four characters

6-Write an SQL statement to returns the position of the second “ten” from the string : seven
ten eight nine ten one

7-Write an SQL statement to returns the position of the second “a” from the string : Take the
first four characters

9
8-Write an SQL statement to returns the value between two commas from the STRING table
below.

TABLE : string
text
---------------
Abc,def,gfh
Sd,fghjk,gh
Asdfgh,f,fghjjk

9-Write an SQL statement to returns JOBs in ascending order by its character number.
10-Write an SQL statement to returns names without any spaces from both sides.

TABLE : students
Name
----------
Nurlan
Asem
Aynura

Write an SQL statement to update ENAME column from table “EMP“ as shown below.

11-Change ‘A’ with ‘X’

12-Change ‘N’ with ‘S’ and ‘M’ with ‘9’

13-Write an SQL statement to returns the ASCII code of the third character from “ename” in
EMP.

10
LABORATORY WORK-6 : Using Built-in Functions

1-Write an SQL statement which returns

a) sin(300 ) b) log10 150 c) log 2 1024 d )a cos(1) e)a tan(1)

f )a sin(0.5) g ) cos(450 )

sin 30
h) + tan 2 45 + log 3 600 + ln 6 + 812
cos 70

2-Write an SQL statement which returns

3.56 >>? Function >>> 3


5.286 >> ? Function >>> 5.29
7.12 >> Function >>> 7
7.12 >>? Function >>> 8
5.45612 >>? Function >>> 5.45
567 >>? Function >>> 570

Use AIRPLANES table for 3rd ,4th and 5th tasks


3-Write an SQL statement which return ‘ONE’ if line_number=1,’TWO’ if line_number=2 or
‘MORE THAN TWO’ if line_number is greater than 2.

11
4-Write an SQL statement which returns the full name of customer_id as shown in the figure.

AAL American Airlines


ILC Intl. Leasing Corp.
NWONorthwest Orient
SAL Southwest Airlines
SWA Sweptwing Airlines
USAF U.S. Air Force

5-Write an SQL statement which returns


‘ONE’, if line_number between 1 and 10,
‘BIG’, if line_number is between 11 and 50,
‘BIGGER’, if line_number is greater than 50.

6-Write an SQL statement which returns values using coalesce in order col1,col2,col3
Table: test
Col1 col2 col3
-----------------------------------
1 3
1 5
6 1
1
2 1

12
LABORATORY WORK-7 : Aggregate Functions

1-Who has the lowest salary in EMP?

2-Who has the highest salary in EMP ?

3-How many employees has commission in EMP?

4-How many employees have no commission in EMP?

5-Calculate the total salary in EMP.

6-Make a list with a column which ranked by line_numbers in AIRPLANES.(use Rank)

7-Make a list with a column which ranked by line_numbers in AIRPLANES.(use dense_rank)

8-Use rank function with partition to list table in AIRPLANES.

9- List the first 3 flights on customer_id='DAL' , ordered by line_number.

10-List the last flight if list is ordered by order_date.

11-Make a list which ranked by city in ENT

12-Make a list which ranked by score in each city.

13-How many different departments are there in EMP table.

14-How many different jobs are there in EMP table.

15-Find the name of 8th successful student from the ENT table.

16-Find the Name of Third successful student in Almaty from ENT table;

13
LABORATORY WORK-8 : Using Joins

1-Display the followings below.


• Empno,Ename,Dname
• Ename,Loc

2-List all salesman with their location.

3-List the name and location of employees from SALES department.

4-Who works in DALLAS ?

5-List CLERKS who work on Research Department.

6-List the number of SALESMAN that work in DALLAS.

7-Where does JAMES live ?

8-How many personals are there in Accounting Department ?

9-List the name and grade of all employees.

COURSE
COURSE_ID COURSE_NAME INSTRUCTOR
CS100 INFORMATICS KOJAHMETOV
CS101 ALGORITHMS ORISBAYEV
CS213 JAVA BOGDANCHIKOV

STUDENT
STUDENT_ID STUDENT_NAME COURSE_ID GRADE
1 ASEM CS100 A
2 NURLAN CS100 A-
3 NARGIZ CS101 C
4 ALIP CS213 B
5 TOLEGEN CS100 C
6 YERNUR CS101 B

10- List student_name and Course_name.

11-List student_name,course_name and grade.

12-List students who take courses from KOJAHMET.

13-List students who take ALGORITHMS.

14
14-HOW CAN YOU DISPLAY THIS RESULT SET USING BIN_A TABLE ONLY.

TABLE
BIN_A

A
---
0
1
RESULT

15
LABORATORY WORK-9 : Unions and Joins

1-Write an SQL statement which returns JOB and TOTAL SALARY of jobs.

2-Write an SQL statement which returns JOB and AVERAGE SALARY of jobs.

3-Write an SQL statement which returns JOB and TOTAL SALARY of jobs is greater than
5000.

4-Write an SQL statement which returns JOB and AVERAGE SALARY of jobs is greater
than 2000.

5-Write an SQL statement which returns the average salary of all DEPTNO.

6-Display the highest,lowest,sum, and average salary of all employees.Label the columns
Maximum,Minimum,Sum,and Average, respectively. Round your results to the decimal
position.

7-Display the maximum, minimum,sum, and average salary for each job type.

8-Write a query to display the number of people with the same job.

16
9-Determine the number of managers without listing them. Label the column “Number of
Managers”

10-Write an SQL statement that will display the difference between the highest and lowest
salaries.
Label the column “Diffrenece”

11-Display the manager number and salary of the lowest paid employee for the manager.
Exclude anyone where the manager id is knot known.
Exclude any groups where the minimum salary is less than $1000. Sort the output in
Descending order of salary

12-Write a query to display the department name, location name, number of employees, and
the Average of salary for all employees in that department.
Label the columns dname,loc,Number of people, and Salary , respectively.

17
13-List DEPTNO exists in DEPT but not in EMP.

14-List All Deptno from emp and dept.

18
LABORATORY WORK-10 : Group By and Group functions

1 – Students with the GPA average for all semesters

2 – Classes with the GPA average for all semesters

2 – Classes with the GPA average for all semesters

19
3 – Classes with the GPA average for each semester

4 – Average of 8th class’s GPA for all semesters

5 – Average of 8th class’s GPA for each semester

20
6 – Students with average GPA=3 ?

7 – Classes with GPA Average for Amount of student >=3

21
8 – Semesters with GPA Average for all classes.

9 – Grand average with rollup

22
10 – Categorize people into child,teenager and adult depending on their ages

11 – Compare a parameter and value with DECODE


If p<5 then ‘smaller’ >>>> decode(sign(p-5),-1,’ smaller’) ;
If p=5 then ‘equal’ >>>> decode(p,5,’equal’) ;
If p>5 then ‘greater’ >>>> decode(sign(p-5),1,’ greater’) ;

SELECT
ENAME,
DECODE( SIGN(SAL-2000 ),1,'GREATER THAN 2000', 'SMALLER THAN 2000')
SALARY
FROM EMP;

23
12 – List the number of old and new cars.

1999 2000

OLD CAR NEW CAR

13 – List the average of prices for old and new cars.

24
14-Retreive the full customer name of top-10 rows. 'AAL', 'American Airlines',
ILC - Intl. Leasing Corp. NOW - Northwest Orient,
SAL - Southwest Airlines SWA - Sweptwing Airlines
USAF - United States Air Force Not Known - AIRLINE

25
15-Retreive PRG_ID and Customer_Id as shown on the figure below.

16-Retreive PRG_ID and amount of each customers as shown on the figure below.

17-Retreive months and total empoyees number who started to work at that month.

26
18-List employees ,in department 10, has salary which is less than any salary of employees in
department 30

19-List all employees who are not working in department 30 and who earn more than
all employees working in department 30:

20-List all departments that have no employees:

21-Retrieve the minimum and maximum salary of clerks for each department having
more than 1 clerk.

22-Retreive all employees with their managers.

27
QUIZ

EMP

ENT

SALGRADE

DEPT

TEST

28
1- Write the result of the following SQL statement.
SELECT COALESCE(COL2,DECODE(COL3,COL1,COL1*2,COL3*3),COL1) COL
FROM TEST ;

2- Write a query which returns an employee who has the maximum salary in EMP?
(You will use EMP table)

3- Write an SQL statement which finds the third successful student(s) in ENT table. (You will
use ENT table)

4- Write the result of the following SQL statement.


SELECT RANK() OVER(PARTITION BY SCORE ORDER BY NAME) AS NO,
NAME,SCORE FROM ENT;

5-How many different departments are there in EMP table. (You will use EMP table)

6- Write a query which finds the GRADE of TURNER. (You will use EMP table)

7- Write an SQL statement which returns the number of employees in “Accounting”


Department. (You will use EMP table)

8-Write an SQL statement which returns all “Deptno” in emp and dept tables.

29
9- Write an SQL statement which returns the average score of ALMATY in ENT table.

10- Write an SQL statement which returns JOB and average salaries of each job,
but include only if the average salary is less than 1500. (You will use EMP table)

30
LABORATORY WORK-11 : Subqueries

1-Write a query to display the name and hire date of any employee in the same department as
SMITH. Exclude SMITH.

2-Write a query to display the employee number and name of all employees who earn more
than the average salary. Sort the result in ascending order of salary.

3-Write a query that displays the employee numbers and the names of all employees who
work in a department with any employee whose name contains a “D”

4-Display the name, and salary of every employee who reports to KING.

31
5-Display the department name, name, and job for every employee in “ACCOUNTING”
department

7-Select all employees whose salary is less than the average of all the employees' salaries in
the same department.

8-Select maximum salary,salary and difference salary between maximum and salary of all
employees.

32
9-Select maximum salary of all departments with employees.

33
LABORATORY WORK-12 : Producing Readable Output with iSQLPLus

1-Determine whether the following two statements are true or false


DEFINE & X =100
TRUE FALSE
2-DEFINE command is an SQL command
TRUE FALSE
3-Write a script to display the ename, job , and hiredates for all employees who started within
a given range of dates. Concatenate the name and job together, separated by a space and
comma, and label the column Employees. Use the DEFINE command to provide the two
ranges. Use the format MM/DD/YYYY
DEFINE lowdate=01/JAN/1980
DEFINE highdate=01/JAN/1981

34
LABORATORY WORK-13 : Sql commands

1-Based on the departments table below, rename the departments table to depts.

CREATE TABLE departments


( department_id Number(10), department_name varchar2(50) );

2-Based on the employees table below, add a column called salary that is a number(6)
datatype.

CREATE TABLE employees ( employee_number Number(10), employee_name


Varchar2(50), department_id
number(10) );

3-Based on the customers table below, add two columns - one column called contact_name
that is a varchar2(50) datatype and one column called last_contacted that is a date datatype.

CREATE TABLE customers ( customer_id Number(10), customer_name Varchar2(50),


address varchar2(50), city varchar2(50), state varchar2(25),
zip_code varchar2(10) );

4-Based on the employees table below, change the employee_name column to a varchar2(75)
datatype.

CREATE TABLE employees ( employee_number Number(10), employee_name


Varchar2(50), department_id number(10) );

5-Based on the customers table below, change the customer_name column to NOT allow null
values and change the state column to a varchar2(2) datatype.

CREATE TABLE customers ( customer_id Number(10), customer_name varchar2(50),


address varchar2(50), city varchar2(50), state varchar2(25),
zip_code varchar2(10) );

6-Based on the employees table below, drop the salary column.

CREATE TABLE employees ( employee_number Number(10), employee_name


Varchar2(50), department_id number(10), salary number(6));

35
7-Based on the departments table below, rename the department_name column to dept_name.
CREATE TABLE departments ( department_id Number(10), department_name
Varchar2(50));

36
LABORATORY WORK-14 : Introduction to PL/SQL

1-Insert the numbers 1 to 10, excluding 6 and 8. >>> Field : TABLE1.RESULT

2-Insert the numbers 10 to 1, >>> Field : TABLE1.RESULT

3-Create a PL/SQL block that computes the commission amount for a given employee based
on
the employee's salary. Table : emp , user : scott
-If the employee's salary is less than 1000, set the commission amount for employee to 10%
of salary
-If the employee's salary is between 1000 and 1500, set the commission amount for employee
to 15%
of salary.
-If the employee's salary exceeds 1500, set the commission amount for employee to 20% of
salary
-If the employee's salary is null, set the commission amount for employee to 0.

4-Find the minimum salary from the emp table and print to screen.

5-Define an external variable , name is x, change it's value and see the current value of x.

6-Your table has the values of names only. Write a PL/SQL block to append one asterisk for
each
character on the names to the column “star”.

TABLE NAME : SCHOOL


NAME STAR
-----------------------------
NURLAN ****** >>>> 6 ASTERISK
ALI *** >>>> 3 ASTERISK
ASEL **** >>>> 4 ASTERISK
BAURZHAN ******** >>>> 8 ASTERISK

7-Write a PL/SQL block to calculate the area of the following triangle.


Use “ACCEPT” to enter Height and base values.

8-Write a PL/SQL block to prints all odd numbers between 0 and 100 .

9-Write a PL/SQL block to prints all numbers between X and Y . Use “Accept” statement to
enter X and Y from keyboard.

37
LABORATORY WORK-15 : Constraints

A-Create the following tables with constraints.


Parent

Table : PT Child Table : CH

Integrity Rules

1.It must be allowed to insert ”Female” and “Male” into “Gender “only.

2.”PT” is parent table and “CH” is child table.

3-Only uppercase data can be inserted to name and surname.

4-Create a sequence for Id in Table “PT”.

5-Semester can have 1 and 2 only.

6-”Grade” can have A , A- , B , B+ ,B- , C ,C+,C-, D, D+ , F

7-Gender can not be null.

8-On the table CH : Id, Course and semester must be unique together. Each student must have
one row
for a course in a semester.

9-Insert the following rows into tables above. Find the 4 errors while inserting following
rows.

38
10-Delete the first row from “PT”. Why can not you delete it?

B-Create a sequence name is MySequence with the following parameters.


Starting values : 25
Minimum Value : 10
Maximum Value : 100
Step : 3
1- Disable MySequence and see that it does not work.
2-Enable MySequence and see that it can work.
3-Can you know the current value of MySequence?

C-Create the following table and insert rows in below.

Rule
3.1-The default value of Taken_date is sysdate: It means, sysdate value is inserted into
Taken_date if
any date is not inserted.

3.2- Students must pay 10 Tenge per each day. Write a query which will display students with
reading
day and paid money.

39

You might also like