0% found this document useful (0 votes)
20 views17 pages

Salik Bashir Lab10 Dbms 5620

Lab No. 10 focuses on performing sub-queries using DML commands, with specific objectives and instructions for individual lab work. It includes examples of various types of subqueries, such as single-row, multiple-row, multiple-column, and correlated subqueries, along with tasks that require writing SQL queries based on given scenarios. The lab concludes with a successful execution of nested and join queries, emphasizing the utility of subqueries in SQL.

Uploaded by

salikbashir5620
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views17 pages

Salik Bashir Lab10 Dbms 5620

Lab No. 10 focuses on performing sub-queries using DML commands, with specific objectives and instructions for individual lab work. It includes examples of various types of subqueries, such as single-row, multiple-row, multiple-column, and correlated subqueries, along with tasks that require writing SQL queries based on given scenarios. The lab concludes with a successful execution of nested and join queries, emphasizing the utility of subqueries in SQL.

Uploaded by

salikbashir5620
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

LAB NO.

10

Name : Salik bashir


Roll.No : 5620
Sub-Queries

To perform sub-queries using DML commands.


Objectives

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

Procedure for doing the experiment

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.

SQL> select ename, sal from emp where sal >


(select min(sal) from emp where job like 'A%');
ENAME SAL
------------ ----------
Ali 12000
Gulfam 20000
Kareem 15000
Q2: Issue a query to display the employees whose job title is the
same as that of employee 7369 and whose salary is greater than
that of employee 7876.

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);

ENAME JOB SAL


---------- -------------------- ----------
SMITH CLERK 800

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’;

EMPNO ENAME JOB


---------- -------------------- ----------
7654 MARTIN SALESMAN
7521 WARD SALESMAN

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);

EMPNO ENAME JOB


---------- -------------------- ---------------------
7839 KING PRESIDENT
7566 JONES MANAGER
7902 FORD ANALYST
7788 SCOTT ANALYST

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;

ORDID PRODID QTY


---------- -------------------- --------------------
617 100861 100
617 100870 500
616 102130 10
Q9: Create a query to display the employee names, salaries,
department numbers, and average salaries for all the employees
who make more than the average salary in their department.

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.

ENAME SAL DEPTNO SALAVG


---------- -------------------- ---------------------- ------------------------
KING 5000 10 2916.6667
JONES 2975 20 2175
SCOTT 3000 20 2175

….
6 rows selected.

OUTCOME
Thus the nested queries and join queries were performed and executed
successfully.

QUESTIONS AND ANSWERS

What is the use of sub queries?


A subquery is a select-from-where expression that is nested with in another
query. A common use of subquery is to perform tests for set membership,
make set comparisons, and determine set cardinality.

Extra Tasks
Q1. Write a query to display all the orders from the orders table issued by
the salesman 'Ali’.

Paste code and screenshot here

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.

Paste code and screenshot here

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.

You might also like