csci235 spring 2010 tutorials query examples

6
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’

Upload: technofreak9

Post on 18-Nov-2014

106 views

Category:

Documents


0 download

DESCRIPTION

SQL Queries. Loads of them.

TRANSCRIPT

Page 1: CSCI235 Spring 2010 Tutorials Query Examples

University of Wollongong in DubaiCSCI 235 – Databases

Select Queries Examples

Task 1This 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.authornameFrom WrittenBy w, Book bWhere w.ISBN = b.ISBN and w.title = ‘Informatics’INTERSECTSelect w1.authornameFrom WrittenBy w1, Book b1Where w1.ISBN = b1.ISBN and w1.title = ‘Databases’

2. Find the ISBN of Books which is written by Benoit or Nolan.Select w.ISBNFrom WrittenBy w, Where w.authorname = ‘Benoit’INTERSECTSelect w1.ISBNFrom WrittenBy w1Where w1.authorname = ‘Nolan’

3. Find the names of Authors who wrote SQL Primer but not Databases.Select w.authornameFrom WrittenBy w, Book bWhere w.ISBN = b.ISBN and w.title = ‘SQL Primer’INTERSECTSelect w1.authornameFrom WrittenBy w1, Book b1

Page 2: CSCI235 Spring 2010 Tutorials Query Examples

Where w1.ISBN = b1.ISBN and w1.title = ‘Databases’

4. Find the names of Authors who wrote at least one book.Select w.authornameFrom WrittenBy w;

5. Find the names of Books which were published in 1998.Select b.titleFrom Book b, WrittenBy wWhere b.ISBN = w.ISBN and w. yrpublished = 1998

6. Find the names of Authors who didn’t write a book.Select a.authornameFrom author AWhere authorname NOT IN(Select w.authornameFrom WrittenBy w)

7. Find the names of Authors who wrote Informatics. (use Exist)Select a.authornameFrom Author aWhere EXISTS (Select w.authornameFrom WrittenBy w, Book bWhere 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.authornameFrom author a1Where a1.age > ANY(Select a.ageFrom Author aWhere nationality = ‘Australian’)

9. Find Authors who are older than all authors from Australia.Select a1.authornameFrom author a1Where a1.age > ALL(Select a.ageFrom Author aWhere a.nationality = ‘Australian’)

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

Page 3: CSCI235 Spring 2010 Tutorials Query Examples

From Author a, Book b, WrittenBy wWhere 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.authornameFrom Author a, Book b, WrittenBy wWhere 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 authorWhere nationality <> ‘Italian’

14. Find the name and nationality of oldest and youngest Author.Oldest:Select a1.name, a1.nationalityFrom author a1Where a1.age = (Select MAX(a.age) From Author a)Youngest:Select a1.name, a1.nationalityFrom author a1Where 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 bWhere b.version_no > 1

16. Find the names of authors who are older than the oldest author who is Australian.Select a1.nameFrom author a1Where 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)

Page 4: CSCI235 Spring 2010 Tutorials Query Examples

From Author aGroupBy a.nationalityHaving count(*) > 1

Task 2The 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.nameForm Employees e, Manages m, Department dWhere e.ssn = m.ssn and d.did=m.did and d.dname = ‘Export’INTERSECTSelect e1.nameForm Employees e1, Manages m1, Department d1Where 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.ageForm Employees e, Manages m, Department dWhere e.ssn = m.ssn and d.did=m.did and d.dname = ‘Marketing’EXCEPTSelect e1.name, e1.ageForm Employees e1, Manages m1, Department d1Where 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 dWhere 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)

Page 5: CSCI235 Spring 2010 Tutorials Query Examples

Form Employees e, Manages m, Department dWhere e.ssn = m.ssn and d.did=m.didGroupBy d.didHaving count(*) > 5

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