Sie sind auf Seite 1von 7

University of Wollongong, Dubai Campus

Spring Session 2010


CSCI 235 – Databases
Tutorial 4 – Week 4
Topic: More on SQL Plus

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.

SQL> select w.authorname


2 from Book b, Writtenby w
3 where w.ISBN=b.ISBN and b.title='Infomatics'
4 INTERSECT
5 select w2.authorname
6 from Book b2,Writtenby w2
7 where w2.ISBN=b2.ISBN and b2.title='Databases';

AUTHORNAME
---------------
Benoit
McAven

2 rows selected.

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

SQL> select w.ISBN


2 from Writtenby w
3 where w.authorname='Benoit'
4 UNION
5 select w1.ISBN
6 from Writtenby w1
7 where w1.authorname='Nolan';
ISBN
----------
101
202
500

3 rows selected.

3. Find the names of Authors who wrote SQL Primer but not Databases.

SQL> select w.authorname


2 from Writtenby w, Book b
3 where w.ISBN=b.ISBN and b.title='SQL Primer'
4 INTERSECT
5 select w1.authorname
6 from Writtenby w1, Book b1
7 where w1.ISBN=b1.ISBN and b1.title!='Databases';

AUTHORNAME
---------------
Nolan

1 row selected.

4. Find the names of Authors who wrote at least one book.

SQL> select DISTINCT authorname from Writtenby;

AUTHORNAME
---------------
Antonini
Benoit
McAven
Nolan
Williams

5 rows selected.

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

SQL> select B.title


2 from Book B, Writtenby W
3 where B.ISBN=W.ISBN
4 and W.yrpublished='1998';
TITLE
---------------
SQL Primer

1 row selected.

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

SQL> select a.authorname


2 from Author a
3 MINUS
4 select w.authorname
5 from Writtenby w;

AUTHORNAME
---------------
Gandhi

1 row selected.

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

SQL> select a.authorname


2 from Author a
3 where EXISTS(select w.authorname
4 from Writtenby w, Book b
5 where w.authorname=a.authorname AND w.ISBN=b.ISBN
6 AND b.title='Infomatics');

AUTHORNAME
---------------
Antonini
Benoit
McAven

3 rows selected.

8. Find Authors who are older than some author from Australia.

SQL> select a1.authorname


2 from Author a1
3 where a1.nationality<>'Australian'
4 AND a1.age > ANY
5 (select a.age
6 from Author a
7 where a.nationality='Australian');
AUTHORNAME
---------------
Williams
Gandhi
Benoit
Antonini

4 rows selected.

9. Find Authors who are older than all authors from Australia.

SQL> select a1.authorname


2 from Author a1
3 where a1.nationality<>'Australian'
4 AND a1.age > ALL
5 (select a.age
6 from Author a
7 where a.nationality='Australian');

AUTHORNAME
---------------
Williams

1 row selected.

10. List the ISBN and title of all books with at least one Australian author.

SQL> select DISTINCT b.ISBN, b.title


2 from Book b, Writtenby w, Author a
3 where b.ISBN=w.ISBN
4 AND w.authorname=a.authorname
5 AND a.nationality='Australian';

ISBN TITLE
---------- ---------------
202 SQL Primer
246 Databases
500 Infomatics

3 rows selected.

11. Find the names of all female authors who wrote databases.
SQL> select a.authorname
2 from Author a, Writtenby w, Book b
3 where b.ISBN=w.ISBN
4 AND w.authorname=a.authorname
5 AND a.sex='Female'
6 AND b.title='Infomatics';

AUTHORNAME
---------------
Benoit
McAven

2 rows selected.

12. Find the average age of authors.

SQL> select AVG(age)


2 from Author;

AVG(AGE)
----------
53.1666667

13. Find the average age of authors who are not Italian.

SQL> select AVG(age)


2 from Author
3 where nationality!='Italian';

AVG(AGE)
----------
57.2

1 row selected.

14. Find the name and nationality of oldest and youngest Author.

SQL> select authorname,nationality


2 from Author
3 where age=(select min(age) from author);

AUTHORNAME NATIONALIT
--------------- ----------
McAven Australian

1 row selected.
SQL> select authorname,nationality
2 from Author
3 where age=(select max(age) from author);

AUTHORNAME NATIONALIT
--------------- ----------
Williams British

1 row selected.

15. Find the number of books which their version number is greater than 1.

SQL> select count(*)


2 from Book
3 where version_no>1;

COUNT(*)
----------
3

1 row selected.

16. Find the names of authors who are older than the oldest author who is Australian.

17. Find the average age of authors for each nationality that has at least one author.

Author
Authorname Sex Age Nationality
Benoit Female 42 French
Nolan Male 65 Australian
Antonini Male 33 Italian
McAven Female 31 Australian
Williams Male 90 British
Ghandi Female 58 Indian

Book
ISBN Title Publisher Version_No
101 Databases Possum 1
202 SQL Primer Hall 2
246 Databases West 1
400 Modula 2 Possum 2
444 Advanced SQL Hall 1
500 Informatics Hall 4

WrittenBy
ISBN Authorname yrpublished no_of_copies
101 Benoit 1993 100
246 Williams 2000 120
246 McAven 2000 120
500 McAven 2002 90
500 Benoit 2002 90
500 Antonini 2002 90
202 Nolan 1998 130

Das könnte Ihnen auch gefallen