You are on page 1of 5

University of Wollongong in Dubai

CSCI 235 – Databases


Select Queries Examples

Task 1
This task refers to the library database discussed and implemented in Week 3 lab class.
The relational schema is repeated below:

Book (ISBN, title, publisher, version_no)

WrittenBy (ISBN, authorname, yrpublished, no_of_copies)

Author (authorname, sex, age , nationality)

Formulate SQL queries for the following requests:

1. Find the names of Authors who wrote both Informatics and Databases
books.
Select w.authorname
From WrittenBy w, Book b
Where w.ISBN = b.ISBN and w.title = ‘Informatics’
INTERSECT
Select w1.authorname
From WrittenBy w1, Book b1
Where w1.ISBN = b1.ISBN and w1.title = ‘Databases’

2. Find the ISBN of Books which is written by Benoit or Nolan.


Select w.ISBN
From WrittenBy w,
Where w.authorname = ‘Benoit’
INTERSECT
Select w1.ISBN
From WrittenBy w1
Where w1.authorname = ‘Nolan’

3. Find the names of Authors who wrote SQL Primer but not Databases.
Select w.authorname
From WrittenBy w, Book b
Where w.ISBN = b.ISBN and w.title = ‘SQL Primer’
INTERSECT
Select w1.authorname
From WrittenBy w1, Book b1
Where w1.ISBN = b1.ISBN and w1.title = ‘Databases’
4. Find the names of Authors who wrote at least one book.
Select w.authorname
From WrittenBy w;

5. Find the names of Books which were published in 1998.


Select b.title
From Book b, WrittenBy w
Where b.ISBN = w.ISBN and w. yrpublished = 1998

6. Find the names of Authors who didn’t write a book.


Select a.authorname
From author A
Where authorname NOT IN
(Select w.authorname
From WrittenBy w)

7. Find the names of Authors who wrote Informatics. (use Exist)


Select a.authorname
From Author a
Where EXISTS (
Select w.authorname
From WrittenBy w, Book b
Where w.ISBN = b.ISBN and b.title = ‘Informatics’ and a.authorname =
w.authorname)

8. Find Authors who are older than some author from Australia.
Select a1.authorname
From author a1
Where a1.age > ANY
(Select a.age
From Author a
Where nationality = ‘Australian’)

9. Find Authors who are older than all authors from Australia.
Select a1.authorname
From author a1
Where a1.age > ALL
(Select a.age
From Author a
Where a.nationality = ‘Australian’)

10. List the ISBN and title of all books with at least one Australian author.
Select b.title, b.ISBN
From Author a, Book b, WrittenBy w
Where a.authorname = w.authorname and b.ISBN=w.ISBN and
a.nationality=’Australian’

11. Find the names of all female authors who wrote databases.
Select a.authorname
From Author a, Book b, WrittenBy w
Where a.authorname = w.authorname and b.ISBN=w.ISBN and a.sex= ‘Female’
and b.title = ‘Databases’

12. Find the average age of authors.


Select AVG (age)
From author

13. Find the average age of authors who are not Italian.
Select AVG (age)
From author
Where nationality <> ‘Italian’

14. Find the name and nationality of oldest and youngest Author.
Oldest:
Select a1.name, a1.nationality
From author a1
Where a1.age = (Select MAX(a.age) From Author a)
Youngest:
Select a1.name, a1.nationality
From author a1
Where a1.age = (Select MIN(a.age) From Author a)

15. Find the number of books which their version number is greater than 1.
Select count(*)
From Book b
Where b.version_no > 1

16. Find the names of authors who are older than the oldest author who is
Australian.
Select a1.name
From author a1
Where a1.age > (
Select MAX(a.age)
From Author a
Where a.nationality = ‘Australian’)

17. Find the average age of authors for each nationality that has at least one
author.
Select AVG (age)
From Author a
GroupBy a.nationality
Having count(*) > 1

Task 2
The relational Schema is reported below:

Employees (ssn, name. age, nationality)


Departments (did, dname, budget)
Manages (ssn,did, since)

Formulate SQL Queries for the following requests:

1. Find the names of Employees who work for both “Export” and “Sales”
departments.
Select e.name
Form Employees e, Manages m, Department d
Where e.ssn = m.ssn and d.did=m.did and d.dname = ‘Export’
INTERSECT
Select e1.name
Form Employees e1, Manages m1, Department d1
Where e1.ssn = m1.ssn and d1.did=m1.did and d1.dname = ‘Sales’

2. Find the names and ages of Employees who work for “Marketing” department
but not “Export”.
Select e.name, e.age
Form Employees e, Manages m, Department d
Where e.ssn = m.ssn and d.did=m.did and d.dname = ‘Marketing’
EXCEPT
Select e1.name, e1.age
Form Employees e1, Manages m1, Department d1
Where e1.ssn = m1.ssn and d1.did=m1.did and d1.dname = ‘Export’
3. Find all information of Employees who are form “Canada” and work for
“Accounting” department.
Select *
Form Employees e, Manages m, Department d
Where e.ssn = m.ssn and d.did=m.did and d.dname = ‘Accounting’ and e.nationality
=’Canada’

4. Find the average age of Employees for each department that has at least 5
Employees.
Select AVG (a.age)
Form Employees e, Manages m, Department d
Where e.ssn = m.ssn and d.did=m.did
GroupBy d.did
Having count(*) > 5

5. Find the names and ssn of Employees that are older than some employees from
“Sales” Department.
Select e1.name, e1.ssn
From Employees e1
Where e1.age > ANY
(Select e.age
Form Employees e, Manages m, Department d
Where e.ssn = m.ssn and d.did=m.did and d.dname = ‘Sales’)

You might also like