0% found this document useful (0 votes)
4 views

Exercise 8

Uploaded by

MOUNIKA ARADHANA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Exercise 8

Uploaded by

MOUNIKA ARADHANA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

EXERCISE 8

Examples on SUB/SUMMARY Queries Using IN, ANY, SOME, ALL, EXISTS and NOT EXISTS
functions
A sub query is a form of an SQL statement that appears inside another SQL statement. It
is also termed as nested query. The statement containing a sub query is called a parent
statement. The parent statement uses the rows returned by the sub query.
It can be used by the following commands:
 To insert records in a target table.
 To create tables and insert records in the table created.
 To update records in a target table.
 To create views.
 To provide values for conditions in WHERE, HAVING, IN etc. used with SELECT, UPDATE, and
DELETE statements.

Before discussing sub queries, create below relations.


1. Create the table
Table name: - Department
Column Name Data Type Size Attributes
DEPT_NO char 4 Primary Key
DEPT_NAME char 25 NOT NULL
LOCATION char 30

2. Create the table


Table name: - employee1
Column Name Data Type Size Attributes
EMP_NO number 4 Primary Key
EMP_FNAME varchar 20 NOT NULL
EMP_LNAME varchar 20 NOT NULL
DEPT_NO varchar 4 Foreign Key references DEPT_NO of
department

3. Create the table


Table name: - projects
Column Name Data Type Size Attributes
PROJECT_NO char 4 Primary Key
PROJECT_NAME varchar 20 NOT NULL
BUDGET number 8,2 NOT NULL

1
4. Create the table
Table name: - works_on
Column Name Data Type Size Attributes
EMP_NO number 4 Foreign Key references EMP_NO of employee1
PROJECT_NO char 4 Foreign Key references PROJECT_NO of projects,
PRIMARY KEY(EMP_NO, PROJECT_NO)
JOB char 15 NOT NULL
ENTER_DATE date

Insert data into department

Insert data into Employee1

2
Insert data into Project

Insert data into Works_on

Sub queries and Comparison Operators: -

1. Write a query to get the first and last names of employees who work in the Research
department.

Sub queries and the IN operator: -

The IN operator allows you to determine if a value matches any value in a list of values.
The syntax of the IN operator is
value IN (value1, value2, value3,...)
3
The IN operator returns 1 (true) if the value equals any value in the list (value1, value2, value3,
…). Otherwise, it returns 0.
Examples: -

1. Write a query to get full details of all employees whose department is located in Dallas

2. Write a query to get the last names of all employees who work on the project Apollo.

Correlated Sub queries


A sub query is said to be a correlated sub query if the inner query depends on the outer query
for any of its values.

3. Write a query to get the last names of all employees who work on project p3

Sub queries and ANY and ALL Operators: -


The operators ANY and ALL are always used in combination with one of the comparison
operators. The general syntax of both operators is
column_name operator [ANY | ALL] query

where operator stands for a comparison operator and query is an inner query.

4
The ANY operator evaluates to TRUE if the result of the corresponding inner query contains at
least one row that satisfies the comparison. The keyword SOME is the synonym for ANY.

4. Write a query to get the employee numbers, project numbers, and job names for
employees who have not spent the most time on one of the projects.

The ALL operator evaluates to TRUE if the evaluation of the table column in the inner query
returns all values of that column.

Sub queries and the EXISTS Function: -


The EXISTS operator is a Boolean operator that returns either true or false.
The EXISTS operator is often used to test for the existence of rows returned by the sub query.
The NOT operator negates the EXISTS operator. In other words, the NOT EXISTS returns true if
the sub query returns no row, otherwise it returns false.

5. Write a query to get the last names of all employees who work on project p1.

6. Write a query to get the last names of all employees who work for departments not
located in Seattle.

Example Queries: -
To practise the queries, use the relation in exercise 7(sailors, boats, reserves)

1. Find the names of sailors who have reserved boat no 103.

5
(OR)

(OR)

2. Find the names of sailors who have reserved a red boat.

(OR)

3. Find the names of sailors who have not reserved a red boat.

4. Find sailors whose rating is better than some sailor called Bob.

6
5. Find the sailors with the highest rating.

6. Find the names of sailors who have reserved both a red and a green boat.

(OR)

7. Find the names of sailors who have reserved all boats.

8. Find the average age of all sailors.

9. Find the average age of sailors with a rating of 10.

7
10. Find the name and age of the oldest sailor.

(OR)

(OR)

11. Count the number of sailors.

12. Count the number of different sailor names.

13. Find the names of sailors who are older than the oldest sailor with a rating of 10.

(OR)

8
14. Find the age of the youngest sailor for each rating level.

15. Find the age of the youngest sailor who is eligible to vote (i.e., is at least 18 years old)
for each rating level with at least two such sailors.

16. Find the average age of sailors for each rating level that has at least two sailors.

17. For each red boat, find the number of reservations for this boat.

You might also like