Exercise 8
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.
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
2
Insert data into Project
1. Write a query to get the first and last names of employees who work in the Research
department.
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.
3. Write a query to get the last names of all employees who work on project p3
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.
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)
5
(OR)
(OR)
(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
10. Find the name and age of the oldest sailor.
(OR)
(OR)
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.