Database Systems Lab
Mid Exams
تم حل هذا الملف من قبل الطالب :ليث عمرو
هذه الحلول ه جهد ر
بشي قابل للخطأ ،فإن أحسنت فمن هللا ي
فمن ومن الشيطان.ّ
وإن أخطأت ي
ّ
هذا العمل مقدم ثوابه لروح والدي ( ق ـ ــاس ـ ــم مـ ـ ـفـ ـ ـي ـ ــد عـ ـ ـم ـ ــرو )
ُ
فمن وجد فيه فائدة فليدع له بالرحمة والمغفرة.
Database Systems Lab
Mid Exams
Q1) Given the following database instance.
-Student:
-Enrolled:
-Class:
-Faculty:
Database Systems Lab
Mid Exams
-Answer the following questions:
#1 Find the names of all junior SELECT sname
students (level='JR'), and list FROM student
in the order of age. WHERE level=’JR’
ORDER BY age;
#2 Find the number of students SELECT COUNT(DISTINCT s.sname)
who have enrolled in at least FROM student s, enrolled e1, enrolled e2
two classes. WHERE e1.snum = e2.snum
AND e1.cnum<>e2.cnum
AND s.snu = e1.snum;
#3 For each level, print the level SELECT level, avg(age)
and the average age of FROM student
students for that level. GROUP BY level;
#4 Find the names of students SELECT sname
who are not enrolled in any FROM student
class. WHERE snu
NOT IN ( SELECT snum FROM enrolled );
#5 For all levels except JR, print SELECT level, avg(age)
the level and the average age FROM student
of students for that level. WHERE level !=’JR’
GROUP BY level;
#6 Find the names of all SELECT DISTINCT S.sname
students who have enrolled FROM Student S, Enrolled E1, Enrolled E2
in both CSC343 and CSC443. WHERE S.snum = E1.snum
AND E1.snum = E2.snum
AND E1.cnum = 'CSC343'
AND E2.cnum = 'CSC443';
Database Systems Lab
Mid Exams
#7 Find the names of all SELECT sname
students in level 'SR' and FROM student
their age greater than 21. WHERE level=’SR’
AND age>21;
#8 What is the output of the
following SQL statement:
SELECT DISTINCT > s.name
as Student_Name
FROM Student S, Class C,
Enrolled E, Faculty F
WHERE S.snu = E.snum
AND E.cname = C.name
AND C.fid = F.fid
AND F.fname = ‘I. Teach’
AND S.level = 'JR';
#9 (Bonus) Find distinct name SELECT DISTINCT > s.name as Student_Name
of all juniors (level = ‘JR’) FROM Student S, Class C, Enrolled E, Faculty F
enrolled in a class taught by WHERE S.snu = E.snum
I.teach. AND E.cname = C.name
AND C.fid = F.fid
AND F.fname = ‘I. Teach’
AND S.level = 'JR';
Database Systems Lab
Mid Exams
Q2) Create table script for these tables.
SCHOOL
Column Name Data Type Allow Nulls
SchoolID int
SchoolName varchar(50)
Description varchar(1000) ✔
Address varchar(50) ✔
Phone varchar(50) ✔
PostCode varchar(50) ✔
PoseAddress varchar(50) ✔
CLASS
Column Name Data Type Allow Nulls
ClassID int
SchoolID int
ClassName varchar(50)
Description varchar(1000) ✔
CREATE TABEL school ( CREATE TABEL class (
SchoolID int NOT NULL, ClassID int NOT NULL,
SchoolName varchar(50) NOT NULL, SchoolID int NOT NULL,
Description varchar(1000), ClassName varchar(50) NOT NULL,
Address varchar(50), Description varchar(1000),
Phone varchar(50),
PostCode varchar(50), Constraint class_class_id_pk
PoseAddress varchar(50), PRIMARYKEY (SchoolID)
Constraint school_school_id_pk Constraint school_school_id_fk
PRIMARYKEY (SchoolID) FOREIGNKEY (SchoolID)
); );
Database Systems Lab
Mid Exams
Q3) Add the following constraint for the customer table.
Table Column Data type Length Precision Scale Primary Nullable Default Common
Key
CUSTOMER CUSTOMERID Number - - 0 1 - - -
CUSTOMERNUMBER Number - - 0 - - - -
LASTNAME Varchar2 50 - - - - - -
FIRSTNAME Varchar2 50 - - - - - -
AREACODE Number - - 0 - ✔ - -
ADDRESS Varchar2 50 - - - ✔ - -
PHONE Varchar2 50 - - - ✔ - -
A) CustomerNumber add constraint unique.
ALTER TABLE CUSTOMER
ADD CONSTRAINT CUST_CUST_NUM_UNI UNIQUE(CUSTOMERNUMBER)
B) CustomerNumber must be positive number.
ALTER TABLE CUSTOMER
ADD CONSTRAINT CUST_CUST_NUM_POS CHECK(CUSTOMERNUMBER > 0)
C) Insert the following information in customer table.
CUSTOMERID CUSTOMERNAME LASTNAME FIRSTNAME AREACODE ADDRESS PHONE
1 1000 Smith John 12 California 11111111
INSERT INTO CUSTOMER (1, 1000, ’SMITH’, ‘JOHN’, 12, ‘California’, 11111111);
D) Change area code number to 46 for customer with ID=2.
UPDATE CUSTOMER
SET AREACODE=46
WHERE CUSTOMERID=2;
E) Remove from table customer information for customer with ID=2.
DELETE FROM CUSTOMER
WHERE ID=2;
F) Remove all information from table customer.
DELETE FROM CUSTOMER;
Database Systems Lab
Mid Exams
Q4) Consider the following EMP table.
EMPNO ENAME JOB SUPERENO HIREDATE SAL COMM DEPTHO
7369 Smith Clerk 7902 17-DEC-02 1200 - 20
7499 Allen Salesman 7698 20-FEB-03 2000 500 30
7521 Ward Salesman 7698 22-FEB-03 1650 800 30
7665 Jones Manager 7839 02-APR-03 3375 - 20
7654 Marin Salesman 7698 28-SEP-03 1650 1400 30
7698 Blake Manager 7839 01-MAY-03 3250 - 30
7782 Clark Manager 7839 09-JUN-03 2850 - 10
7788 Scott Analyst 7566 27-JUN-02 3500 - 20
7839 King President - 17-NOV-03 6500 - 10
7844 Turner Salesman 7688 08-SEP-03 1900 0 30
7000 William Manager - 02-APR-01 5000 - 10
A) List the Empno, Ename, Sal, comm of all emps working for Mgr 7369.
SELECT Empno, ename, sal, comm
FROM Emp
WHERE supereno=7369;
B) Display all the details of the emps whose Comm. Is more than their Sal.
SELECT *
FROM Emp
WHERE comm>sal;
C) List the emps along with their comm and Daily Sal is more than 100$.
SELECT *
FROM Emp
WHERE (sal/30)>100;
Database Systems Lab
Mid Exams
D) List the emps who are either 'CLERK' or 'ANALYST' in the Desc order.
SELECT *
FROM Emp
WHERE JOB IN (‘CLERK’, ‘ANALYST’)
ORDER BY JOB DESC;
E) List the emps who joined on 01-may-03, 03-dec-03, 17-dec 02, 27-jun-02 in asc
order of seniority.
SELECT *
FROM Emp
WHERE HIREDATE IN (’01-MAY-03’, ‘03-DEC-03’, ‘17-DEC 02’, ‘27-JUN-02’)
ORDER BY HIREDATE;
F) List the Enames those have five characters in their Names.
SELECT ename
FROM Emp
WHERE ename like ’_ _ _ _ _’;
G) List the emps who does not belong to Deptno 20.
SELECT *
FROM Emp
WHERE deptno != 20;
Display the employee name and department number of all employees in
departments 10 and 30 in alphabetical order by name.
SELECT ename, deptno
FROM Emp
WHERE deptno in (10,30)
ORDER BY ename;
H) Display the name and job title of all employees who do not have a manager.
SELECT ename, job
FROM Emp
WHERE supereno IS NULL;
Database Systems Lab
Mid Exams
I) Identify errors in this statement? Correct it.
SELECT empno, ename
Sal * 12 ANNUAL SALARY
FROM emp;
SELECT empno, ename, sal*12 “ANNUAL SALARY”
FROM Emp;
J) What is the output of the following SQL statement:
SELECT ename, CONCAT(ename, job), LENGTH(ename), INSTR(ename, 'A')
FROM emp
WHERE SUBSTR(ename,-1,1) = 'm';
Output:
Database Systems Lab
Mid Exams
Q5) Consider a table named product with data in the following columns:
id (primary key), name, category, and price.
id name category price
105 rose flower 5.70
108 desk furniture 120.00
115 tulp flower 6.50
123 sunflower flower 7.50
145 guitar music 300.00
155 orchid flower 9.50
158 flute music 156.00
A) Create a table called flower from product table that includes only flower
category. (Copy structure and data)
CREATE TABLE flower
AS
SELECT *
FROM product
WHERE category=”flower”;
B) For all products in music category make a discount 25%.
UPDATE product
SET price = price * 0.75
WHERE category = 'music';
C) Use product table update the price of product with ID=115 to the same as
product with ID=105.
UPDATE product
SET price = (SELECT price FROM product WHERE id=105)
WHERE id=115;
D) For the column NAME in product table ADD constraint unique.
ALTER TABLE product
ADD CONSTRAINT product_name_un UNIQUE(name);
E) For the column price in product table must be positive number, set constraint
for that.
ALTER TABLE product
ADD CONSTRAINT product_price_positive CHECK price>0;
Database Systems Lab
Mid Exams
Q6) What is the output of the following SQL statements.
#1 select round(1285.923 , -2) from dual; 1300
#2 select concat('Data' , concat(' base' , ' EXAM')) Data base EXAM
from dual;
#3 select substr('AMMANJOR' , -9, 2) from dual; NULL
#4 select instr('That is' , 'is') from dual; 6
#5 select lpad('BAUFET' , 10 , '*') from dual; ****BAUFET
#6 select 30
trunc(months_between(sysdate,'23-SEP-22'))
from dual; Note: sysdate=’9-APR-25’
#7 select to_char(sysdate , 'ddth "of" Month') 09th of April
from dual; Note: sysdate=’9-APR-25’
#8 select round(159.69,-1) from dual; 160
#9 select rpad(round(889966.52, 0), 10, '*') from dual; 889967****
#10 Assuming the SYSDATE is 20-APR-23, What will be Today is: 20-APR-23
the outcome of the following query?
select concat(‘Today is: ’ , SYSDATE) from dual;
#11 select instr(‘Green' , 'e') from dual; 3
#12 select trunc(2902.92,-3) from dual; 2000
#13 What value will be returned after executing the 04-JAN-2013
following statement? Note that 01-JAN-2013
occurs on a Tuesday.
select next_day('01-JAN-2013' , 'friday') from dual;
Database Systems Lab
Mid Exams
Q7) Examine the structure of the EMPLOYEES table as given, write SQL queries for the
following question.
Column Name Nullable Data Type
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)
A) You need to display the names of the employees who have the letter 's' in their
first name and the letter 't' at the second position in their last name. write a
query to display the required output?
SELECT first_name, last_name
FROM employees
WHERE UPPER(first_name) LIKE '%S%'
AND UPPER(SUBSTR(last_name, 2, 1)) = 'T';
B) Display all employee last name and job ID and substitute NULL job IDs with a
literal 'Unknown'.
SELECT last_name, NVL(job_id, 'Unknown') AS job_id
FROM employees;
C) Display the salaries for the employees whose name start with a 'P' if the
salaries are NULL.
SELECT salary
FROM employees
WHERE (first_name LIKE ‘P%’) AND (salary IS NULL);
Database Systems Lab
Mid Exams
D) Display employees with the first name starting with 'P' and
salary+(salary*commission_pct) will be returned if the employee earns a
commission.
SELECT first_name, (salary+(salary*commission_pct))
FROM employees
WHERE (first_name LIKE ‘P%) AND (commission_pct IS NOT NULL);
E) Generate a report which shows the first name and the salary for all the
employees in the department 10. The report should show the results in the form
'Andy Smith earned 50000'.
SELECT first_name || ‘Earned ‘ || salary
FROM employees
WHERE department_id=10;
F) lists the employees who have worked more than 1 year in the company.
SELECT *
FROM employees
WHERE MONTHS_BETWEEN(SYSDATE, hire_date) > 12;
G) Write a query to display the following output:
Salary
KING’s salary is
12500
JIHAD’s salary is
8200
SELECT first_name || q'[‘s salary is ]' || sal
FROM employees;
H) You need to generate a report that displays the IDs of all employees in the
EMPLOYEE whose salary is at least 25% more than the value 20000.
SELECT employee_id
FROM employees
WHERE sal > 20000*1.25;
.تم بفضل هللا
ّ
.اللهم ارحم ( ق ـ ــاس ـ ــم م ـ ـف ـ ـي ـ ــد ع ـ ـم ـ ــرو ) واجعله من ورثة جنة النعيم