Salik Bashir Lab10 Dbms 5620
Salik Bashir Lab10 Dbms 5620
10
Instructions
This is individual Lab work/task.
Complete this lab work within lab timing.
Discussion with peers is not allowed.
You can consult any book, notes & Internet.
Copy paste from Internet will give you negative marks.
Lab work is divided into small tasks, complete all tasks sequentially.
Show solution of each lab task to your Lab Instructor.
Paste your solution (i.e. code) in given space under each task.
Also make a zip/rar archive of all lab tasks and upload this file on LMS
before leaving lab.
In-Lab Exercises/Tasks
FACILITIES REQUIRED AND PROCEDURE
Facilities required to do the experiment
SN Facilities Quantity
o Required
1 System 1
2 Operating System Windows 8.1
3 Front End Java
4 Back End Oracle 11g, MySQL
Step Detail
1 Subqueries
The query within another is known as a subquery. A
statement containing sub query is called parent statement.
The rows returned by a subquery are used by the parent
statement.
2 Types of Subqueries
Single-row subquery
Queries that return only one row from the inner SELECT
statement.
Multiple-row subqueries
Queries that return more than one row from the inner SELECT
statement. The results can be obtained using the operators
IN, ANY and ALL.
Multiple-column subqueries
Queries that return more than one column from the inner
SELECT statement.
Correlated subquery
A sub query is evaluated once for the entire parent statement
whereas a correlated sub query is evaluated once per row
processed by the parent statement.
SQL COMMANDS
Using a Subquery
Example:
select eno, ename
where salary >
(select sal
from employee
where ename =’JONES’);
Single-Row Subqueries
Example:
select ename, job
from emp
where job =
(select job
from emp
where empno =7369);
Multiple-Row Subqueries
Example:
select ename, sal, deptno
from emp
where sal in
(select min(sal)
from emp
group by deptno);
Multiple-Column Subqueries
Example:
select ordid, prodid, qty
from item
where (prodid, qty) in
(select prodid, qty
from item
where ordid =605);
Correlated subquery
Example:
select *
from emp x
where x.salary >
(select avg(salary)
from emp
where deptno =x.deptno);
Tasks
Q1: Display all employee names and salary whose salary is greater
than minimum salary of the company and job title starts with ‘M’.
Answer:
Use select from clause.
Use like operator to match job and in select clause to get the result.
Answer:
SQL> select ename, job
from emp
where job =
(select job
from emp
where empno = 7369)
and sal >
(select sal
from emp
where empno =7876):
ENAME JOB
---------- --------------------
MILLER CLERK
Q3: Issue a query to display the employee name, job title and salary
for all employees whose salary is equal to the minimum salary.
Answer:
SQL> select ename, job, sal
from emp
where sal =
(select min(sal)
from emp);
Q4: Issue a query to display all the that have a minimum salary
greater than that of department 20.
Answer:
SQL> select deptno, min(sal)
from emp
group by deptno
havint min(sal) >
(select min(sal)
from emp
where deptno = 20);
DEPTNO MIN(SAL)
---------- --------------------
10 1300
30 950
Multi-row Subqueries
Q5: List the employee name, salary and department No for all
employees who earn the same salary as the minimum salary for the
department.
Answer:
SQL> select ename, sal, deptno
from emp
where sal in (800, 950, 1300);
Q6: Issue a query to display the employee No, name, job title whose
salary is less than any clerk and who are not clerk.
Answer:
SQL> select empno, ename, job
from emp
where sal < any
(select sal
from emp
where job = ‘CLERK’)
and job <> ‘CLERK’;
Q7: Issue a query to display the employee No, name, job title whose
salary is greater than the average salaries of all department.
Answer:
SQL> select empno, ename, job
from emp
where sal > all
(select avg(sal)
from emp
group by deptno);
Multiple-column Subqueries
Q8: Issue a query to display the order number, product number, and
quantity of any item in which the product number and quantity
match both the product number and quantity of an item in order
605.
Answer:
SQL> select ordid, prodid, qty
from item
where (prodid, qty) in
(select prodid, qty
from item
where ordid = 605)
and ordid <> 605;
Answer:
SQL> select a.ename, a.deptno, b.salavg
from emp a,
(select deptno, avg(sal) salavg
from emp
group by deptno) b
where a.deptno = b.deptno
and a.sal > b.
….
6 rows selected.
OUTCOME
Thus the nested queries and join queries were performed and executed
successfully.
Extra Tasks
Q1. Write a query to display all the orders from the orders table issued by
the salesman 'Ali’.
Q2. Write a query to display all the orders for the salesman who belongs to
the city Islamabad.
Paste code and screenshot here
Q3. Write a query to find all the orders issued against the salesman who may
works for customer whose id is 3007.
Q4. Write a query to display all the orders which values are greater than the
average order value for 10th October 2012.
Paste code and screenshot here
Q5. Write a query to find all orders attributed to a salesman in Islamabad.
Paste code and screenshot here
Q6. Write a query to display the commission of all the salesmen servicing
customers in Multan.