SQL Assignment 1 With Solution
SQL Assignment 1 With Solution
SQL Assignment 1 With Solution
max_limit
grade
C
B
B
B
B
Single-row subquery is the query that returns only one value or row to the calling select
statement
1. Display the name of the teacher who is oldest among all teachers.
Explanation: to know the name of the teacher who is the oldest, you need to first find the
minimum birth and then corresponding to that date display the name of the teacher.
select
from
where
f_name, l_name
teacher
birthdate = (select min(birthdate)
from teacher);
2. Display teacher numbers and names of those teachers who are earning less than Jatin.
Explanation: to find the list of teachers earning less than Jatin, you need to find first the
salary of Jatin.
select
from
where
Multiple-Row Subquery are the queries that return only more than one value or rows to the
calling select statement.
3. Display the list of all teachers who are earning equal to any teacher who have joined before
31-dec-94
Explanation: first you need to know the salaries of all those who have joined before 31-dec94 and then any teacher whose salary matches any of these returned salaries. IN operator
looks for this existence into the set. You can also use Distinct to avoid duplicate salary tuples.
select
from
where
4. Display the list of all those teachers whose salary is greater than any other teacher with job
title PRT.
Explanation: first you need to know the salaries of all those who are PRT and then any
teacher whose salary is greater than any of these returned salaries. ANY operator looks for
inequality.
select
from
where
5. Display the list of all those teachers whose salary is greater than all the teachers with job title
as PRT.
Explanation: First you need to know the salaries of all those who are PRT and then any
teacher whose salary is greater than all of these returned salaries. ALL operator looks for
inequality.
select
from
where
Single-row subquery
Operator Description
=
Equal to
>
Greater than
<
>=
Less than
Greater than equal to
<=
<>
Multiple-row subquery
Operator Description
Returns true if any of the values
IN
in the list match i.e. equality
check
Returns true if all the values
ALL
returned by the subquery match
the condition
Returns true if any of the values
ANY
returned by subquery match the
condition
Operators for subquery
Multiple-Column subquery is the subquery that returns more than one column to the calling or
main select statement. For example:
6. Display the list of all teachers whose job title and salary is same as that of the employee
whose first name is Jaideep;
Explanation: Firstly you need to find the job title and salary of Jaideep and then you need to
find all other teachers whose job title and salary exactly matches Jaideeps job title and
salary.
select
from
where
Correlated subqueries
This is another type of subquery where teh subquery is executed for each and every record
retrieved by the calling or main select statement. A correlated subquery returns only a Boolean
value (T/F). The operator EXISTS can be used for these types of subqueries.
7. Display the records in the format given below for all class teachers:
Jaideep kumar is a class teacher
Explanation : the main query uses the EXISTS operator to find whether the teacher is a class
teacher or not. If the correlated subquery returns a true value, then the record retrieved by
main select statement is accepted otherwise it is rejected.
select
from
where
Joins
Joins means retrieving data from more than one table in a single query. There are several types of
joins:
A. Equijoin in this type of join, two or more tables are joined over common columns and
common values.
8. Display the name of all the teachers who are class teachers.
select
from
where
f_name, l_name
teacher t, class c
t.t_no=c.t_no;
B. Non-Equijoin In this type of join, two or more tables do not have common columns and
common values but they are joined indirectly. For example:
9. Display names, salaries and salary grades of all teachers.
select
from
where
C) Outer join in this type of join, two or more tables are joined over common column bu the
records may or may not have common values. For example:
10. Display names and class numbers of all the teachers. In addition display the classes of those
teachers who are class teachers. Thus, the result should include names of teachers who are
not class teachers.
select
from
where
E) Cartesian join in this type of join, each record of one table is joined to each record of the
other table i.e., the join condition is not given. For example
12. show all possible teacher-class values
select
from
SALARY SUPERVISOR
GRADE
------ -------------- ------------------------- --------------------------------------- --------- --------- ---------- ----5 Asma Husain
7 naim ahmad
1 faraz ahmad
4 rows selected.
2* where teacher.supervisor='jaideep'
SQL> /
T_NO F_NAM
TITLE
L_NAME
SALARY SUPERVISOR
JOININGDA BIRTHDATE
khan
14000 jaideep
12-MAR-09
f) Display the names and numbers of all teachers who are class teachers and are in
grade C.
select t_no,f_name
4*
AND '17999')
QL> /
T_NO F_NAME
--------- ------------------------Arshi
Display details of all those teachers who are class teachers of classes 1 to 5.
where teacher.t_no=class.t_no(+);
F_NAME
L_NAME
CLASS_NO
ahmad
Sharma
5
8
Ali
Khan
Husain
6
3