SQL Exercises

Tables:

Showing the contents of the tables

>> select * from course;

COURSENOCNAMECDATE
1Basic Accounting11-JAN-89
2Further Accounting25-JAN-89
3Issues In Administration27-SEP-88
4More Administration16-OCT-88
5Ada01-NOV-88
6Introduction To Ingres05-FEB-89
7New Topologies10-MAR-89
8LANs12-DEC-88
9Structural Stress Analysis20-FEB-89
10Taffic Flow Analysis24-OCT-88

>> select * from department;

DEPNODNAMELOCATIONHEAD
1accountsfloor 35
2administrationfloor 25
3software designfloor 12
4communicationsfloor 43
5engineeringfloor 57

>> select * from empcourse;

EMPNOCOURSENO
11
12
21
22
73
74
83
84
145
146
155
156
195
196
217
227
228

>> select * from employee;

EMPNOSURNAMEFORENAMESDOBADDRESSTELNODEPNO
1JonesElizabeth Barbara05-JAN-4426 Agnews Terrace, Shamrock Bay212 337 22881
2SmithRobert07-FEB-4718 Marsh Street, Tollcross, Edinburgh031 732 89721
3WhiteAllan05-MAY-616 Remote Place, North Berwick121 555 66221
4ReidGordon10-AUG-639 Noble Road, Penicuik629 424 67131
5MacCallanClaire18-SEP-5825 Crisis Avenue, Leith, Edinburgh031 337 41661
...

Simple selects

What is Robert Smith's address?
select address from employee where surname='Smith' and forenames='Robert';
What is Allan White's phone number?
select telno from employee where surname='White' and forenames='Allan';
Where is the engineering department?
??select location from department where dname='engineering';
What department is on the second floor?
??select dname from department where location='floor 2';
What are the phone numbers of people with last name Smith or Jones?
??select surname, forenames, telno from employee where surname='Jones' or surname='Smith';

Complex Joins

What department is Robert Smith in?
select dname from employee, department
where employee.depno=department.depno and surname='Smith' and forenames='Robert';
Who's in accounts?
select surname, forenames from employee, department
where employee.depno=department.depno and dname='accounts';
What floor does Gordon Reid work on?
??select location from employee, department
where employee.depno=department.depno and surname='Reid' and forenames='Gordon';
Give me the names and phone numbers of everyone who works on the first floor.
??select surname, forenames, telno from employee, department
where employee.depno=department.depno and location='floor 1';
Who's taking Ada?
??select surname, forenames from course, empcourse, employee
where empcourse.courseno=course.courseno and empcourse.empno=employee.empno and cname='Ada';
What courses are Robert Smith taking?
??select cname from course, empcourse, employee
where empcourse.courseno=course.courseno and empcourse.empno=employee.empno and surname='Smith' and forenames='Robert';
What courses are the people from the second floor taking?
??select surname, forenames, cname from course, empcourse, employee, department where department.depno=employee.depno and empcourse.courseno=course.courseno and empcourse.empno=employee.empno and location='floor 2';


Valid XHTML 1.0! Valid CSS!
Last updated: $Date: 2006/08/27 14:35:52 $